BB987
BB987

Reputation: 181

adding user to database from textbox in web forms

I am trying to save a username to a database after clicking the Add User button but having trouble this is what i have:

 private void btnAddUser_Click(object sender, EventArgs e)
    {
        using (var db = new DocMgmtDataContext())
        {
            User user = new User()
            {
                FullName = (NewUserName.Text as User).ID
            };
            db.Users.InsertOnSubmit(user);
            db.SubmitChanges();
        }
        UpdateUserLists();
    }

it is not liking the as User part.

MY SOLUTION

FullName = NewUserName.Text, ID = Guid.NewGuid()

Upvotes: 0

Views: 203

Answers (3)

gzaxx
gzaxx

Reputation: 17590

I assume that from your NewUserName Textbox you want to get user FullName so change your code to this:

private void btnAddUser_Click(object sender, EventArgs e)
{
    using (var db = new DocMgmtDataContext())
    {
         User user = new User()
         {
                FullName = NewUserName.Text //fix
         };
         db.Users.InsertOnSubmit(user);
         db.SubmitChanges();
    }
}

Upvotes: 1

Andre Loker
Andre Loker

Reputation: 8408

NewUserName.Text is probably of type String and you're trying to cast it to a User. This will certainly not work.

Try this:

 FullName = NewUserName.Text

Upvotes: 2

scartag
scartag

Reputation: 17680

change this

 FullName = (NewUserName.Text as User).ID

To this

 FullName = NewUserName.Text

Upvotes: 1

Related Questions