Reputation: 181
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
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
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
Reputation: 17680
change this
FullName = (NewUserName.Text as User).ID
To this
FullName = NewUserName.Text
Upvotes: 1