Return DropDownList 为 NULL 时的所有数据
Return ALL data when DropDownList is NULL
我正在尝试修复我犯的一些错误和错误,但我没有及时注意。
我有两个下拉菜单(Status
是 bit
)和(OrgUnitId int 0-9
)。
所以问题是当用户 select 在这两个下拉列表中只有一个记录,而另一个下拉列表不是 select 默认情况下 return 0
。
但在我的示例中,我想修改它并创建如果用户 select 仅 Status = 1
它应该 return 记录状态为 1 的位置和 OrgUnit 它应该 return 所有数据未经过滤。
到目前为止,我为从数据库中捕获 Status
和 OrgUnitId
编写了这些代码,并在 DropDownMenu
中显示
public void FillOrgUnit()
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
{
string com = "SELECT DISTINCT OrgUnitID FROM tblZaposleni_AD ORDER BY OrgUnitID ASC";
SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
DataTable dt = new DataTable();
adpt.Fill(dt);
ddlOrgUnit.DataSource = dt;
ddlOrgUnit.DataTextField = "OrgUnitID";
ddlOrgUnit.DataValueField = "OrgUnitID";
ddlOrgUnit.DataBind();
ddlOrgUnit.Items.Insert(0, new ListItem("-- Izaberi Org Jedinicu --", "NULL"));
}
}
public void FillStatus()
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
{
string com = "SELECT DISTINCT Status FROM tblZaposleni_AD";
SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
DataTable dt = new DataTable();
adpt.Fill(dt);
ddlStatus.DataSource = dt;
ddlStatus.DataTextField = "Status";
ddlStatus.DataValueField = "Status";
ddlStatus.DataBind();
ddlStatus.Items.Insert(0, new ListItem("-- Izaberi Status --", "NULL"));
}
}
这是我过滤数据的主要功能
private void GetReports(XtraReport report)
{
try
{
string connString = @"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
string strproc = "TestReport";
using (SqlDataAdapter sda = new SqlDataAdapter(strproc, connString))
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = ddlStatus.SelectedValue == "1" ? true : false;
sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = ddlOrgUnit.SelectedValue;
sda.Fill(ds);
string[] arrvalues = new string[ds.Tables[0].Rows.Count];
for (int loopcounter = 0; loopcounter < ds.Tables[0].Rows.Count; loopcounter++)
{
//assign dataset values to array
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["PrezimeIme"].ToString();
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["NetworkLogin"].ToString();
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["Status"].ToString();
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["OrgUnitID"].ToString();
}
report.DataSource = ds;
report.DataMember = ds.Tables[0].TableName.ToString();
}
}
catch (Exception)
{
throw;
}
}
为了更好地理解我想要什么以及问题出在哪里,我编写了这个存储过程:
DECLARE @Status bit = 1,
@OrgJed int
BEGIN
SELECT PrezimeIme, NetworkLogin, Status, OrgUnitId, DT_Creat, DT_Modif
FROM [DesignSaoOsig1].[dbo].[tblZaposleni_AD]
WHERE (@Status IS NULL OR Status = @Status)
AND (@OrgJed IS NULL OR OrgUnitID = @OrgJed)
END
下面是这个存储过程的结果:
这是我在视图中得到的结果
It looks to me like when the ALL option is selected, that you are setting the parameter values to the string "NULL", rather than DBNull.Value.
尝试替换这个:
sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = ddlStatus.SelectedValue == "1" ? true : false;
sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = ddlOrgUnit.SelectedValue;
有了这个:
if (ddlStatus.SelectedValue == "NULL")
sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = DBNull.Value;
else
sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = ddlStatus.SelectedValue == "1" ? true : false;
if (ddlOrgUnit.SelectedValue == "NULL")
sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = DBNull.Value;
else
sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = ddlOrgUnit.SelectedValue;
我正在尝试修复我犯的一些错误和错误,但我没有及时注意。
我有两个下拉菜单(Status
是 bit
)和(OrgUnitId int 0-9
)。
所以问题是当用户 select 在这两个下拉列表中只有一个记录,而另一个下拉列表不是 select 默认情况下 return 0
。
但在我的示例中,我想修改它并创建如果用户 select 仅 Status = 1
它应该 return 记录状态为 1 的位置和 OrgUnit 它应该 return 所有数据未经过滤。
到目前为止,我为从数据库中捕获 Status
和 OrgUnitId
编写了这些代码,并在 DropDownMenu
public void FillOrgUnit()
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
{
string com = "SELECT DISTINCT OrgUnitID FROM tblZaposleni_AD ORDER BY OrgUnitID ASC";
SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
DataTable dt = new DataTable();
adpt.Fill(dt);
ddlOrgUnit.DataSource = dt;
ddlOrgUnit.DataTextField = "OrgUnitID";
ddlOrgUnit.DataValueField = "OrgUnitID";
ddlOrgUnit.DataBind();
ddlOrgUnit.Items.Insert(0, new ListItem("-- Izaberi Org Jedinicu --", "NULL"));
}
}
public void FillStatus()
{
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
{
string com = "SELECT DISTINCT Status FROM tblZaposleni_AD";
SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
DataTable dt = new DataTable();
adpt.Fill(dt);
ddlStatus.DataSource = dt;
ddlStatus.DataTextField = "Status";
ddlStatus.DataValueField = "Status";
ddlStatus.DataBind();
ddlStatus.Items.Insert(0, new ListItem("-- Izaberi Status --", "NULL"));
}
}
这是我过滤数据的主要功能
private void GetReports(XtraReport report)
{
try
{
string connString = @"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
string strproc = "TestReport";
using (SqlDataAdapter sda = new SqlDataAdapter(strproc, connString))
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = ddlStatus.SelectedValue == "1" ? true : false;
sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = ddlOrgUnit.SelectedValue;
sda.Fill(ds);
string[] arrvalues = new string[ds.Tables[0].Rows.Count];
for (int loopcounter = 0; loopcounter < ds.Tables[0].Rows.Count; loopcounter++)
{
//assign dataset values to array
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["PrezimeIme"].ToString();
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["NetworkLogin"].ToString();
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["Status"].ToString();
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["OrgUnitID"].ToString();
}
report.DataSource = ds;
report.DataMember = ds.Tables[0].TableName.ToString();
}
}
catch (Exception)
{
throw;
}
}
为了更好地理解我想要什么以及问题出在哪里,我编写了这个存储过程:
DECLARE @Status bit = 1,
@OrgJed int
BEGIN
SELECT PrezimeIme, NetworkLogin, Status, OrgUnitId, DT_Creat, DT_Modif
FROM [DesignSaoOsig1].[dbo].[tblZaposleni_AD]
WHERE (@Status IS NULL OR Status = @Status)
AND (@OrgJed IS NULL OR OrgUnitID = @OrgJed)
END
下面是这个存储过程的结果:
这是我在视图中得到的结果
It looks to me like when the ALL option is selected, that you are setting the parameter values to the string "NULL", rather than DBNull.Value.
尝试替换这个:
sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = ddlStatus.SelectedValue == "1" ? true : false;
sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = ddlOrgUnit.SelectedValue;
有了这个:
if (ddlStatus.SelectedValue == "NULL")
sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = DBNull.Value;
else
sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = ddlStatus.SelectedValue == "1" ? true : false;
if (ddlOrgUnit.SelectedValue == "NULL")
sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = DBNull.Value;
else
sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = ddlOrgUnit.SelectedValue;