Tuesday, May 01, 2012 11:25 AM
i try to delete a record from a datagrid and DB,,i've written this code:
adapter.DeleteCommand = new SqlCommand("DELETE FROM tabName WHERE ID=@ID", connect); adapter.DeleteCommand.Parameters.Add("@ID", SqlDbType.Int, 0).Value = stdDataGrid.SelectedItem; connect.Open(); adapter.DeleteCommand.ExecuteNonQuery(); MessageBox.Show("Row deleted !! "); adapter.Update(ds, "tabName"); adapter.Fill(ds); connect.Close();
this exception seems to me:
plz tell me what should i do..??
- Edited by aseelalbnna Tuesday, May 01, 2012 11:27 AM
Tuesday, May 01, 2012 11:55 AM
I was able to replicate the exception that ur getting. The cause of the exception is "stdDataGrid.SelectedItem is null".
As a result of this ur getting this error. I would suggest u to check that value in stdDataGrid.SelectedItem is not null before executing the delete command.
Please mark it as an answer if it resolves ur query.
Regards, Parth Shah
- Proposed As Answer by parth.shah Tuesday, May 01, 2012 11:55 AM
Tuesday, May 01, 2012 12:53 PM
thank u for answer.
I actually set a record number that i want to delete and then click on the delete button to execute it..
i thought that this method to set a value in the selectedItem..
how can i put a value in the selected Item?? Is there another way?
Tuesday, May 01, 2012 11:12 PM
i have the same problem,,
in weeks look for the solution
so plz any help??
- Edited by JooRy Tuesday, May 01, 2012 11:13 PM
Wednesday, May 02, 2012 5:29 AM
I am unable to understand the statement in ur above post "I actually set a record number that i want to delete and then click on the delete button to execute it..".
Can u post a sample code so that I can get a better idea about the issue.
If u want to delete the SelectedItem in the datagrid then what ur doing so far is correct. The only check u need to apply is that the selecteditem isn't null.
A sample code will do a world of good to understand the problem and provide a solution.
Regards, Parth Shah
Wednesday, May 02, 2012 8:06 AM
u said " The only check i need to apply is that the selecteditem isn't null"..
how can i do that?
I think that selectedItem takes the value of the selected record number from the datagrid when i do choose't..
Or is there another way to make selecteditem not null
I do not have another code only the code in above post..
Thank you very much for your response
- Edited by aseelalbnna Wednesday, May 02, 2012 2:06 PM