question

CriAticus-8329 avatar image
0 Votes"
CriAticus-8329 asked DanielZhang-MSFT edited

Saving modified data in gridview on clicking SaveButton

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 = @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("@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!

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

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://docs.microsoft.com/en-us/answers/articles/67444/email-notifications.htmlElba-3401


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.