Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
i am using below code for updating database value, but it is not working

please help

I am new to c#

private void button1_Click(object sender, EventArgs e)
{
  if (objConn1.State != ConnectionState.Open)
  {
    objConn1.Open();
  }

  for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
  {
    DialogResult result;
    result = MessageBox.Show("Are You sure want to update?", "Conformation", MessageBoxButtons.YesNo);
    if (result == System.Windows.Forms.DialogResult.Yes)
    {
      //string sql = "update   [SAPProduction].[ProductionData] set Rejection  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
      // Jobcard = your desire Rows Jobcard Id
      SqlCommand command = new SqlCommand("UPDATE ProductionData_BK SET Rejection = @Reject Where id = '" + dataGridView1.CurrentRow.Cells["id"].Value.ToString() + "'  ", objConn1);

      command.Parameters.AddWithValue("@Reject",dataGridView1.SelectedCells[13].ToString());

      //objConn1.Open();
      command.ExecuteNonQuery();
      objConn1.Close();
    }
  }
}


[edit]spelling and indexation[/edit]
Posted
Updated 24-Jun-13 23:52pm
v5

 
Share this answer
 
Comments
Syed Faheem Abbas 10-Jun-15 9:04am    
hate msdn
CHill60 10-Jun-15 9:20am    
You might hate it, but nothing beats RTFM
//replace this c# code to the right place...
// you can use the debugger to see were exactly the problem is...

string query = "UPDATE ProductionData_BK " +
"SET Rejection=@Reject; " +
"Where id = 'dataGridView1.CurrentRow.Cells['id'].Value.ToString()', objConn1"

SqlCommand insertCommand = new SqlCommand(query, objConn1);
 
Share this answer
 
v2
private void button1_Click(object sender, EventArgs e)
{
if (objConn1.State != ConnectionState.Open)
{
objConn1.Open();
}

for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
DialogResult result;
result = MessageBox.Show("Are You sure want to Delete?", "Conformation", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
//string sql = "update [SAPProduction].[ProductionData] set Rejection " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "' ";
// Jobcard = your desire Rows Jobcard Id
SqlCommand command = new SqlCommand("UPDATE SAPProduction.ProductionData_BK SET Rejection = @Reject Where id = '" + dataGridView1.CurrentRow.Cells["id"].Value.ToString() + "' ", objConn1);

command.Parameters.AddWithValue("@Reject", dataGridView1.CurrentRow.Cells["Rejection"].Value.ToString());
command.ExecuteNonQuery();
MessageBox.Show("Row Updated...");

objConn1.Close();

}
}
}
 
Share this answer
 
First issue I see is that it will set All entries to be set to rejection is that query is run.

You are missing the "Where" clause of the SQL statement.

However, other than that are you getting any errors.. then phrase "not working" does not tell us much. What exactly is not working?
 
Share this answer
 
Comments
Master Vinu 25-Jun-13 5:51am    
thx pheonyx..... i have update the query, but when i was try to update the cell value its neither update nor give any error
Pheonyx 25-Jun-13 5:54am    
Okay, so my next question is why did you decide not to use a parameter for passing the Id to the query like you did with the @Reject parameter?

After that, When you are stepping through are the correct values being retrieved for "id" and for "@Reject" ?
private void button1_Click(object sender, EventArgs e)
       {
           if (objConn1.State != ConnectionState.Open)
           {
               objConn1.Open();
           }

           for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
           {
               DialogResult result;
               result = MessageBox.Show("Are You sure want to Delete?", "Conformation", MessageBoxButtons.YesNo);
               if (result == System.Windows.Forms.DialogResult.Yes)
               {
                   //string sql = "update   [SAPProduction].[ProductionData] set Rejection  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
                   // Jobcard = your desire Rows Jobcard Id
                   SqlCommand command = new SqlCommand("UPDATE SAPProduction.ProductionData_BK SET Rejection = @Reject Where id = '" + dataGridView1.CurrentRow.Cells["id"].Value.ToString() + "'  ", objConn1);


                   command.Parameters.AddWithValue("@Reject", dataGridView1.CurrentRow.Cells["Rejection"].Value.ToString());



                               //objConn1.Open();
                   command.ExecuteNonQuery();
                   objConn1.Close();

               }
           }
       }
 
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