Run time error on: objCommand.ExecuteNonQuery()

M

Matthew Louden

I just tried to create a simple ASP.NET application to add record to the SQL
Server database. However, it has run time error on Line 88:
objCommand.ExecuteNonQuery()

Any ideas?? Please help!! Thanks!!

Sub AddNewRecord()
Dim objConnection As SqlConnection
Dim strConnection As String = "Data Source=20.11.12.91;Network
Library=DBMSSOCN;Initial Catalog=testtable;User ID=sa;Password=iw;"
objConnection = New SqlConnection(strConnection)
Dim objCommand As SqlCommand
Dim strSQLQuery As String
strSQLQuery = "INSERT INTO Test " _
& "(FirstName, LastName, SerialNumber) " _
& "VALUES (@TextValue, @TextValue, @TextValue)"
objCommand = New SqlCommand(strSQLQuery, objConnection)
objCommand.Parameters.Add(New SqlParameter("@TextValue", SqlDbType.Char,
255))
objCommand.Parameters.Add(New SqlParameter("@TextValue", SqlDbType.Char,
255))
objCommand.Parameters.Add(New SqlParameter("@TextValue", SqlDbType.Char,
255))
objCommand.Parameters("@TextValue").Value = CStr(TextBox1.Text)
objCommand.Parameters("@TextValue").Value = CStr(TextBox2.Text)
objCommand.Parameters("@TextValue").Value = CStr(TextBox4.Text)
objConnection.Open()
objCommand.ExecuteNonQuery()
objConnection.Close()
End Sub

Runtime error:

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: ?? ??
'@TextValue'?(?) ?? ???????. ?? ??? ?? ?? ?? ?? ?? ???? ??? ???? ???.

Source Error:

Line 86: ' Open the connection, execute the command, and close the
connection.
Line 87: objConnection.Open()
Line 88: objCommand.ExecuteNonQuery()
Line 89: objConnection.Close()
Line 90:
 
M

michael

& "VALUES (@TextValue, @TextValue, @TextValue)"

should be:

& "VALUES ('@TextValue', '@TextValue', '@TextValue')"

notice the single-quotes

Hope this helps...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top