Saving modified data in gridview on clicking SaveButton

Cri Aticus 1 Reputation point
2021-06-30T07:35:52.26+00:00

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!

C#
C#
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
    2021-06-30T08:00:08.697+00:00

    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
    2021-07-01T05:09:40.07+00:00

    HiCriAticus-8329,
    >>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.
    https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.htmlElba-3401

    0 comments No comments