Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one masked textbox in windows application 2010 in which I have entered date in dd/mm/yyyy format and converted it in mm/dd/yyyy format(because SQL 2008 database accept date in mm/dd/yyyy format) to submit it successfully in SQL 2008 database as follows:
for eg 25/09/2013
NOTE: My system date is also in dd/mm/yyyy format
C#
private void btn_submit_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO date (discharge_date) values (CONVERT(smalldatetime, '" + maskedtxbx_dischargedate.Text + "', 103))", con);//date is my table name and discharge_date is column name in which i have to save dischargedate.


            cmd.Parameters.AddWithValue("@discharge_date", maskedTextBox1.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            MessageBox.Show("Records inserted");

        }
private void btn_retreive_Click(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from date where discharge_date= '"+maskedTextBox1.Text+"'", con);

            SqlCommandBuilder cmd = new SqlCommandBuilder(da);
            da.Fill(dtusers);
            dataGridView1.DataSource = dtusers;
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

        }

Now comes the second part i.e when my system date format is in mm/dd/yyyy format I have entered date in dd/mm/yyyy format in the same masked textbox and after that I have clicked the retrieve button so that the discharge date entered in masked textbox should match with the date saved in SQL database and then the matching discharge dates should show in grid view.

But when I enter 25/09/2013 it retrieves the results in grid view in mm/dd/yyyy format i.e 09/24/2013

So my question is:
What is the role of system date format in Microsoft Visual Studio 2010 Windows application and SQL 2008 databases when we are already using datetime format functions as 103.
What to do when system date format is in mm/dd/yyyy format?

[EDIT]Do not SHOUT![/EDIT]
Posted
Updated 2-Oct-13 23:38pm
v6
Comments
OriginalGriff 3-Oct-13 5:15am    
Sorry?
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
alanmaster 3-Oct-13 5:25am    
ok

1 solution

Why?
Why do you do that?
Your field in the DB is a DateTime field, yes? If it isn't, it should be, because doing sorting and comparisons on string-based dates is a pain in the A**.

You are using a parametrised query to insert the value - well done - except as a string, which is silly.

Don't. Convert the masked edit into a DateTime in the C# code using DateTime.TryParseExact: this also validate the date and removes any attempts to put the 31st April, or 29th Feb when it isn't a leap year and so forth. So your DB ends up cleaner because you only ever get valid dates entered.

Then, use your parameterized query to pass the DateTime value directly.
C#
         DateTime dt;
         if (DateTime.TryParseExact(maskedtxbx_dischargedate.Text, "dd/MM/yyyy", out dt))
            {
            SqlCommand cmd = new SqlCommand("INSERT INTO [date] (discharge_date) values (@discharge_date)", con);
            cmd.Parameters.AddWithValue("@discharge_date", dt);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
...


Then use a similar process to do the comparisons.
 
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