Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Error when using ExecuteNonQuery
Message
From
09/04/2004 19:34:23
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00893753
Message ID:
00893759
Views:
16
Kirk,

I don't have anything specific, but have you tried looking at lcCmd in the debugger before you run the .ExecuteNonQuery()just to make sure it looks ok? Also, I don't know if you're aware of how slow it is to create strings that way. Either use a StringBuilder or do it all in one command (instead of continually adding to lcCmd). Of course, it's only a big problem if you're doing this a large number of times in a loop, but it's probably a good idea to at least think about other ways of creating strings.

Now, that said, I don't think you should create the command string that way at all ... it would probably be better to use Parameters.

~~Bonnie


>Hoping someone will point out the errors of my way. I'm trying to run an update command againt the database, and when I hit the cmdUpdate.ExecuteNonQuery() function, I get the following error message:
>
>
>Exception has been thrown by tart of an invocation.
>
>
>I'm not sure what that means...other than I screwed up. Basicly I'm building a string that has the update command, assigning it to a sqlCommand and trying to execute it. Below is the code, thanks everyone.
>
>
>     ' Create our connection
>     Dim oConn As New SqlConnection(ConnString)
>     ' Build our update sql string
>     Dim lcCmd As String = " "
>     lcCmd = "Update patients set "
>     lcCmd = lcCmd + "lastname = " & fld_LastName & ", "
>     lcCmd = lcCmd + "firstname = " & fld_FirstName & ", "
>     lcCmd = lcCmd + "ssn= " & fld_SSN & ", "
>     lcCmd = lcCmd + "sex = " & fld_Sex & ", "
>     lcCmd = lcCmd + "dob = " & fld_DOb & ", "
>     lcCmd = lcCmd + "homenumber = " & fld_Home & ", "
>     lcCmd = lcCmd + "worknumber = " & fld_Work & ", "
>     lcCmd = lcCmd + "address = " & fld_Addr & ", "
>     lcCmd = lcCmd + "city = " & fld_City & ", "
>     lcCmd = lcCmd + "state = " & fld_State & ", "
>     lcCmd = lcCmd + "zipcode = " & fld_Zip & ", "
>     lcCmd = lcCmd + "where patientid = " & iPatientID
>     ' Create the Update CommNad Object
>     Dim cmdUpdate = oConn.CreateCommand
>     ' Set the Command Type and Text
>     cmdUpdate.CommandType = CommandType.Text
>     cmdUpdate.CommandText = lcCmd
>     ' Try the update
>     Try
>        cmdUpdate.ExecuteNonQuery()
>     Catch ex As Exception
>       MsgBox(ex.Message)
>       'Return exception
>       ' Exception has been thrown by target of invocation.
>     End Try
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform