Can anyone help me take a look at my code?
-
Hi.. I would like to ask about C# web application.I have created a calendar. I would like to ask that if i click the date on the calendar,how can i retrieve the database ?? I have tried butit seem like is not working. Below is the code, could anyone help me with it as i'm totally lost?? Thanks!! The code: private void Radcalendar2_SelectionChanged(object sender, System.EventArgs e) { TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); } I just want to retrieve(view) the user's login/logout time of date we have click on the calendar. Anyone who can help me..You can also edit from here or a new code... Thanks!! :-D Cheers!
-
Hi.. I would like to ask about C# web application.I have created a calendar. I would like to ask that if i click the date on the calendar,how can i retrieve the database ?? I have tried butit seem like is not working. Below is the code, could anyone help me with it as i'm totally lost?? Thanks!! The code: private void Radcalendar2_SelectionChanged(object sender, System.EventArgs e) { TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); } I just want to retrieve(view) the user's login/logout time of date we have click on the calendar. Anyone who can help me..You can also edit from here or a new code... Thanks!! :-D Cheers!
I'm sorry to say this, but your question is not very clear. Is English not your native language? Here's what I *think* the problem is: If you have a DateTime value, do NOT use the ToLongDateString() method as it formats the date depending on the user's Windows settings. Rather, use DateTime.ToString("yyyy-MM-dd") or whatever format the database accepts the date string in. HTH. :) Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
-
I'm sorry to say this, but your question is not very clear. Is English not your native language? Here's what I *think* the problem is: If you have a DateTime value, do NOT use the ToLongDateString() method as it formats the date depending on the user's Windows settings. Rather, use DateTime.ToString("yyyy-MM-dd") or whatever format the database accepts the date string in. HTH. :) Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
Hi, Vikram.. Thanks for replying.. Actually, maybe what i explained is not very clear to you.. But actually, what i wants to do is that i've create a calendar and what i need to do now is that when i click on the date i've select it will show out the database of the user.. Is it clearer for you. As you've mention earlier on that i shouldn't write the ToLongDateString() if i using DateTime.ToString().. Can i ask you, then now how should i write the code so that it'll show out the database when i click on the date. Thanks!! Cheers!
-
Hi, Vikram.. Thanks for replying.. Actually, maybe what i explained is not very clear to you.. But actually, what i wants to do is that i've create a calendar and what i need to do now is that when i click on the date i've select it will show out the database of the user.. Is it clearer for you. As you've mention earlier on that i shouldn't write the ToLongDateString() if i using DateTime.ToString().. Can i ask you, then now how should i write the code so that it'll show out the database when i click on the date. Thanks!! Cheers!
saddies wrote:
what i need to do now is that when i click on the date i've select it will show out the database of the user
What do you mean by 'database of the user'? Let's say you want to retrieve all the details of employees who joined on a certain date, from a table Employee. Assume that the user chose the date 01 Sept 2004, and that it is contained in the DateTime value userDate. Your query should look like:
string selectQuery = "SELECT * FROM Employees WHERE JoiningDate='" + userDate.ToString("yyyy-MM-dd") + "'";
Of course, the details will differ in your application, but you should get the general idea. :) Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
-
saddies wrote:
what i need to do now is that when i click on the date i've select it will show out the database of the user
What do you mean by 'database of the user'? Let's say you want to retrieve all the details of employees who joined on a certain date, from a table Employee. Assume that the user chose the date 01 Sept 2004, and that it is contained in the DateTime value userDate. Your query should look like:
string selectQuery = "SELECT * FROM Employees WHERE JoiningDate='" + userDate.ToString("yyyy-MM-dd") + "'";
Of course, the details will differ in your application, but you should get the general idea. :) Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
-
Hi.. I would like to ask about C# web application.I have created a calendar. I would like to ask that if i click the date on the calendar,how can i retrieve the database ?? I have tried butit seem like is not working. Below is the code, could anyone help me with it as i'm totally lost?? Thanks!! The code: private void Radcalendar2_SelectionChanged(object sender, System.EventArgs e) { TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); } I just want to retrieve(view) the user's login/logout time of date we have click on the calendar. Anyone who can help me..You can also edit from here or a new code... Thanks!! :-D Cheers!
Hi, I can see from your code that when a user selects a date, you display it to him apsolutely 100% perfectly!! It will be formatted according to HIS/HER windows settings This line is perfect! TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); However, take 1 step backwards: DateTime selectedDateTime = CAL1.SelectedDate; Without the .ToLongDateString() call, you have a DateTime Object. The only place you went wrong below is that you tried to parse the string back into a Date, which at best is unnecessary and at worst will not work. SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); The beauty of parameters (especially date ones) is that the ADO.NET provider will take care of any conversion for you... so change the last line to: cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = CAL1.SelectedDate; simple, huh?
-
Well done, the OP was actually using paramaterized queries and you just taught him how to introduce Sql Injection attacks to his code.
Jamie, I took a look at your reply below and it's interesting - I didn't know that parameters are automatically converted at runtime. Thanks! But what do you mean by 'introducing SQL injection attacks'? :confused: Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
-
Jamie, I took a look at your reply below and it's interesting - I didn't know that parameters are automatically converted at runtime. Thanks! But what do you mean by 'introducing SQL injection attacks'? :confused: Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
-
Sorry, how stoopid of me to not link - especially with a good article right here http://www.codeproject.com/cs/database/SqlInjectionAttacks.asp[^]
Thanks, Jamie. Glad to have learnt something useful today. :) Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.
-
Hi, I can see from your code that when a user selects a date, you display it to him apsolutely 100% perfectly!! It will be formatted according to HIS/HER windows settings This line is perfect! TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); However, take 1 step backwards: DateTime selectedDateTime = CAL1.SelectedDate; Without the .ToLongDateString() call, you have a DateTime Object. The only place you went wrong below is that you tried to parse the string back into a Date, which at best is unnecessary and at worst will not work. SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); The beauty of parameters (especially date ones) is that the ADO.NET provider will take care of any conversion for you... so change the last line to: cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = CAL1.SelectedDate; simple, huh?
Hi,J4amieC.. Below is my code which i have changed.. But somehow when i run the program, the database does not show out when i click on the date. Is there's anything wrong or i still missing on some part?? DateTime selectedDateTime = CAL1.SelectedDate; SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = CAL1.SelectedDate; Thanks for your help!!:) Regards