minimatt
minimatt

Reputation: 59

unhandled exception error won't allow me to insert data

So if i run this code i'm thrown a multi-part identifier errorregarding the binding of the textbox "clid"

Can somebody please help to resolve this issue?

Using cnnentry As New SqlConnection("Data Source=NB-1492\sqlipt;Initial Catalog=Bookings;Integrated Security=True;Pooling=False")
    Dim entrclientid As String
    Dim clid As New TextBox
    clid = FormView1.FindControl("txtclientid")
    entrclientid = Convert.ToString(clid)
    Dim sqlentry1 As String = "INSERT INTO BOOKING_DETAILS(CLIENT_ID) VALUES("
    Dim sqlentry2 As String = entrclientid
    Dim sqlentry3 As String = ")"
    Dim sqlentry4 As String = sqlentry1 & sqlentry2 & sqlentry3
    Dim cmdclientid As New SqlCommand(sqlentry4, cnnentry)
    cnnentry.Open()
    string1 = cmdclientid.ExecuteNonQuery()
    cnnentry.Close()
End Using

Thanks in advance

P.S. please excuse the naming conventions i wrote this extremely quickly

Upvotes: 1

Views: 44

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460008

You don't want to convert the TextBox to a String but it's Text property:

change

entrclientid = Convert.ToString(clid)

to

entrclientid = clid.Text

But more important, you should use sql-parameters to prevent sql-injection.

Dim sql As String = "INSERT INTO BOOKING_DETAILS(CLIENT_ID) VALUES(@CLIENT_ID)"
Dim cmdclientid As New SqlCommand(sql, cnnentry)
cmdclientid.Parameters.AddWithValue("@CLIENT_ID", clid.Text)

Upvotes: 2

Related Questions