This has been stumping me for a few days now.
I have a stored procedure on a MS SQL 2008 server. I can connect through management studio and as the user I am using in my code and execute the stored procedure and it works how I expect it to. The problem comes in when I try to use it in my code. Here is my code:
Dim strConn1 As String = "Data Source=server;Initial Catalog=db;User ID=user;Password=pass"
Dim objconn1 As New SqlConnection(strConn1)
Dim objCommand1 As New SqlCommand("storedProcedure", objconn1)
objCommand1.CommandType = Data.CommandType.StoredProcedure
objCommand1.Parameters.AddWithValue("@strEncounter", Encounter)
objCommand1.Parameters.AddWithValue("@NetworkName", Employee)
objconn1.Open()
Dim test = objCommand1.ExecuteNonQuery()
objconn1.Close()
When I run this it does nothing. There is no exception, no error, and the variable test which stores the number of rows affected from the method ExecuteNonQuery is 1. When I check the database to see the row it should have inserted there is nothing.
The other really strange thing is that I have the same code in an ASP.NET website and it works fine. I have copied it and pasted it exactly the same into this application which is a forms application and it will not work. Thanks.
Well, since you are using
ExecuteNonQuery, “nothing” should happen in the application.ExecuteNonQuerysays that an action is going to be performed on the SQL Server, and do not expect a returned result set.Now, if you are expecting a result set, you have to use a DataAdapter and .Fill() a DataTable object with the results:
You’d only want to use ExecuteNonQuery when you are inserting, updating, or deleting rows. When you want to read data to the client app, you have to store it somewhere. As a side note, this is an argument against copy-n-paste coding 😉 Also, it’s arguably out of fashion to use bastardized Hungarian Notation (i.e. the “obj” prefix to your variables). It much more readable like this:
Everything prefixed with “obj” makes it harder for the eye to pick out the variables.