Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All I've a search form, from where i want to retrieve saved date and time values. I've two dateandimepickers which enable a user to select two different date and time from one coloumn of database. I've used BETWEEN FUNCTION but when i am running it, it gives me an error "Conversion failed". Because the date is saved in 2012/01/02 format and DATEANDTIMEPICKER Format is Wednesday April 2012! How can i retrieve it please?

Here is the code :
C#
SqlDataAdapter commm = new SqlDataAdapter("Select * from Donor where [Planned Start Date] Between '"+ dtpSBPlannedStartDate.Checked+"' And '"+ dateTimePicker1.Checked +"' ", con);
                DataSet dsss = new DataSet();
                
                commm.Fill(dsss, "Donor");
                if (dsss.Tables["Donor"].Rows.Count != 0)
                {
                    dataGridView1.DataSource = dsss.Tables["Donor"];
                }
                else
                {
                    MessageBox.Show("Record Not Found..........");
                }
Posted

Um. You do realise that the DateTimePicker.Checked[^] property is a bool, not a DateTime? Perhaps you want the Value property? And to pass it with a Parametrized query?
 
Share this answer
 
Try this...
string DateTo=null,DateFrom=null;
private void SetDateFormat()
{
DateTimePicker1.CustomFormat="yyyy/MM/dd";
DateTimePicker1.Format=DateTimePickerFormat.Custom;
DateTo=DateTimePicker1.Text;
DateTimePicker1.Format=DateTimePickerFormat.Long;//Set Format as per your needs

DateTimePicker2.CustomFormat="yyyy/MM/dd";
DateTimePicker2.Format=DateTimePickerFormat.Custom;
DateFrom=DateTimePicker2.Text;
DateTimePicker2.Format=DateTimePickerFormat.Long;//Set Format as per your needs
}

then Call this Fuction Before Query..like this

YourFunction()
{
SetDateFormat();
string query="Select * from Table_Name where DateTable_name BETWEEN '"+ DateTo +"' AND '"+ DateFrom +"'";
.
.
}


Hope this helps you..
 
Share this answer
 
v2
hi check [Planned Start Date] column have datatype DateTime or smalldatetime
if not then change it to smalldatetime


best Luck
Happy Coding
 
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