Click here to Skip to main content
Rate this: bad
good
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!!!!");
           }
 
           else
           {
               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();
                   //dateTimePicker1_ValueChanged();
                   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();
               }
               else
               {
 
                   txtrctno.Clear();
                   //dateTimePicker1.Update();
                   //this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
                   //this.dateTimePicker1.CustomFormat = " "; //a string with one whitespace
                   txtrcdfrm.Clear();
                   txtgrno.Clear();
                   txtclass.Clear();
                   txtdigits.Clear();
                   txtrupees.Clear();
                   txttowords.Clear();
                   MessageBox.Show("Sorry record does not exits..");
 
               }
               dr.Close();
           }
 

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 7:45am
Arifa S508
Edited 26-Mar-13 23:43pm
v3
Comments
Sergey Alexandrovich Kryukov at 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?
—SA
Member 9823122 at 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
....
braop at 26-Mar-13 13:28pm
   
Not got ur question well but
1). NEVER make a mistake of converting date to String...date is Date. and try using parametrized queries. Re-phrase your qstn..it will make it easier for us to help you. sample code u posted isn't enough.
YAIR-I at 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 at 26-Mar-13 17:53pm
   
My virtual 5!

1 solution

Rate this: bad
good
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";
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2014
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