开发者

How to delete data on page_load event if literal3.text="N"?

I want to delete record on page load event ... using vb.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Literal3.Text = "N" Then
    Dim con As New SqlConnection
                Dim cmd As New SqlCommand
                con.ConnectionString = "My connection string"
                con.Open()
                cmd.Connection = con
                cmd.CommandText = "DELETE FROM a1_ticket WHERE Ticket_no='" & Literal3.Text & "'"
                cmd.ExecuteNonQuery()
                con.Close()
                cmd = New SqlCommand("DELETE FROM a1_holds WHERE Ticket_no='" & Li开发者_Go百科teral3.Text & "'", con)
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
End If
End Sub


cmd.CommandType = System.Data.CommandType.Text

Put a try{} catch and see if you catch an Exception?

use a SQL Query Like this:

cmd.CommandText = String.Format("DELETE FROM a1_ticket WHERE Ticket_no='{0}'" Literal3.Text.ToString())

and Debug the cmd.CommandText and see what the String outputs as.

is Ticket_no a VARCHAR or INT?


A few things come to mind...

  • You're overloading Literal3 - using the field both as a flag to control the flow into the delete statement, and also as the number of the ticket to be deleted.
  • Is the ticket number really a string? If not, then why are you quoting it within the delete statement?
  • you should really parameterise the query to avoid injection attacks
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜