我如何使用c#显示两个状态的数据
本文关键字:两个 状态 数据 何使用 显示 | 更新日期: 2023-09-27 18:12:34
我有一个表单,在其中我有用户控制(用于显示成员名与日期时间)和两个组合框(一个是cbstatus值,如(拒绝,接受,注销)和另一个组合框cbperiod和值,如(今天,7天,30天…)我正在显示成员名以及访问日期时间和注销日期时间,就像这样…)
使用以下查询
sql = @"SELECT member_Firstname, member_Lastname, member_Postcode,
visit_DateTime, visit_Status, visit_Logout_DateTime, visits.member_Id, visit_AlertMsg
FROM members,visits
WHERE members.member_Id = visits.member_Id
AND members.member_Active LIKE 'y%'";
this is working fine .....
我得到的值取决于组合框的值,像这样…
if (cbStatus.Text == "Accepts")
{
sql += " AND visits.visit_Status = 'accepted' ";
}
我有两个以上的条件…
if (cbStatus.Text == "refusals")
{
sql += blahh blahh blahhhh
}
我得到的值为选择cbperiod组合框像这样…
if (cbPeriod.Text == "Today")
{
string dtStartString = DateTime.Today.ToString(DataHelper.dateFormat);
sql += string.Format(" AND visits.visit_Date = '{0}'", dtStartString);
}
//here i am comparing the cbstatus value logout and cbperiod text with today to get the member details whose logout datetime is today
if (cbStatus.Text == "Logout" && cbPeriod.Text == "Today")
{
string dtStartString = DateTime.Today.ToString("yyyy-MM-dd");
sql += string.Format(" AND DATE(visits.visit_Logout_DateTime) = '{0}'", dtStartString);
}
,我有两个条件像这样....对于cbstatus和cbperiod
中的值这里我用usercontrol绑定了我的值....
datatable dt1 = Helper.GetData(sql);
if (dt1 != null)
{
if (dt1.Rows.Count > 0)
{
foreach (DataRow row in dt1.Rows)
{
newItem = new EntryItem();// this is my usercontrol
if (cbStatus.Text != "Logout")
{
DateTime dtTemp = DateTime.Parse(row["visit_DateTime"].ToString()); // here I am showing the data when user not selected the cbstatus text as logout I mean (refusal,..)
but the below function overrides this data ....
if (cbPeriod.Text == "Today") newItem.lblTime.Text = dtTemp.ToString("HH':'mm':'ss");
else newItem.lblTime.Text = dtTemp.ToString("yyyy'-'MM'-'dd' - 'HH':'mm':'ss");
}
if (row["visit_Logout_DateTime"] != DBNull.Value)
{
DateTime dtlogout = DateTime.Parse(row["visit_Logout_DateTime"].ToString());
if (dtlogout != null)
{
if (cbStatus.Text == "Logout" && cbPeriod.Text == "Today")
{
newItem.lblTime.Text = dtlogout.ToString("HH':'mm':'ss");
newItem.lblName.Text = row["member_Firstname"].ToString() + " " + row["member_Lastname"].ToString();
newItem.lblAlertMessage.Text = row["visit_AlertMsg"].ToString();
}
else
newItem.lblTime.Text = dtlogout.ToString("yyyy'-'MM'-'dd' - 'HH':'mm':'ss");
newItem.lblName.Text = row["member_Firstname"].ToString() + " " + row["member_Lastname"].ToString();
newItem.lblAlertMessage.Text = row["visit_AlertMsg"].ToString();
}
}
newItem.lblName.Text = row["member_Firstname"].ToString() + " " + row["member_Lastname"].ToString();
newItem.lblAlertMessage.Text = row["visit_AlertMsg"].ToString();
当我选择cbstatus.text == logout
时,它将显示正确的数据,成员注销日期时间…
,但当我选择cbstatus.text == refusals
…它正在显示注销数据(具有logoutdatatetime的成员)。这是错误的,实际上它必须显示refusal members
和visit_Datetime
如何显示具有logoutdatetime和visit datetime的成员的数据
注意:一个成员同时具有logoutdatetime和visitdatetime....
我的数据是这样的…
firstname lastname postcode status visit_Logout_DateTime visit_datetime
------------- -------- --------- ------- --------------------- ----------------
rob peter hhd344h refused 2011-05-06 12:09:07 2011-05-06 08:09:34
peter chan hy78kjk refused 2011-09-08 12:09:08 2011-05-03 06:09:34
rock sam yudufg3746h refused 2011-08-08 09:08:45
rob peter hhd344h refused 2011-05-10 12:09:07 2011-05-10 08:09:34
在sql中使用case语句请看下面的例子
sql = @"SELECT member_Firstname, member_Lastname, member_Postcode, visit_DateTime, visit_Status, visit_Logout_DateTime, visits.member_Id, visit_AlertMsg , CustomDateColumn = CASE visit_Status
WHEN 'refused' THEN visit_Datetime
WHEN 'Logout' THEN visit_Logout_DateTime
WHEN ....
End
FROM members,visits WHERE members.member_Id = visits.member_Id AND members.member_Active LIKE 'y%'";
我想你可能在else语句周围缺少一些括号,看起来像这样:
else
newItem.lblTime.Text = dtlogout.ToString("yyyy'-'MM'-'dd' - 'HH':'mm':'ss");
newItem.lblName.Text = row["member_Firstname"].ToString() + " " + row["member_Lastname"].ToString();
newItem.lblAlertMessage.Text = row["visit_AlertMsg"].ToString();
而且,似乎这个块的第一行可能是您的问题的答案。您正在将标签更改为"注销"时间。也许你真的想使用"拜访"时间?
我已经解决了我的问题…像这样…
if (row["visit_Logout_DateTime"] != DBNull.Value || row["visit_DateTime"] == DBNull.Value)
{
if (cbStatus.Text != "Refusals" && cbStatus.Text != "Accepts" && row["visit_Logout_DateTime"] != DBNull.Value)
{
DateTime dtlogout = DateTime.Parse(row["visit_Logout_DateTime"].ToString());
if (true)
{
if (cbPeriod.Text == "Today")
{
newItem.lblTime.Text = dtlogout.ToString("HH:mm:ss");
newItem.BackColor = Color.Cyan;
}
else
newItem.lblTime.Text = dtlogout.ToString("yyyy'-'MM'-'dd' - 'HH':'mm':'ss");
newItem.lblName.Text = string.Format("{0} {1}", row["member_Firstname"].ToString(), row["member_Lastname"].ToString());
newItem.lblAlertMessage.Text = string.Format("{0} {1}", row["member_EntryMessage"].ToString(), row["visit_AlertMsg"].ToString());
newItem.lblName.Text = string.Format("{0} {1}", row["member_Firstname"].ToString(), row["member_Lastname"].ToString());
newItem.lblAlertMessage.Text = string.Format("{0} {1}", row["member_EntryMessage"].ToString(), row["visit_AlertMsg"].ToString());
newItem.tbHiddenId.Text = row["member_Id"].ToString();
newItem.ID = Convert.ToInt32(row["member_Id"].ToString());
}
}
}
else if (row["visit_Logout_DateTime"] == DBNull.Value || row["visit_DateTime"] != DBNull.Value)
{
DateTime dtTemp = DateTime.Parse(row["visit_DateTime"].ToString());
newItem.lblTime.Text = dtTemp.ToString(cbPeriod.Text == "Today" ? "HH':'mm':'ss" : "yyyy'-'MM'-'dd' - 'HH':'mm':'ss");
newItem.lblName.Text = string.Format("{0} {1}", row["member_Firstname"].ToString(), row["member_Lastname"].ToString());
newItem.lblAlertMessage.Text = string.Format("{0} {1}", row["member_EntryMessage"].ToString(), row["visit_AlertMsg"].ToString());
newItem.tbHiddenId.Text = row["member_Id"].ToString();
newItem.ID = Convert.ToInt32(row["member_Id"].ToString());
}