SQL Connection on the fly???

G

Guest

Can someone tell me what is wrong with the sub below???

I'm try to create a bunch of SQL connections on the fly...

I'm sure there is a better way to do this but was having problem with the
sql statement in the Stored Procedure so I decided to try this way...

If anyone can give me a hand with this I would really appritiate it!

Thanks
.....
Exception Details: System.InvalidOperationException: ExecuteNonQuery:
Connection property has not been initialized.

Source Error:
Line 94: cmd.ExecuteNonQuery()

=============CODE===========

For i As Integer = 1 To 10
Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand
Dim varSQL As String
Dim varMenuValue As String
Dim varPriceValue As String
varSQL = "varSQL" & i.ToString()
varMenuValue = "varMenuValue" & i.ToString()
varPriceValue = "varPriceValue" & i.ToString()
varMenuValue = CType(Page.FindControl("tbMenuItem" &
i.ToString()), TextBox).Text
varPriceValue = CType(Page.FindControl("tbMenuItem" &
i.ToString()), TextBox).Text
varSQL = "UPDATE tblRestaurant SET menuitem" & i.ToString() &
"=" & varMenuValue & ", priceitem" & i.ToString() & "=" & varPriceValue & "
WHERE ItemID =" & i.ToString
cmd.CommandText = varSQL
cmd.CommandType = CommandType.Text
MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
Next
 
P

Patrice

I don't see where you tell which connexion "cmd" should use...

Use the appropriate constructor or the connection property to define the
connection for the "cmd" object...
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top