delete statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zaiena
    New Member
    • Jun 2007
    • 20

    delete statement

    hi,
    i'm working in asp.net using visual studio 2005 with vb language,
    my tables is access database, will i'm trying to write the delete statement to delete some fields in my tables i wrote this statement and have no errors but the row didn't deleted. here is my code:

    [CODE=vbnet]Public Function DeleteQuestion( ByVal Question_ID As Integer)
    Dim cmd As New OleDbCommand("D elete from Questions where Question_id= @Question_id)", conn)
    Try
    conn.Open()
    cmd.ExecuteNonQ uery()
    Catch ex As Exception
    ex.Message.ToSt ring()
    End Try
    conn.Close()
    If er = "" Then

    Return "the Question has been Deleted succefully!!!!"

    Else

    Return er
    End If
    End Function
    End Class[/CODE]
    so any one can help or tell me where the problem is or if i miss something here????
    Last edited by Shashi Sadasivan; Jan 6 '08, 10:03 PM. Reason: adding code tags
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    How did you check that no exceptions were thrown?

    From your code, if the exception occours, you are creating a reference of the message exception string but nothing happens with that reference.

    it would be better if you put it in a messagebox so some output window.

    Since your database is Access you should use brackets around field names

    [CODE=sql]Delete from [Questions] where [Question_id]= @Question_id[/CODE]

    The other thing about the command you wrote is, what is @Question_id ?
    when and wher are u assigning it?


    Your query should look more likee
    [CODE=sql]Delete from [Questions] where [Question_id]= ?[/CODE]

    and use the AddParameter method for the sql command

    Comment

    • kunal pawar
      Contributor
      • Oct 2007
      • 297

      #3
      try this code

      [CODE=vbnet]Dim cmd As New OleDbCommand("D elete from Questions where Question_id=" & Question_ID , conn)

      And update ur function
      Public Function DeleteQuestion( ByVal Question_ID As Integer)
      Dim cmd As New OleDbCommand("D elete from Questions where Question_id=" & Question_ID , conn)
      Try
      conn.Open()
      cmd.ExecuteNonQ uery()
      Catch ex As Exception
      ex.Message.ToSt ring()
      End Try
      conn.Close()
      If er = "" Then

      Return "the Question has been Deleted succefully!!!!"

      Else

      Return er
      End If
      End Function
      End Class[/CODE]
      Last edited by Shashi Sadasivan; Jan 7 '08, 05:06 AM. Reason: adding code tags

      Comment

      • zaiena
        New Member
        • Jun 2007
        • 20

        #4
        Originally posted by kunal pawar
        try this code

        [CODE=vbnet]Dim cmd As New OleDbCommand("D elete from Questions where Question_id=" & Question_ID , conn)

        And update ur function
        Public Function DeleteQuestion( ByVal Question_ID As Integer)
        Dim cmd As New OleDbCommand("D elete from Questions where Question_id=" & Question_ID , conn)
        Try
        conn.Open()
        cmd.ExecuteNonQ uery()
        Catch ex As Exception
        ex.Message.ToSt ring()
        End Try
        conn.Close()
        If er = "" Then

        Return "the Question has been Deleted succefully!!!!"

        Else

        Return er
        End If
        End Function
        End Class[/CODE]

        hello,
        i'm trying your statement and i'ts totaly work thank you very much.

        Comment

        Working...