title: insert to database checked row in datagridview in c# using ado.net
body: <p>Code:</p>
  
  <pre><code>int i = 0;
  List&lt;int&gt; ChkedRow = new List&lt;int&gt;();
  for (i = 0; i &lt;= dataGridView1.RowCount; i++)
  {
      if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "yes")
      {
          ChkedRow.Add(i);
      }
  }
  
  if (ChkedRow.Count == 0)
  {
      MessageBox.Show("Select one checkbox");
      return;
  }
  
  foreach (int j in ChkedRow)
  {
      try
      {
          Query = "Insert into dbo.tbl_Class(Class_Name,Category,Description,Item) values ('" + txtname.Text + "','" + cbo_cat.Text + "','" + txtdesc.Text + "','" + dataGridView1.Rows[j].Cells[1].Value.ToString() + "')";
          SqlCommand cmd = new SqlCommand(Query, con);
          con.Open();
          cmd.ExecuteNonQuery();
          con.Close();
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
  }
  MessageBox.Show("Records successfully inserted");
  </code></pre>
  
  <p>I want to save in sql the row that i selected in my datagridview but I'm having a <strong>error in this line</strong> <code>"if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "yes")"</code> . and I don't know why. can you please help me with this? or suggest another code or another approach? Please help.</p>