Run time error on: objCommand.ExecuteNonQuery()

  • Thread starter Thread starter Matthew Louden
  • Start date Start date
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:
 
Is it possible that you have to name your parameters
different names????? Just guessing

-----Original Message-----
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'?
(?) ?? ???????. ?? ??? ?? ?? ?? ?? ?? ???? ??? ???? ???.
 
Hi Matthew,

You should use different parameter names for each parameter, otherwise you
will assign value only to the first parameter

strSQLQuery = "INSERT INTO Test " _
& "(FirstName, LastName, SerialNumber) " _
& "VALUES (@TextValue1, @TextValue2, @TextValue3)"
objCommand = New SqlCommand(strSQLQuery, objConnection)
objCommand.Parameters.Add(New SqlParameter("@TextValue1", SqlDbType.Char,
255))
objCommand.Parameters.Add(New SqlParameter("@TextValue2", SqlDbType.Char,
255))
objCommand.Parameters.Add(New SqlParameter("@TextValue3", SqlDbType.Char,
255))
objCommand.Parameters("@TextValue1").Value = CStr(TextBox1.Text)
objCommand.Parameters("@TextValue2").Value = CStr(TextBox2.Text)
objCommand.Parameters("@TextValue3").Value = CStr(TextBox4.Text)
 
Back
Top