Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone

I write some webapp and i must use grid view with deleting and edit button.
I try write deleting function :
C#
try
           {


               connectionString = cnn;
               sqlConnection = new SqlConnection(connectionString);
               sqlConnection.Open();
               SqlCommand cmdDelete = new SqlCommand();
               string Delete = "DELETE FROM Teacher_Major WHERE Teacher_Major.major_id=@id";
               cmdDelete.Connection = sqlConnection;
               cmdDelete.CommandText = Delete;
               int id = int.Parse(gvMajor.Rows[e.RowIndex].Cells[1].Text.ToString());
               cmdDelete.Parameters.AddWithValue("@id", id);
               cmdDelete.ExecuteNonQuery();

           }
           catch (Exception exceptionObj)
           {

           }


but this return error :

Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified.


How i can fix this ?
Thnx for help
Posted
Updated 3-Dec-11 12:46pm
v2
Comments
[no name] 3-Dec-11 18:48pm    
EDIT: added "pre" tag

1 solution

You apparently did not assign a DeleteCommand to the SqlDataSource. If you do so you won't need the code you have written.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.deletecommand.aspx[^]
 
Share this answer
 
Comments
storben 3-Dec-11 19:16pm    
I have one more question how can I get text which i write in editfield after click on edit button ?
[no name] 4-Dec-11 8:13am    
http://www.codeproject.com/KB/webforms/datagriddemo.aspx

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