cflores89
asked on
How do I use AddWithValue in Visual Basic?
This is what I have, but it keeps failing, I've added dummy data instead of variables to see if it is set up correctly but it fails everytime. Unless I comment out the executenonquery
the table is defined as:
an_id int Not Null
, cl_id int Not Null
, an_name varchar (35) Null
, an_type varchar (15) Not Null
, an_sex varchar (1) Not Null
the table is defined as:
an_id int Not Null
, cl_id int Not Null
, an_name varchar (35) Null
, an_type varchar (15) Not Null
, an_sex varchar (1) Not Null
Friend Params As String = "INSERT INTO vt_animals VALUES (an_id, cl_id, an_name,an_type,an_sex) VALUES (@an_id,@cl_id,@an_name,@an_type,@an_sex)"
. . .
Sub insertWithParams()
Dim cmd As SqlCommand = New SqlCommand(Params, cnn)
cmd.Parameters.AddWithValue("@an_id", 99999)
cmd.Parameters.AddWithValue("@cl_id", 99999)
cmd.Parameters.AddWithValue("@an_name", "Mike")
'cmd.Parameters.AddWithValue("@an_type", "'lion'")
cmd.Parameters.AddWithValue("@an_sex", "'M'")
Try
cnn.Open()
WriteLine("Attempting to insert a new animal with entered values")
cmd.ExecuteNonQuery()
Catch ex As Exception
WriteLine("Unable to insert with entered values")
Finally
cnn.Close()
End Try
End Sub
and what exception exactly do you get?
ASKER
Attempting to insert a new animal with entered values
Unhandled Exception: System.Data.SqlClient.SqlE xception: The name "an_id" is not
permitted in this context. Valid expressions are constants, constant expression
s, and (in some contexts) variables. Column names are not permitted.
at System.Data.SqlClient.SqlC onnection. OnError(Sq lException exception, Boolea
n breakConnection)
at System.Data.SqlClient.SqlI nternalCon nection.On Error(SqlE xception exception
, Boolean breakConnection)
at System.Data.SqlClient.TdsP arser.Thro wException AndWarning (TdsParser StateObj
ect stateObj)
at System.Data.SqlClient.TdsP arser.Run( RunBehavio r runBehavior, SqlCommand cm
dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
ParserStateObject stateObj)
at System.Data.SqlClient.SqlC ommand.Fin ishExecute Reader(Sql DataReader ds, Run
Behavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlC ommand.Run ExecuteRea derTds(Com mandBehavi or cmdBe
havior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlC ommand.Run ExecuteRea der(Comman dBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult
result)
at System.Data.SqlClient.SqlC ommand.Int ernalExecu teNonQuery (DbAsyncRe sult res
ult, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlC ommand.Exe cuteNonQue ry()
at A05_Flores.A05_Flores.inse rtWithPara ms() in C:\Users\Carlos\Documents\ Home
work\112D\A05_Flores\A05_F lores\A05_ Flores.vb: line 102
at A05_Flores.A05_Flores.Main () in C:\Users\Carlos\Documents\ Homework\1 12D\A0
5_Flores\A05_Flores\A05_Fl ores.vb:li ne 13
Press any key to continue . . .
Unhandled Exception: System.Data.SqlClient.SqlE
permitted in this context. Valid expressions are constants, constant expression
s, and (in some contexts) variables. Column names are not permitted.
at System.Data.SqlClient.SqlC
n breakConnection)
at System.Data.SqlClient.SqlI
, Boolean breakConnection)
at System.Data.SqlClient.TdsP
ect stateObj)
at System.Data.SqlClient.TdsP
dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
ParserStateObject stateObj)
at System.Data.SqlClient.SqlC
Behavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlC
havior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlC
ior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult
result)
at System.Data.SqlClient.SqlC
ult, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlC
at A05_Flores.A05_Flores.inse
work\112D\A05_Flores\A05_F
at A05_Flores.A05_Flores.Main
5_Flores\A05_Flores\A05_Fl
Press any key to continue . . .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window