string strqry = "select dob from master_studentpersonal where stud_id= '" +
TextBox1.Text + "'";
DataTable dt = mc.selectQryDataTable2(strqry);
DataView dv = new DataView(dt);
DateTime dob = new DateTime();
if (dob != null)
{
if (dob <= new DateTime(2004, 01, 01) &&
dob <= new DateTime(2005, 12, 31))
{
TextBox6.Text = "Group A";
}
else if (dob <= new DateTime(2003, 12, 31) &&am开发者_如何学编程p;
dob <= new DateTime(2002, 01, 01))
{
TextBox6.Text = "Group B";
}
}
plz help me friends
You haven't assigned the value from your SQL query to your dob variable. dob is only ever set to the value that comes with an empty constructor. Also, as Chris F said, check the conditions for the two if statements: currently if the date is less than or equal to 31/12/2005 the group will always be A.
dob
will never be null, both because you assign it and because DateTime
is a value type. Doesn't fix your problem, but it'll tidy your code ;-)
More importantly though, you never assign a value to it, so it's going to pick up an arbitrary value like DateTime.MinValue
(at a guess).
In addition to Dan Iveson's answer - your output will always be "Group A" because 2003-12-31 <= 2004-01-01.
I guess you meant:
string strqry = "select dob from master_studentpersonal where stud_id= '" +
TextBox1.Text + "'";
DataTable dt = mc.selectQryDataTable2(strqry);
DataView dv = new DataView(dt);
DateTime dob = // get date of birth from dt
if (dob != null)
{
if (dob <= new DateTime(2003, 12, 31) &&
dob >= new DateTime(2002, 01, 01))
{
TextBox6.Text = "Group B";
}
else if (dob >= new DateTime(2004, 01, 01) &&
dob <= new DateTime(2005, 12, 31))
{
TextBox6.Text = "Group A";
}
}
Your TextBox will say "Group A" everytime:
DateTime dob = new DateTime();
if (dob != null) // dob can't be null unless you haven't instantiated it,
// it's a value type. Actually it's
// value here is always DateTime.MinValue,
// since you aren't assigning anything to it.
{
if (dob <= new DateTime(2004, 01, 01) &&
dob <= new DateTime(2005, 12, 31)) // this second check is redundant.
// if this condition is ever evaluated,
// it will always be true. I assume you have
// mixed up your < and > signs here
{
TextBox6.Text = "Group A";
}
else if (dob <= new DateTime(2003, 12, 31) && // as above, this statement can never
// evaluate to true. you won't even
// reach this code unless dob > 2004
dob <= new DateTime(2002, 01, 01))
{
TextBox6.Text = "Group B";
}
}
精彩评论