Reputation: 114
I have a table that stores records of customers, I would like to filter through these customers but I am having trouble placing a null value into the sproc. The values can be null sometimes and I tested it in sql and it works but It will not accept null value from c# behind. I tried with if statements and the results are the same.
Any help appreciated
you can view the code below
SQL
WHERE ( ProcessStatus.ProcessStatusKey = @ProcessStatusKey OR @ProcessStatusKey IS NULL )
AND
( PriorityLevels.PriorityLevelKey = @PriorityLevelKey OR @PriorityLevelKey IS NULL )
AND
( SystemUsers.SystemUserKey = @SystemUserKey OR @SystemUserKey IS NULL )
AND
( CaseHoganData.LoanAccountNumber = @AccountNumber OR @AccountNumber IS NULL )
AND
( ( LTRIM(RTRIM(( ISNULL(Customers.FirstName, '') + ' ' + ISNULL(Customers.LastName, '') ))) ) LIKE '%'
+ @CustomerName + '%'OR @CustomerName IS NULL )
ASP.NET
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("sp_Cases_ViewAll", con))
{
//if (prioritylev == "")
// cmd.Parameters.AddWithValue("@PriorityLevelKey", DBNull.Value);
//else
cmd.Parameters.AddWithValue("@PriorityLevelKey", (string.IsNullOrEmpty(prioritylev) ? (Guid?)null : new Guid(prioritylev)));
cmd.Parameters.AddWithValue("@ProcessStatusKey", (string.IsNullOrEmpty(processkey) ? (Guid?)null : new Guid(processkey)));
cmd.Parameters.AddWithValue("@SystemUserKey", (string.IsNullOrEmpty(systemuser) ? (Guid?)null : new Guid(systemuser)));
cmd.Parameters.AddWithValue("@AccountNumber", accnumber);
cmd.Parameters.AddWithValue("@CustomerName", cusname);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
DataTable dt = new DataTable();
sda.Fill(dt);
grid_Cases.DataSource = dt;
grid_Cases.DataBind();
}
}
}
EDIT This worked for me
cmd.Parameters.AddWithValue("@PriorityLevelKey", (string.IsNullOrEmpty(prioritylev) ? (object)DBNull.Value : new Guid(prioritylev)));
Upvotes: 2
Views: 122
Reputation: 8462
You were on the right path on your commented code "DBNull.value", here is your changed code:
using (SqlCommand cmd = new SqlCommand("sp_Cases_ViewAll", con))
{
//if (prioritylev == "")
// cmd.Parameters.AddWithValue("@PriorityLevelKey", DBNull.Value);
//else
if (string.IsNullOrEmpty(prioritylev))
{
cmd.Parameters.AddWithValue("@PriorityLevelKey", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@PriorityLevelKey", new Guid(prioritylev));
}
if(string.IsNullOrEmpty(processkey))
{
cmd.Parameters.AddWithValue("@ProcessStatusKey", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@ProcessStatusKey", new Guid(processkey));
}
if (string.IsNullOrEmpty(systemuser))
{
cmd.Parameters.AddWithValue("@SystemUserKey", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@SystemUserKey", new Guid(systemuser));
}
cmd.Parameters.AddWithValue("@AccountNumber", accnumber);
cmd.Parameters.AddWithValue("@CustomerName", cusname);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
DataTable dt = new DataTable();
sda.Fill(dt);
grid_Cases.DataSource = dt;
grid_Cases.DataBind();
}
}
}
Upvotes: 2
Reputation: 7783
NULL is represented by the DBNull
class:
https://msdn.microsoft.com/en-us/library/system.dbnull(v=vs.110).aspx
Use DBNull.Value
when no value is present.
Upvotes: 2