Click here to Skip to main content
13,050,840 members (70,798 online)
Rate this:
Please Sign up or sign in to vote.
hi i want to know what property/methods should i use for reading through dr like for text-boxes we can use .ToString();

private void Search_Click(object sender, EventArgs e)
         #region Search
           if (txtrctno.Text == "")
               MessageBox.Show("Pleas enter Rctid For Searching!!!!");
               cmd = new SqlCommand("RoyalSearch", con);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add(new SqlParameter("@rctno", int.Parse(txtrctno.Text)));
               dr = cmd.ExecuteReader();
               if (dr.Read())
                  // string theDate = dateTimePicker1.Value.ToShortDateString();
                  // dateTimePicker1.Text = dr["latedate"].ToString();
                   txtrcdfrm.Text = dr["RcvdFrm"].ToString();
                   txtgrno.Text = dr["GRNO"].ToString();
                   txtclass.Text = dr["Class"].ToString();
                   txtdigits.Text = dr["RupeesDigits"].ToString();
                   txtrupees.Text = dr["Rupees"].ToString();
                   txttowords.Text = dr["Towords"].ToString();
                   //this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
                   //this.dateTimePicker1.CustomFormat = " "; //a string with one whitespace
                   MessageBox.Show("Sorry record does not exits..");

and if records does not found then all fields gets cleared and i want datetimepicker should show todays datetime??
Now my query is cleared in short i want whenever records does not exits it should show default means todays datetime otherwise if you have some other idea please share....
Posted 26-Mar-13 6:45am
Arifa S508
Updated 26-Mar-13 22:43pm
Sergey Alexandrovich Kryukov 26-Mar-13 13:17pm
How search can be not available in a database? How could you call such thing a database? What is it?
braop 26-Mar-13 13:28pm
Not got ur question well but
1). NEVER make a mistake of converting date to is Date. and try using parametrized queries. Re-phrase your will make it easier for us to help you. sample code u posted isn't enough.
YAIR-I 26-Mar-13 16:26pm
date need to be with big M's like this dd/MM/yyyy small m's is for minutes. this is why you see 56.
Maciej Los 26-Mar-13 17:53pm
My virtual 5!
Member 9823122 27-Mar-13 3:53am
Actually in form i entered 15 records(for example) so when i am searching till record number 15 every thing is working properly but when i entered record number 16 that time one msgbox appears and it shows "sorry record 16 does not found" ok on clilk button every fields become blank except Datetimepicker it shows lats searched value 13/3/2013(for example) but i want it should show default value i.e todays date time

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Firsy of all, you do not set value for DateTimePicker:
dateTimePicker1.Text = dr["latedate"].ToString();

To set value, use DateTimePicker.Value property[^]:
dateTimePicker1.Value = dr["latedate"].Value;

If dr["latedate"].Value is equal DbNull.Value[^], you need to use simple trick:
dateTimePicker1.Value = dr["latedate"].Value ?? new DateTime(2000,1,1)

Above code should return dr["latedate"].Value or manually defined date.

Why you see: 26/56/2013 instead of 26/03/2013? YAIR-I has answered to you in comment, but see the description of DateTimePicker.CustomFormat[^] property.
To properly set custom format, use:
public void SetMyCustomFormat()
   // Set the Format type and the CustomFormat string.
   dateTimePicker1.Format = DateTimePickerFormat.Custom;
   dateTimePicker1.CustomFormat = "dd/MM/yyyy";

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100