Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void btn_BooksList_Click(object sender, EventArgs e)
       {
           FRM_ReservationList frm = new FRM_ReservationList();


           con.Open();
           s = " select R.P_id, Patients.P_Name,Total_Reservation Date_Reservation   ,  R.Notes from ResrvationData R  
           s = s + " where  Date_Reservation = '" + dateTimePicker_Time.Text + "' ";
           s = s + " order by Date_Reservation desc , Total_Reservation ";
           sCommand = new SqlCommand(s, con);
           sdAdapter = new SqlDataAdapter();
           sdAdapter.SelectCommand = sCommand;
           dt = new DataTable();
           sdAdapter.Fill(dt);
           //BindingSource BSource = new BindingSource();
           //BSource.DataSource = dt;
           frm.dataGridView_ReservationList.DataSource = dt;
           con.Close();
           frm.ShowDialog();

       }


What I have tried:

How i can find solution for this problem
Posted
Comments
Kornfeld Eliyahu Peter 15-Feb-16 8:44am    
Bear in your mind that not every string can be converted to a date/time value (what is the date/time equivalent of your name?)...
So take the debugger, hit the line and check the value that can not be converted...
Wombaticus 15-Feb-16 8:44am    
1. Validate dateTimePicker_Time.Text
2. Use a parametized query

1 solution

First off, stop doing that.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
In this case, that code isn't vulnerable to SQL injection, but it is likely to fail because SQL doesn't know what locale the user is in, and tries to "guess" what the actual date format is when it does the convert.
If you pass the DateTimePicker.Value property directly as a parameter, then it needs no conversion in any direction, and SQL just gets the correct value immediately.
C#
s = " SELECT R.P_id, Patients.P_Name,Total_Reservation Date_Reservation   ,  R.Notes FROM ResrvationData R ";
s = s + " WHERE Date_Reservation = @DR ";
s = s + " ORDER BY Date_Reservation desc , Total_Reservation ";
sCommand = new SqlCommand(s, con);
sCommand.Parameters.AddWithValue("@DR", dateTimePicker_Time.Value);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900