Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i use this code to delete selected datagrid from data base but it is not delete
 
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
   DataGridViewRow dr = dataGridView1.Rows[i];
   if (dr.Selected == true)
   {
      dataGridView1.Rows.RemoveAt(i);
      try
      {
         SqlCommand cmd = new SqlCommand("Delete from calender where ID= ' " + i + "'", con);     
         cmd.ExecuteNonQuery();
         con.Close();
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.ToString());
      }
   }
}
 
[edit]code block added and indexation fixed[/edit]
Posted 16-Nov-12 22:05pm
Edited 16-Nov-12 23:09pm
Nelek55.5K
v2
Comments
faisal23 at 17-Nov-12 4:15am
   
windows / web
n.podbielski at 17-Nov-12 6:11am
   
No problem? No error?
meme_307 at 17-Nov-12 6:33am
   
i want to delete data from data grid
the not deleted from database
i worked in windows
i make button to delete the selected row from grid ,, it delete just from grid not data base
pryashrma at 17-Nov-12 7:08am
   
does value of database field "ID" and variable "i" are same?
meme_307 at 17-Nov-12 7:26am
   
no it is not the same so it is not delete ,,,
,,,
now i need delete code to delete the selected row from database
meme_307 at 17-Nov-12 7:33am
   
the id not necessary in data base i cane delete it but i need delete the selected row from data grid

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi
 
Can you try after removing space between single quotes and double quotes from query.
I.E
 
 SqlCommand cmd = new SqlCommand("Delete from calender where ID= '" + i + "'", con); 
  Permalink  
Comments
meme_307 at 17-Nov-12 6:53am
   
i tried it's not delete from database
Dominic Abraham at 17-Nov-12 7:06am
   
Can you try directly removing the value from database after giving a value for i. I.E . Execute the query
Delete from calender where ID = '1' in database. Just to make sure that, there is no issues with your query/data. And if you are able to delete the data, just try the same query from code [after inserting the deleted data]
 
ie.
 
SqlCommand cmd = new SqlCommand("Delete from calender where ID= '1'", con);
cmd.ExecuteNonQuery();
con.Close();

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

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 384
3 PIEBALDconsult 245
4 BillWoodruff 243


Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100