简体   繁体   中英

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()

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

change this

 FullName = (NewUserName.Text as User).ID

To this

 FullName = NewUserName.Text

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM