1

I have a datepicker textbox. I want to store the date that is selected in the datepicker into a string variable, using c#. How will the code look like?

string date = datepicker.Text; //Does not work

I later in the code want to store the date into a database, like the following code:

string sql = "INSERT INTO tbl_mote (time, date, id) VALUES ('" + time + "','" + date + "','" + id + "')";
NpgsqlCommand command = new NpgsqlCommand(sql, conn);
int no = command.ExecuteNonQuery();

Datepicker:

<asp:TextBox ID="datepicker" runat="server"></asp:TextBox>

What happens is that todays date always saves into variable date and not the selected date.

Trajan
  • 339
  • 2
  • 15
user2939293
  • 793
  • 5
  • 16
  • 41

3 Answers3

2

1) Use datepicker.Value to get the selected date as a DateTime object. A DateTime object has several functions to produce a string for that date.

2) Do not, I repeat, do not concatenate strings for SQL parameters. Use parameters in the query (for SQL server they start with a @) and add values to the Command object later. I've not used an Npgsqlcommand but it must have a similar feature to SQL server SqlCommands.

3) You can use (1) to get a string value if you need it, but don't use it to insert into the database unless that specific database's objects don't understand DateTime objects. Use the DateTime object you got instead, as the value you add to the command object.

George T
  • 698
  • 9
  • 18
  • datepicker.Value is if the box is a DateTimePicker used in Windows Forms. As you don't specify what toolkit you use I assumed that. If you're using something other than Windows Forms it will be different, probably what Trajan has said. – George T Apr 22 '14 at 07:59
  • I'm using asp.net – user2939293 Apr 22 '14 at 08:17
  • I'm not well versed in asp.net but if you've set it as ReadOnly can the date even be changed? – George T Apr 22 '14 at 08:21
  • Ok, I changed it to False, but that is not the problem. The problem is how to capture date into a string variable. – user2939293 Apr 22 '14 at 08:34
  • In my code datepicker.Value does not work (it gets underlined and does not exist), neither does datepicker.selectedData. – user2939293 Apr 22 '14 at 08:37
  • Thank you for your advices about concatenated strings. Thank you for your advice about DateTime. I have now changed the variable to DateTime instead (in postgres the post is stored as DATE). I'll take care of the parameters later. Must focus on getting this to work first. – user2939293 Apr 22 '14 at 08:58
0

The property you're looking for is SelectedDate:

string date = null;
if(datepicker.SelectedData.HasValue)
{
    date = datepicker.SelectedDate.Value.ToString("MM/dd/yyyy");
}
else
{
    // don't forget to define a default behavior. Your DB seems to choose today as a default date when the provided one doesn't fit. 
    // Even if that's ok for you, you should leave a comment to point it out to your colleagues/future self.
}

Besides, from a security point of view, you should consider using parameterized queries.

Community
  • 1
  • 1
Trajan
  • 339
  • 2
  • 15
0

Ok, so here was the problem. I had included the code below in page load. Datepicker therefore always turned back to todays date, even if I changed it in the calender. After removing the line everything worked perfectly. Thanks all for your help!

datepicker.Text = DateTime.Now.ToShortDateString();
user2939293
  • 793
  • 5
  • 16
  • 41