The Datagrid view does not update when the refresh button is clicked.

In fact, when I click on the rows or cells of the datagrid view, they are filled with text fields for editing after editing and fixing the update, the datagridview does not change instantly, if I close and run the form again, it changes. My requirement is that this should change immediately after clicking the refresh button. The code I use to update is:

private void btnUpdate_Click(object sender, EventArgs e) { SqlConnection con = Helper.getconnection(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; string PrjID = txtPrjID.Text; string PrjName = txtPrjNmae.Text; string Description = txtPrjdescription.Text; string Date = txtPrjDate.Text; string Size = txtPrjSize.Text; string Manager = txtPrjManager.Text; cmd.CommandText = "Update Projects set ProjectName= '" + PrjName + "', Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectID= " + PrjID + " "; MessageBox.Show("Project Details are updated"); dataGridView2.Update(); dataGridView2.Refresh(); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } 

Please tell me what mistake I am making.

+4
source share
3 answers
 SqlConnection con = Helper.getconnection(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; con.Open(); cmd.CommandType = CommandType.Text; string PrjName = txtPrjNmae.Text; string Description = txtPrjdescription.Text; string Date = txtPrjDate.Text; string Size = txtPrjSize.Text; string Manager = txtPrjManager.Text; cmd.CommandText = "Update Projects set Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectName= '" + PrjName + "' "; MessageBox.Show("Project Details are updated"); dataGridView2.Update(); dataGridView2.Refresh(); cmd.ExecuteNonQuery(); con.Close(); 
+1
source

two important things to remember:

First of all, when using DB or Streams, you should use the using statement or try to catch finally and close the connection in the finally block.

second, if you want to update the dataGridView update after updating the database, you must do this after the update. in the code that you do before upgrading.

your third and most important DB command is dangerous and open to SQL Injections . using a parameterized command is almost always better:

 private void btnUpdate_Click(object sender, EventArgs e) { UpdateDB(); dataGridView2.Update(); dataGridView2.Refresh(); } private void UpdateDB() { using (DbConnection con = Helper.getconnection()) { con.Open(); using(DbCommand cmd = con.CreateCommand("Update Projects set ProjectName= @PrjName, Description=@Description , DateStarted=@Date , TeamSize=@Size , Manager=@Manager where ProjectID=@PrjID ")) { cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("PrjName", txtPrjNmae.Text)); cmd.Parameters.Add(new SqlParameter("Description", txtPrjdescription.Text)); cmd.Parameters.Add(new SqlParameter("Date", txtPrjDate.Text)); cmd.Parameters.Add(new SqlParameter("Size", txtPrjSize.Text)); cmd.Parameters.Add(new SqlParameter("Manager", txtPrjManager.Text)); cmd.Parameters.Add(new SqlParameter("PrjID", txtPrjID.Text)); cmd.Connection = con; cmd.ExecuteNonQuery(); } } } 

now I don’t know how you bound the data to the dataGridView , but you may need to get the data from the database again. this part, however, simply contains a call to the same command that you did at the beginning of the program

+3
source

Well, I would say that you are calling things in the wrong order to start ... and you really have to have your connection in the using statement.

 protected void btnUpdate_Click(object sender, EventArgs e) { // Update DB first UpdateProjectDetails(txtPrjID.Text, txtPrjNmae.Text, txtPrjdescription.Text, txtPrjDate.Text, txtPrjSize.Text, txtPrjManager.Text); // Fetch new results from DB IEnumerable<ProjectDetail> projectDetails = GetProjectDetails(); // Update UI dataGridView2.DataSource = projectDetails; dataGridView2.Update(); dataGridView2.Refresh(); // Alert the user MessageBox.Show("Project Details are updated"); } public void UpdateProjectDetails(string prjID, string prjName string description, string date, string size, string manager) { using (DbConnection con = Helper.getconnection()) { con.Open(); DbCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "Update Projects set ProjectName= '" + PrjName + "', Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectID= " + PrjID + " "; cmd.Connection = con; cmd.ExecuteNonQuery(); } } 
+2
source

Source: https://habr.com/ru/post/1501268/


All Articles