Click here to Skip to main content
16,013,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to get the date value from database and set that selected value to my datetimpicker while using dataReader.

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

C#
public void loadVoucher()
        {
            connection = new SqlConnection(DBHelper.ConnectionString());
            connection.Open();
            command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "select * from lease where linovice='" + txtLeaseVoucherNo.Text + "'";
            da = new SqlDataAdapter();
            da.SelectCommand = command;
            ds = new DataSet();
            da.Fill(ds);

            try
            {


                reader2 = command.ExecuteReader();
                {
                    while (reader2.Read())
                    {

                        txtName.Text  = (reader2["client_name"].ToString());
                        txtcFname.Text= (reader2["f_name"].ToString());
                       txtCNIC.Text   = (reader2["cnic"].ToString());
                       txtGName.Text  = (reader2["g_name"].ToString());
                       txtGFname.Text = (reader2["g_fname"].ToString());
                       txtGCnic.Text  = (reader2["g_cnic"].ToString());
                       txtGPhone.Text = (reader2["g_phone"].ToString());
                       txtGPhone2.Text= (reader2["g_phone2"].ToString());
                       txtGAdress.Text= (reader2["g_adress"].ToString());
                       txtGadress2.Text= (reader2["g_adress2"].ToString());
                       txtTotalBill.Text = (reader2["total_bill"].ToString());
                       txtAdvance.Text = (reader2["advance"].ToString());
                       txtPlan.Text = (reader2["lplan"].ToString());
                       txtLeaseAmount.Text = (reader2["lease_amu"].ToString());
                       txtInstalment.Text = (reader2["linstalment"].ToString());
                       txtSMName.Text = (reader2["sales_men"].ToString());
                       txtInspName.Text = (reader2["insp"].ToString());
                       comboBoxReceivedIn.Text = (reader2["GName"].ToString());
                       txtCurrentBal.Text = (reader2["balbefore"].ToString());
                       txtNewCurrentBal.Text = (reader2["balafter"].ToString());
                       DateTime datevalue;
                       if (DateTime.TryParse(reader2["startsfrom"].ToString(), out datevalue))
                       {
                          dateTimePickerStartsFrom.Value= datevalue;
                       }
                       else
                       {
                           // TODO - convertion failed... not a valid datetime
                           // Print it out and see what is wrong with it...
                       }
                       
                        txtChecque.Text = (reader2["checqueNo"].ToString());
                       txtPageNo.Text = (reader2["pageNo"].ToString());
                       

                    }
                   
                }
            }
            catch (Exception ex)
            {
                //  MessageBox.Show(ex.Message);


            }
            finally
            {
                connection.Close();
            }

        }
Posted
Updated 23-Feb-16 8:08am
v2
Comments
Member 13543274 11-Dec-17 6:23am    
Thanks

1 solution

First off, never do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Second, why are you using a loop to retrieve data? You always load it into the same place, so a look is silly - the last set of values will always be the only one displayed.
Third, why are you converting the database value to a string, then parsing it back to a DateTime value? If it's a DateTime in the database - and if it isn't it should be - then a simple cast will give your the value without needing to check anything.

So use a parameterised query, drop the loop and check the number of rows instead (more than one is a problem somewhere), make sure your DB has teh ciorrect datatype columns, and replace the parse with a cast:
C#
dateTimePickerStartsFrom.Value = (DateTime) reader2["startsfrom"];
 
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