Can execute a query with multiple parameters

M

mrmagoo

Dim cn As SqlConnection = New SqlConnection("<ConnStr>")
cn.Open()
Dim cmd As SqlCommand = New SqlCommand("scsp_Stored_Proc", cn)
cmd.CommandType = CommandType.StoredProcedure
Dim pc As SqlParameterCollection = cmd.Parameters

pc.Add("@Param1", Param1Val)
pc.Add("@Param2", Param2Val)
If Param3Val <> 0 Then
pc.Add("@Param3", Param3Val)
Else
pc.Add("@Param4", Param4Val)
End If
cmd.ExecuteNonQuery()

what am I doing wrong?

It's erroring out on the last line:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred
in system.data.dll

Additional information: System error.
 
M

mrmagoo

I got it. It was expecting nulls. I used System.DBNull.Value for the other
params in the If..then condition.
 
P

parez

I think you should have looked at the inner exception.. that would
have told you the exact problem.
 

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

Top