Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search date by using DateTimePicker

Status
Not open for further replies.

webwired72

Programmer
Sep 20, 2008
4
US
Hi, I am using C# and SQL Express 2005...

I have a table that has a datetime column, on the form I have placed a DateTimePicker and I would like to search by date, using that object, but am running into some snares... Here's my code...

Code:
        private void fillByDateToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.ticketsTableAdapter.FillByDate(this.massageProDataSet.Tickets, ((System.DateTime)(System.Convert.ChangeType(ticketSearchDateTimePicker, typeof(System.DateTime)))));
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

I am getting this error...

Object must implement IConvertible.
 
what is ticketSearchDateTimePicker?
if this is a control you must get the value from the control, not just convert the control to a date time.
something like
Code:
DateTime date = ticketSearchDateTimePicker.SelectedDate;
if this is a string they use
Code:
DateTime date = DateTime.Parse(ticketSearchDateTimePicker);
if this is an object, just cast
Code:
DateTime date = (DateTime)ticketSearchDateTimePicker;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
DateTimePicker is a control from the "Toolbox" in Visual C# IDE...

I tried converting it first like you suggested, but get this error...

Cannot convert type 'System.Windows.Forms.DateTimePicker' to 'System.DateTime'
 
of course not, DateTimePicker is a control. you need to get the date from the control. something like
Code:
DateTime date = ticketSearchDateTimePicker.Value;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top