Compare system date with a date field in SQL
Posted
by
JeT_BluE
on Stack Overflow
See other posts from Stack Overflow
or by JeT_BluE
Published on 2012-04-14T23:16:52Z
Indexed on
2012/04/14
23:28 UTC
Read the original article
Hit count: 198
I am trying to compare a date record in SQL Server with the system date. In my example the user first register with his name and date of birth which are then stored in the database. The user than logs into the web application using his name only. After logging in, his name is shown on the side where it says "Welcome "player name
" using Sessions
.
What I am trying to show in addition to his name is a message saying "happy birthday" if his date of birth matches the system date. I have tried working with System.DateTime.Now
, but what I think is that it is also comparing the year, and what I really want is the day and the month only. I would really appreciate any suggestion or help.
CODE In Login page:
protected void Button1_Click(object sender, EventArgs e)
{
String name = TextBox1.Text;
String date = System.DateTime.Today.ToShortDateString();
SqlConnection myconn2 = new
SqlConnection(ConfigurationManager.ConnectionStrings["User"].ToString());
SqlCommand cmd2 = new SqlCommand();
SqlDataReader reader;
myconn2.Open();
cmd2 = new SqlCommand("Select D_O_B from User WHERE Username = @username",
myconn2);
cmd2.Parameters.Add("@username", SqlDbType.NVarChar).Value = name;
cmd2.Connection = myconn2
cmd2.ExecuteNonQuery();
reader = cmd2.ExecuteReader();
while (reader.Read().ToString() == date)
{
Session["Birthday"] = "Happy Birthday";
}
}
Note: I using the same reader in the code above this one, but the reader here is with a different connection. Also, reader.Read()
is different than reader.HasRows
?
Code in Web app Page:
string date = (string)(Session["Birthday"]); // Retrieving the session
Label6.Text = date;
© Stack Overflow or respective owner