Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to delete the gridview data when submit button click

I have gridview and a button on my webpage.

when pageloads, the data displays on gridview.

so when i clicks the submit button i want to delete the complete data from table.
so it cannot be displayed in gridview.

this is my code

SqlConnection conDelete = new SqlConnection(_connString);
         SqlCommand cmdDelete = new SqlCommand();
         cmdDelete.Connection = conDelete;
        cmdDelete.CommandText = "Delete from students";
        using (cmdDelete)
        {
            conDelete.Open();

            cmdDelete.ExecuteNonQuery();
            conDelete.Close();
        }


this is not working please help.


thanks
Posted
Updated 15-Apr-13 2:25am
v2
Comments
Bojjaiah 15-Apr-13 8:25am    
First you have to know the delete query and try to execute in sqlserver after that you can try c#. your delete query is wrong DELETE FROM TABLENAME WHERE condition<search>
fjdiewornncalwe 15-Apr-13 9:34am    
It isn't wrong syntax. This syntax is perfectly valid, but dangerous.
Naz_Firdouse 15-Apr-13 8:55am    
no need of where condition for delete query...
check this
http://msdn.microsoft.com/en-us/library/ms189245(v=sql.105).aspx
rmksiva 15-Apr-13 9:04am    
it shows any Error ? then please share

You need to define connection for sqlcommand[^], like this:
C#
SqlConnection conDelete = new SqlConnection(_connString);
conDelete.Open();
SqlCommand cmdDelete = new SqlCommand("DELETE FROM Students WHERE StudentId=1", conDelete);
int retVal = cmdDelete.ExecuteNonQuery();
//retval stores value for recoreds affected


Remember, you need to define records range to DELETE (T-SQL)[^], but if you want to delete all data, WHERE statement is not needed:
SQL
DELETE 
FROM Students

Above query differs from TRUNCATE TABLE (T-SQL)[^].
 
Share this answer
 
v2
Use this :-

SQL
TRUNCATE TABLE students


Good luck
 
Share this answer
 
v2
try for this:-

VB
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=atisource;Initial Catalog=BillingSys;Persist Security Info=True;User ID=sa;Password=12345678"
con.Open()
cmd.Connection = con
cmd.CommandText = "DELETE FROM table_name"
cmd.ExecuteNonQuery()

Catch ex As Exception
MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")
Finally
con.Close()
End Try
 
Share this answer
 
try this !!

where condition is needed for delete query..

string query = "delete from tab1 where id=" + ddl.SelectedValue;
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = query;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
 
Share this answer
 
Comments
Naz_Firdouse 15-Apr-13 8:55am    
no need of where condition for delete query...
check this
http://msdn.microsoft.com/en-us/library/ms189245(v=sql.105).aspx
sainadh.chintha 15-Apr-13 9:13am    
otherwise simply use, "delete from tab1". It is working for me..

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900