CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    27

    Lightbulb Deleting records

    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.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Deleting records

    Code:
    SQL CODE:::: Stored procedure
    
    CREATE PROCEDURE [dbo].[Del_From_Table]
    
    	@id int
    
    	DELETE 
    	FROM   [Table]
    	WHERE  
    		[id] = @id
    and in VB ..
    Code:
                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...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Nov 2008
    Posts
    27

    Re: Deleting records

    Quote Originally Posted by GremlinSA View Post
    Code:
    SQL CODE:::: Stored procedure
    
    CREATE PROCEDURE [dbo].[Del_From_Table]
    
    	@id int
    
    	DELETE 
    	FROM   [Table]
    	WHERE  
    		[id] = @id
    and in VB ..
    Code:
                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...

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Deleting records

    Okay that's not so difficult ..

    Code:
                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...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured