Click here to Skip to main content
15,896,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I don’t know how to complete my query...

What I have tried:

C#
public void btnDelete_Click(object sender, EventArgs e)
        {
            DMSDataContext db = new DMSDataContext();
            int userId = Int32.Parse(txtId.Text);
            User u = db.Users.Single(u1 => u1.Id == UserId); //here - how to get UserId from grid view?
            u.Id = Int32.Parse(txtId.Text);
            if (u != null)
            {
                db.Users.DeleteOnSubmit(u);
                db.SubmitChanges();
                MessageBox.Show("Record Deleted");
            }
            
      }
Posted
Updated 9-Feb-16 6:57am
v2
Comments
[no name] 7-Feb-16 13:40pm    
You wrote code to delete record. By the way what is problem exactly : it is not deleting or getting any exception. Please provide details.
Philippe Mori 12-Feb-16 22:54pm    
That code does not make much sense. Since you try to load a record with it id (code won't work because UserId should start with a lowercase to match the declaration).
And why do you parse the ID textbox twice. If the record exist in the database, then it will have the proper id and if it does not exist, then you should not delete it anyway.
And by the way, if the record does not exist, then u.Id would cause an access violation. Not only that line is useless but it fails in which case an error message will be displayed.
Philippe Mori 12-Feb-16 22:55pm    
Usually you cannot or should not change the key of a database record.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900