Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to insert date in dd-MM-yyyy format in c#. Query for inserting is

C#
SqlCommand cmd_cust = new SqlCommand(@"insert into custdetail values ('" + txtInvoiceNo.Text + "','" + txtCustomerName.Text + "','" + txt_contact.Text + "', '" + txtAddress.Text + "', '" + txt_total_amt.Text + "', '" + dt_date.Value.ToString("dd-MM-yyyy") + "')", con_create);
            con_create.Open();
            cmd_cust.ExecuteNonQuery();
            con_create.Close();



I have created table with column name date has datatype date. After inserting record the value in date column field is in yyyy-dd-MM format. I want this in dd-MM-yyyy format.
Posted
Updated 1-Feb-14 3:44am
v2

No, it isn't.
DATETIME and DATE fields ion SQL do not have a "format" any more than they do for DateTime values in C# - they are stored as a number of milliseconds since an arbitrary point in the past. Formatting is only ever applied when the date is converted to a string to display to the user - so check the code that read these values out and displays them: SSMS will normally display records with it's standard formatting (ISO 8601, or yyyy-MM-dd) unless it is specifically told not to.

But please, don't do things 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.
This also means that you don't convert a DateTime value to a string, just to convert it back to a DateTime at the SQL end with all the possible errors that can involve. Just pass the dt_date.Value directly to SQL as a DataTime value via a parameter.
 
Share this answer
 
As you are saying "the value in date column field is in yyyy-dd-MM format" but your code says "dt_date.Value.ToString("dd-MM-yyyy")" So the value should be dd-MM-yyyy. so your date column fieldis not correct i think.

you can use 103 format to convert date
101 is US format that is mm/dd/yyyy
103 is UK format that is dd/mm/yyyy

convert(varchar(10),fieldname,103)
 
Share this answer
 
v2

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