CipherIS
asked on
C# Pass a Null value to a Stored Procedure Parameter
I need to pass a null to Parameter guidNumber2. Any ideas?
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(cs);
//Create a DataAdapter, and then provide the name of the stored procedure.
SqlDataAdapter da = new SqlDataAdapter("sproc", con);
//Set the command type as StoredProcedure.
da.SelectCommand.CommandType = CommandType.StoredProcedure;
//Create and add a parameter to Parameters collection for the stored procedure.
da.SelectCommand.Parameters.Add(new SqlParameter("@guidNumber1",
SqlDbType.UniqueIdentifier));
//Assign the search value to the parameter.
Guid gGuid = new Guid(ViewState["guidNumber1Value"].ToString());
da.SelectCommand.Parameters["@Number1"].Value = gGuid;
//Create and add a parameter to Parameters collection for the stored procedure.
da.SelectCommand.Parameters.Add(new SqlParameter("@Parameter2",
SqlDbType.Int, 4));
//Assign the search value to the parameter.
da.SelectCommand.Parameters["@Parameter2"].Value = ViewState["Param2Value"];
//Create and add a parameter to Parameters collection for the stored procedure.
string sGuidNumber2 = ViewState["GuidNumber2Value"].ToString();
da.SelectCommand.Parameters.Add(new SqlParameter("@GuidNumber2",
SqlDbType.UniqueIdentifier));
if (sGuidNumber2.Length == 32 && sGuidNumber2.Contains("-"))
{
//Assign the search value to the parameter.
Guid gGuidNo2= new Guid(ViewState["GuidNumber2Value"].ToString());
da.SelectCommand.Parameters["@guidNumber2"].Value = gGuidNo2;
}
else
{
//Assign the search value to the parameter.
da.SelectCommand.Parameters["@guidNumber2"].Value = null;
}
//Fill the DataSet with the rows that are returned.
da.Fill(ds, "Report");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.