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
source share