Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I am struggling to do a save button in C# Windows Form Application where I have a GridView and I want to save all modifications I do in GridView Table(I connected it to SQL). This is the data sourc...
#3: Post edited
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.- ```c#
- 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!
- I am struggling to do a save button in C# Windows Form Application where I have a GridView and I want to save all modifications 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 stuck.
- ```c#
- 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!
#2: Post 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!
- 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.
- ```c#
- 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!
#1: Initial revision
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!