Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I would like to know on how to update data from the datagridview to database. I have two datagridview and two buttons. At form load, Datagridview1 will display all the records from tblAssessment. Then if the user clicked button 1, it will select the top 2 students based from their results. It will then be passed to Datagridview2. the user will saved the data from the datagridview2 to the database. But I got problem in updating the database. How to fix this? Below is my code:


void fillGrid() // loads data from the database to datagridview1
      {
          p_table = new DataTable();
          p_table.Clear();

          m_da = new SqlDataAdapter("Select AssessmentNumber,StudentFirstName,StudentMiddleName,StudentLastName,SchoolYear,GradeLevel,Result from tblSectioning Where GradeLevel='" + label2.Text + "' AND SectionName='" + "Null" + "'", conn);
          m_da.Fill(p_table);
          dataGridView1.DataSource = p_table;
      }


      void sumofall() //sum of all enrolled students
      {

          string query = "SELECT COUNT (*) FROM tblAssessment where GradeLevel ='" + label2.Text +  "'";
          SqlDataAdapter dAdapter = new SqlDataAdapter(query, conn);
          DataTable source = new DataTable();
          dAdapter.Fill(source);
          label7.Text = source.Rows[0][0].ToString();
          loginpage1 frr = new loginpage1();
          frr.numberofstudents = label7.Text;


      }


      private void button1_Click(object sender, EventArgs e) //gets the top 2 in the datagridview1
      {


          for (int i = 0; i <= 4; i++)
          {
              p_table1 = new DataTable();
              p_table1.Clear();
              m_da1 = new SqlDataAdapter("Select Top 2 AssessmentNumber,StudentFirstName,StudentMiddleName,StudentLastName,GradeLevel,Result FROM tblSectioning Where GradeLevel='" + label2.Text + "' ORDER BY Result Desc", conn);

              m_da1.Fill(p_table1);
              dataGridView2.DataSource = p_table1;
          }


      }
      private void button2_Click(object sender, EventArgs e) //updating of data from datagridview 2 to database
      {


              conn = new SqlConnection(connec.GetServer());
              conn.Open();
              cmd = new SqlCommand("update  tblStudentInformation set SectionName='" + textBox1.Text + "', ShiftSched='" + comboBox1.SelectedItem + "' Where  GradeLevel='" + label2.Text + "'", conn);
              m_dr = cmd.ExecuteReader();
              conn.Close();
              m_dr.Close();


              conn = new SqlConnection(connec.GetServer());
              conn.Open();
              cmd1 = new SqlCommand("update  tblClass set SectionName='" + textBox1.Text + "', ShiftSched='" + comboBox1.SelectedItem + "' Where  GradeLevel='" + label2.Text + "'", conn);
              m_dr1 = cmd1.ExecuteReader();
              conn.Close();
              m_dr1.Close();
              MessageBox.Show("Record Updated");

              dataGridView2.ClearSelection();
              fillGrid();


      }
Posted
Comments
What is the issue? What did you find out while debugging?
DarkDreamer08 18-Jan-15 18:03pm    
there is no issue on my code. It's just that whenever I clicked the button2 to update, nothing happens. that is what I want to solve, but I cannot
[no name] 18-Jan-15 21:25pm    
It was a very reasonable question. What DID you find out while debugging. If no issue with code then no question. Maybe just learn to use debugger.
DarkDreamer08 18-Jan-15 21:42pm    
What I mean is that the record does not update whenever I clicked the button. as far as I know, my query is correct. Maybe it looks like that is the issue? :)
ExecuteReader() is not required. Use ExecuteNonQuery();

1 solution

You need
command.ExecuteNonQuery();
instead of
cmd1.ExecuteReader();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900