Saving modified data in gridview on clicking SaveButton

Cri Aticus 1 Reputation point

I am strugglig doing a save button in C# Windows Form Application where I have a GridView and I want to save all modification I do in GridView Table(I connected it to sql). This is the data source for my database DataTable dtCustomers = new DataTable();

I did this so far and got stucked.

private void SaveButton_Click(object sender, EventArgs e)
string connectionString = Properties.Settings.Default.dbConnectionString;
SqlConnection con = new SqlConnection(connectionString);
using (con)
SqlCommand cmd = con.CreateCommand();
using (cmd)
cmd.CommandText = @"UPDATE CUSTOMERS SET " +
"CustomerId = @CustomerId, " +
"FirstName = @Firstname , " +
"LastName = @LastName, " +
"Email = @tiedtlaw email , " +
"Height = @Height, " +
"DateOfBirth = @DateOfBirth ";
cmd.Parameters.Add("@CustomerId", SqlDbType.Int, 5, "StudentID");
cmd.Parameters.Add("@Firstname ", SqlDbType.VarChar, 50, "FirstName");
cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 50, "LastName");
cmd.Parameters.Add("@tiedtlaw email ", SqlDbType.VarChar, 1, "Email");
cmd.Parameters.Add("@Height", SqlDbType.Int, 50, "Height");
cmd.Parameters.Add("@DateOfBirth", SqlDbType.Date, 10, "DateOfBith");
cmd.Connection = con;
using (SqlDataAdapter da = new SqlDataAdapter())
da.UpdateCommand = cmd;
// da.Update();
Thank you for any suggestion!

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,218 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 112.1K Reputation points

    Try another command:

    cmd.CommandText = @"UPDATE CUSTOMERS SET
    FirstName = @FirstName, 
    LastName = @LastName, 
    Email = @Email, 
    Height = @Height, 
    DateOfBirth = @DateOfBirth 
    WHERE CustomerId = @CustomerId";

    What happens in this case?

    0 comments No comments

  2. Daniel Zhang-MSFT 9,611 Reputation points

    >>I did this so far and got stucked.
    What specific problem did you encounter? Please explain in detail.
    And refer to the following code(I only listed a few fields):

    cmd.CommandText = @"update CUSTOMERS set CustomerId= @CustomerId, FirstName= @FirstName,LastName = @LastName where CustomerId= = @oldCustomerId";  
    cmd.Parameters.Add("@CustomerId= ", SqlDbType.Int, 5, "StudentID");  
    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 50, "FirstName");  
    cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 50, "LastName");  
    SqlParameter parameter = cmd.Parameters.Add("@oldCustomerId", SqlDbType.Int, 5, "StudentID");  

    More details I suggest you follow this document.
    Best Regards,
    Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments