Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi i want to know what property/methods should i use for reading through dr like for text-boxes we can use .ToString();

C#
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
Updated 26-Mar-13 22:43pm
v3
Comments
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?
—SA
Arifa S 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 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 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!

1 solution

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

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

If dr["latedate"].Value is equal DbNull.Value[^], you need to use simple trick:
C#
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:
C#
public void SetMyCustomFormat()
{
   // Set the Format type and the CustomFormat string.
   dateTimePicker1.Format = DateTimePickerFormat.Custom;
   dateTimePicker1.CustomFormat = "dd/MM/yyyy";
}
 
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