Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If txtCustomer_No.Text <> "" Then
            cmdDelete.CommandText = "DELETE FROM custmer WHERE Customer_No = " & txtCustomer_No.Text & ";"
            MsgBox(cmdDelete.CommandText)
            cmdDelete.CommandType = CommandType.Text
            cmdDelete.Connection = cnnOLEDB
            cmdDelete.ExecuteNonQuery()  <----- Error Msg :  No value given for one or more required parameters.

            MsgBox("Record deleted.")
            txtCustomer_No.Text = ""

            cmdDelete.Dispose()
        Else
            MsgBox("Enter the required values:" & vbNewLine & "1. Customer_No")
        End If
        cmdUpdate.Dispose()

    End Sub





Thanks in Advance ....
Posted

1 solution

First off, don't do it that way: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Second, the content of your text box is probably responsible for the error: if it contains spaces, or semicolons, or any other special characters SQL will get confused and you will get an error. You could enclose it in quotes, but using parametrised queries will fix the problem, and save your database from damage at the same time.


"So can u give one little example about parametrized ...
i m new about all this stuff.."


Try:
VB
Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("DELETE FROM custmer WHERE Customer_No=@CN", con)
        com.Parameters.AddWithValue("@CN", txtCustomer_No.Text)
        com.ExecuteNonQuery()
    End Using
End Using
 
Share this answer
 
v2
Comments
important17 29-Oct-12 6:15am    
So can u give one little example about parametrized ...
i m new about all this stuff..
OriginalGriff 29-Oct-12 6:22am    
Answer updated.
important17 29-Oct-12 6:30am    
thank you
important17 29-Oct-12 6:38am    
hey one more thing i m using ACCESS database....
your answer structure me not getting
OriginalGriff 29-Oct-12 6:44am    
The example uses SQL - but you do exactly the same thing with OdbcConnection and OdbcCommand objects.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900