Click to See Complete Forum and Search --> : Deleting records


kriti
December 1st, 2008, 11:22 PM
How to delete the records from a sql table in VB.NET when a button is clicked.The records from the table in the database should be deleted.

GremlinSA
December 2nd, 2008, 01:20 AM
SQL CODE:::: Stored procedure

CREATE PROCEDURE [dbo].[Del_From_Table]

@id int

DELETE
FROM [Table]
WHERE
[id] = @id

and in VB .. Dim sqlConn As New SqlConnection([ConnectionString])
Dim cmd As New SqlCommand
Dim dr As SqlDataReader

Try
sqlConn.Open()
With cmd
.Connection = sqlConn
.CommandType = CommandType.StoredProcedure
.CommandText = "Del_from_Table"
.Parameters.AddWithValue("@id", id)
.Connection = sqlConn
dr = .ExecuteReader()

End With

Catch ex As Exception
'Log an error here.....
End Try
It's that easy ...

Gremmy...

kriti
December 4th, 2008, 02:59 AM
SQL CODE:::: Stored procedure

CREATE PROCEDURE [dbo].[Del_From_Table]

@id int

DELETE
FROM [Table]
WHERE
[id] = @id

and in VB .. Dim sqlConn As New SqlConnection([ConnectionString])
Dim cmd As New SqlCommand
Dim dr As SqlDataReader

Try
sqlConn.Open()
With cmd
.Connection = sqlConn
.CommandType = CommandType.StoredProcedure
.CommandText = "Del_from_Table"
.Parameters.AddWithValue("@id", id)
.Connection = sqlConn
dr = .ExecuteReader()

End With

Catch ex As Exception
'Log an error here.....
End Try
It's that easy ...

Gremmy...

can i have a method instead of a stored procedure????i just want to empty out the contents of the table...

GremlinSA
December 4th, 2008, 05:46 AM
Okay that's not so difficult ..

Dim sqlConn As New SqlConnection([ConnectionString])
Dim cmd As New SqlCommand
Dim dr As SqlDataReader

Try
sqlConn.Open()
With cmd
.Connection = sqlConn
.CommandText = "DELETE FROM [Table] WHERE [id] = @id"
.Parameters.AddWithValue("@id", id)
.Connection = sqlConn
dr = .ExecuteReader()

End With

Catch ex As Exception
'Log an error here.....
End Try

Now you have a method ...

Gremmy...