Click here to Skip to main content
15,892,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow Every one i have a problem during Developed our final symester Database Project
the code you see below is delete data in datagirdview but not delete data in database table ? And my database table varible type is nvarchar and when we delete data in datagridview they display the follow error
system.data.sqlclient.sqlException(0*8103365) Conversion failed when converting the
'nvarchar' value "مال مسروقہ و برامدگی" to datatype int at system.data........
C#
private void firdel_Click(object sender, EventArgs e)
      {
          DataTable tb = new DataTable();
          connection_crime cr = new connection_crime();
          SqlConnection abc = new SqlConnection(cr.crim());
         // abc.Open();
          for (int i = 0; i < firGridView1.Rows.Count; i++)
          {
              DataGridViewRow dr = firGridView1.Rows[i];
              if (dr.Selected == true)
              {
                  firGridView1.Rows.RemoveAt(i);
                  try
                  {

          foreach (DataGridViewRow row in firGridView1.SelectedRows)
              if (!row.IsNewRow) firGridView1.Rows.Remove(row);
          int selectedCount = firGridView1.SelectedRows.Count;
          while (selectedCount > 0)
          {
              if (!firGridView1.SelectedRows[0].IsNewRow)
                  firGridView1.Rows.RemoveAt(firGridView1.SelectedRows[0].Index);
              selectedCount--;
          }


                      abc.Open();
                      SqlCommand cmd = new SqlCommand("Delete from FIRrecord where namplc2 =" + i + "", abc);
                      cmd.ExecuteNonQuery();
                      SqlDataReader c = cmd.ExecuteReader();
                      abc.Close();
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
              }



plz any one tell me how we correct the above code
Posted
Comments
swapnilKumbhar 13-Apr-12 5:37am    
This method is very complicated.Use SQL Notification.
ZurdoDev 13-Apr-12 8:32am    
Just step through it with a debugger. Should be easy to find the issue.

1 solution

Try adding single quotes to the string.

C#
SqlCommand cmd = new SqlCommand("Delete from FIRrecord where namplc2 ='" + i + "'", abc);


Here: my_data is converted to INT or a column name depending on the database server
SQL
Delete from FIRrecord where namplc2 = my_data


Here: my_data is converted to NVARCHAR
SQL
Delete from FIRrecord where namplc2 = 'my_data'


Despite that, I think your code will have problems because you changed the datagridview rows collection while iterating using the variable i.

The problem is this:
i = 0 firGridView1.Rows.Count = 4
i = 1 firGridView1.Rows.Count = 4
i = 2 firGridView1.Rows.Count = 4 // If this row is for deletion
i = 3 firGridView1.Rows.Count = 3
 
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