vb.net and sql server(delete cmd)

Discussion in 'SQL Server' started by rockyy, Feb 10, 2010.

  1. rockyy

    rockyy New Member

    Joined:
    Feb 6, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    can anyone tell me the right way


    its showing the msg even when the author id doesn't exist

    i want it sshould delete only those id's which are present in the database
    else it shld give msg no recorsd found

    my code please correct me: )
    Code:
    
    If TextBox2.Text <> "" Then
    
                connectionString = "Data Source=hello\SQLEXPRESS;Initial Catalog=mytanveer;Integrated Security=True"
    
                SQLStr = "DELETE FROM author WHERE author='" & TextBox1.Text & "'"
    
                'Write to SQL
    
                SQLConn.ConnectionString = connectionString             SQLConn.Open() 
    
                SQLCmd.Connection = SQLConn 
                SQLCmd.CommandText = SQLStr 
                SQLCmd.ExecuteNonQuery() 
    
                SQLConn.Close()   
                MsgBox("author data deleted", MsgBoxStyle.OkOnly)
            Else
    
                MsgBox("Enter the required values:" & vbNewLine & "1. Au_ID")
    
            End If
    
    
        End Sub
    
    
     

    Attached Files:

  2. sql-programs

    sql-programs New Member

    Joined:
    Oct 21, 2009
    Messages:
    14
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software Developer
    Home Page:
    http://www.sql-programmers.com
    If TextBox2.Text <> "" Then
    int authorId=Convert.Toint32(TextBox2.Text.ToString())

    connectionString = "Data Source=hello\SQLEXPRESS;Initial Catalog=mytanveer;Integrated Security=True"

    SQLStr = "DELETE FROM author WHERE author=" & authorId

    'Write to SQL

    SQLConn.ConnectionString = connectionString
    SQLConn.Open()
    SQLCmd.Connection = SQLConn
    SQLCmd.CommandText = SQLStr
    SQLCmd.ExecuteNonQuery()

    SQLConn.Close()
    MsgBox("author data deleted", MsgBoxStyle.OkOnly)
    Else

    MsgBox("Enter the required values:" & vbNewLine & "1. Au_ID")

    End If


    End Sub


    Please Check it
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice