C# .NET - The SqlParameterCollection only accepts non-null
Asked By Jim Tanner
13-Nov-04 12:08 AM
Hi,
I'm the guy having problems adding parameters to my SP.
I tried out:
command.Parameters.Add(new SqlParameter("@role", SqlDbType.Int).Value= 0);
and get error:
The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.
Tried to google it and couldn't find anything. This one doesn't make sense.
Doesn't make sense to me either
SqlDbType is an enumeration, and ".Int" is one of them. I would try breaking this up - creat the parameter, type and direction , then assign the .Value=0 later.
Only other thing I can think of is you may be calling the wrong execute method of your Command object - ExecuteNonQuery instead of ExecuteScalar, for instance. Stuff like this has been known to generate unusual exception messages.
PS - I have gotten this error
this is an approach that takes care of it:
SqlParameter parm1 = new SqlParameter("@Beginning_Date",SqlDbType.DateTime );
parm1.Value = myDateTimeVariable;
command.Parameters.add(parm1);

An SqlParameter with ParameterName '@User_Id' is not contained by this hi, I am a beginner in C layer like this public int ValidateAdminUser(string User_Name, string User_Password) { int Admin_Id ; int User_Id = 0 ; SqlParameter[] sqlParam = new SqlParameter[]{ new SqlParameter("@User_Name", User_Name), new SqlParameter("@User_Password", User_Password), new SqlParameter("@User_Id", User_Id) }; myCommand.SqlComm.Parameters["@User_Id"].Direction = ParameterDirection.Output; myCommand.ExecuteSP("ValidateAdminUser", sqlParam); Admin_Id = (int ExceuteSP function is from DAL and is written like this : public void ExecuteSP(string strStoredProc, SqlParameter[] sqlParams) { SqlComm = new SqlCommand(strStoredProc, SqlConn); SqlComm.CommandType = CommandType.StoredProcedure; / / SqlConn.Open(); foreach (SqlParameter sqlParam in sqlParams) { SqlComm.Parameters.Add(sqlParam); } SqlComm.ExecuteNonQuery(); } however when I run this thing
SqlParameter is already contained by another SqlParameterCollection The SqlParameter is already contained by another SqlParameterCollection we are getting error at the following line marked in red. try { dsresult = new DataSet SqlCommand ( "spICMS2JDEDetail_RWY" , myConnection); myCommand.CommandType = CommandType .StoredProcedure; myCommand.CommandTimeout = 500; myParameter = myCommand.Parameters.Add( "@pStartDate" , SqlDbType .VarChar); myParameter.Value = StartDate; myParameter = myCommand.Parameters.Add( "@pLedgerDate" , SqlDbType .VarChar); myParameter.Value = LedgerDate; myParameter = myCommand.Parameters.Add( "@pCardType" , SqlDbType .VarChar); myParameter.Value = CardType; myParameter = myCommand.Parameters.Add( "@ret" , SqlDbType .Int); myParameter.Direction = ParameterDirection .Output; myCommand.Parameters.Add(myParameter); / / status = myCommand.ExecuteNonQuery(); myDataAdapter = new SqlDataAdapter
cmdSNItems.Parameters.Add("@shipDate", txtShipDate.Text); / / new parameter cmdSNItems.Parameters.Add("@cdSerialNumID "); dbConn.Open(); cmdSNItems.ExecuteNonquery(); dbConn.Close(); @@identity returns value of last inserted identity column in the current session. Once getting the Auto generated ID from one is where I'm havind a problem. The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects. Source Error: Line 109: cmdSNItems.Parameters.Add("@orderDate", txtOrderDate.Text need to specify the datatype and the direction of the parameter cmdSNItems.Parameters.Add("@cdSerialNumID ", SqlDbType.Int) cmdSNItems.Parameters["@cdSerialNumID"].Direction = ParameterDirection.Output; and after executing the stored procedure usingNonQuery get
The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlDateTime objects. Hi I'm getting "The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlDateTime objects. " do you know why I get this message? = = = = = = = = = = = = = = = = = = = = = = Dim getDate As SqlDateTime getDate = SqlDateTime.Null . . . . . Dim parameterEncDate As New SqlParameter( "@EncDate" , SqlDbType.VarChar, 100) parameterEncDate.Value = Request( "EncDate" ) If parameterEncDate.Value = "" Then myCommand.Parameters.Add(getDate) Else myCommand.Parameters.Add(parameterEncDate) End If myConnection.Open() myCommand.ExecuteNonQuery() myConnection.Close() = = = = = = = = = = = = = = = = Thank you Hi, try this instead of this two lines, Dim getDate As SqlDateTime getDate = SqlDateTime.Null use this Dim getDate As SqlParameter = New SqlParameter( "parameter name" , SqlDbType.DateTime, 8) x.Value = System.Data.SqlTypes.SqlDateTime.Null Dim
String[8]: the Size property has an invalid size of 0. at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) at System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters) at System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at postview_user.Page_Load(Object sender, EventArgs e) in D: \ sites \ 160by2_PublicSMS \ postview_user.aspx.vb:line sqlcmd = New SqlCommand() With sqlcmd .CommandText = "SendGroupSMS" .CommandType = CommandType.StoredProcedure .Connection = connection .Parameters.Add("@userid", SqlDbType.BigInt) .Parameters("@userid").Direction = ParameterDirection.Input .Parameters("@userid").Value = userid Dim i As String For Each i In mno 'Response.Write(i) 'Response.End() .Parameters.Add("@mobile_numbers", SqlDbType.VarChar) .Parameters("@mobile_numbers").Direction = ParameterDirection.Input .Parameters("@mobile_numbers").Value = i Next i .Parameters.Add("@userip SqlDbType.VarChar, 30) .Parameters("@userip").Direction = ParameterDirection.Input .Parameters("@userip").Value = "" .Parameters.Add("@message", SqlDbType.VarChar, 160) .Parameters("@message").Direction = ParameterDirection.Input .Parameters("@message").Value = message .Parameters.Add("@source_id", SqlDbType