Link to home
Start Free TrialLog in
Avatar of mscprojhk
mscprojhk

asked on

How to convert long to bigint to access stored procedure

I need to pass a long value from C# to a sql stored procedure, but got error: "SqlParameterCollection accepts non-null sqlParameter type objects, not Int64)

Following is my c# coding

long lngJobID = JobID;      
 
SqlParameter paramJobID = new SqlParameter("@JobID", SqlDbType.BigInt, 8, ParameterDirection.Input, false, 0, 0, "JobID", DataRowVersion.Current, lngJobID);

I can run the sp at Query Analyzer by the following

Declare @JobID as bigint

set @JobID = 20031114000001
EXECUTE GetJobItems @JobID
Avatar of testn
testn

You can simply change it to

object lngJobID = JobID;      
 
SqlParameter paramJobID = new SqlParameter("@JobID", SqlDbType.BigInt, 8, ParameterDirection.Input, false, 0, 0, "JobID", DataRowVersion.Current, lngJobID);
Avatar of mscprojhk

ASKER

I found out the bug:

sqlCommand.Parameters.Add(paramJobID);

Should add the object, not the value!
Actually, you can do something as simple as this:

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE table1 SET row1=@JobID WHERE ID=@ID";
long JobID;
long ID;
cmd.Parameters.Add("@JobID",JobID);
cmd.Parameters.Add("@ID",ID);
cmd.Connection = /*connection object*/;
cmd.ExecuteNonQuery();

/* Using this way, you don't even need to specify SqlDbType.BigInt as the Parameters object will automatically match the type of JobID with the column in the MS SQL Table */

I hope my comments help
ASKER CERTIFIED SOLUTION
Avatar of PashaMod
PashaMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial