mardi 3 mars 2015

Edit/Update datagridview using sql without indexer (C# Winform)

-I have a datagridview with 4 columns: UserID, FirstName, LastName, Email


-When I double-click a row, another form pops up with 4 textboxes with the row's values inside those, each textbox with the cell value


-When I click the submit button, that row is updated with the textbox values.


-What I have works with "....where UserID = @UserID " . But if a column cell I'm changing IS the UserId, the wrong row, or no row, gets modified.


My Question: How can I edit a row for ANY column value and it changes the right row WITHOUT an incrementer column?


Code:



private void button1_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection();
SqlCommand com = new SqlCommand();
sc.ConnectionString = ("My Connection String");
sc.Open();
com.Connection = sc;
com.CommandText = ("update JoshUserTable SET UserID = @UserID, FirstName=@FirstName,LastName=@LastName,Email=@Email where UserID = @UserID");
com.Parameters.AddWithValue("@UserID", txtUserID.Text);
com.Parameters.AddWithValue("@FirstName", txtFName.Text);
com.Parameters.AddWithValue("@LastName", txtLName.Text);
com.Parameters.AddWithValue("@Email", txtEmail.Text);
com.ExecuteNonQuery();
sc.Close();
}

Aucun commentaire:

Enregistrer un commentaire