Base types (int, bit, double, etc) - How to allow NULL
Menu
By adding a question mark after the base type it allows it to pass nulls. See example below.
ASPX.CS
int? sessionId = null;
if (ddlSessionFilter.SelectedValue.ToString() != "Show all")
{
sessionId = int.Parse(ddlSessionFilter.SelectedValue.ToString());
}
gvRecipients.DataSource = Recipient.RecipientReport(attending, sessionId);
gvRecipients.DataBind();
APP_Code > FileName.CS
public static DataSet RecipientReport(int attending, int? sessionId)
{
DataSet oDataSet = new DataSet();
using (SqlConnection oSqlConnection = new SqlConnection(ConfigSettings.SqlServerConnectionString()))
{
SqlCommand oSqlCommand = new SqlCommand("CustomerFocusRecipientReport", oSqlConnection);
oSqlCommand.CommandType = CommandType.StoredProcedure;
oSqlCommand.Parameters.Add("@Attending", SqlDbType.Int, 4).Value = attending;
if (sessionId != null)
{
oSqlCommand.Parameters.Add("@SessionId", SqlDbType.Int, 4).Value = sessionId;
}
SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter(oSqlCommand);
oSqlDataAdapter.Fill(oDataSet, "Report");
}
return oDataSet;
}
Copyright 2012. All rights reserved.
