SqlCommand fires too soon.....

  • Thread starter Thread starter Jeppe Dige Jespersen
  • Start date Start date
J

Jeppe Dige Jespersen

I have the following:

___________________________________

Dim cmdSaveUser As New SqlCommand
cmdSaveUser.Connection = con 'con has been configured previously

cmdSaveUser.CommandType = CommandType.StoredProcedure
cmdSaveUser.CommandText = "spInsertUser"
cmdSaveUser.Parameters.Add("@FirstName", "Joe")
cmdSaveUser.Parameters.Add("@Navn", "Brown")
cmdSaveUser.Connection.Open
......

___________________________________

What happens is this: As soon as i .open the connection, it seems that the
command is fired. I don't want this to happen, as I want to call
executeScalar and get an object in return.

Any ideas?

Jeppe Jespersen
 
Hi,

Open the connection before you set cmdSaveUser.Connection
to the specific connection before you set CommandText and
you will be home free..
:
Dim cmdSaveUser As New SqlCommand
'New code!
con.Open()
'End of new code!
cmdSaveUser.Connection = con 'con has been configured
previously

cmdSaveUser.CommandType = CommandType.StoredProcedure
cmdSaveUser.CommandText = "spInsertUser"
cmdSaveUser.Parameters.Add("@FirstName", "Joe")
cmdSaveUser.Parameters.Add("@Navn", "Brown")
 
Hi again,

Thanks for reply, but it doesn't help any.
Any other ideas?

Jeppe
 
Hi,

I have tried to run your code and I do not see any problems. I am using
VS2003. How do you check that it tries to execute command when you open
connection?
 
Hi Val,
I have tried to run your code and I do not see any problems. I am using
VS2003. How do you check that it tries to execute command when you open
connection?

The only way I can tell, is that a new record is inserted before i execute
the command myself. But I changed the code alltogether, so now I use a
dataset+adapter and update through that. And that seems to work just fine
with a minumum of extra overhead.

I too have tried running the posted code in another project, and all seems
fine. But oh well....

But thanks for replying.

Jeppe
 
Back
Top