Storing DateTime Format, C# MSSQL DateTime field
-
:doh: I am having trouble storing the date in UK format. I have an aspx page with a DateTime Validator control - works ok. If the Date Text field on the page is entered in US format: mm/dd/yyy my INSERT works fine. However iif the format is UK: dd/mm/yyyy, the INSERT fails. So i got to thinking i should be formatting the date myself after it passes through the Validator. i tried the code below: dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); Is this the correct approach? (full listing shown below) If i do: //values.Add("'" + txtBirth.Text + "'"); and the Date is in US format it works fine, but fails for UK. your help would be greatly appreciated... Carl Full Listing + stuff i've tried: SqlConnection con; string sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append("INSERT INTO [Users] "); sb.Append("(UserID,Login,Password,FirstName,LastName,PhoneNumber,Email,Address,"); sb.Append(" MobileNumber, DateOfBirth,IsAdministrator) "); sb.Append("VALUES ('{0}', '{1}', '{2}', '{3}','{4}','{5}','{6}','{7}','{8}',{9},{10})"); values.Add(Guid.NewGuid().ToString() ); values.Add(txtUser.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); //values.Add(0); if(txtBirth.Text != string.Empty) { //test vars string output="No Output"; DateTime dt; dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); //Test Blocks //block 1 //DateTime dtNow = DateTime.Now; //values.Add("'" + dtNow + "'"); //block 2 //values.Add("'" + txtBirth.Text + "'"); //block 3 //values.Add("'27/11/1999'"); //block 4 //storing the date & time local to the client //DateTime dt; //dt = DateTime.Now; //string output = dt.ToString("G",DateTimeFormatInfo.CurrentInfo); //values.Add("'" + dt + "'"); //block 5 //dt = Convert.ToDateTime(txtBirth.Text); //output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); //values.Add("'" + output + "'"); //block 6 //values.Add("'" + txtBirth.Text + "'"); //test stuff Label1.Text = output; } else values.Add("Null");
-
:doh: I am having trouble storing the date in UK format. I have an aspx page with a DateTime Validator control - works ok. If the Date Text field on the page is entered in US format: mm/dd/yyy my INSERT works fine. However iif the format is UK: dd/mm/yyyy, the INSERT fails. So i got to thinking i should be formatting the date myself after it passes through the Validator. i tried the code below: dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); Is this the correct approach? (full listing shown below) If i do: //values.Add("'" + txtBirth.Text + "'"); and the Date is in US format it works fine, but fails for UK. your help would be greatly appreciated... Carl Full Listing + stuff i've tried: SqlConnection con; string sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append("INSERT INTO [Users] "); sb.Append("(UserID,Login,Password,FirstName,LastName,PhoneNumber,Email,Address,"); sb.Append(" MobileNumber, DateOfBirth,IsAdministrator) "); sb.Append("VALUES ('{0}', '{1}', '{2}', '{3}','{4}','{5}','{6}','{7}','{8}',{9},{10})"); values.Add(Guid.NewGuid().ToString() ); values.Add(txtUser.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); //values.Add(0); if(txtBirth.Text != string.Empty) { //test vars string output="No Output"; DateTime dt; dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); //Test Blocks //block 1 //DateTime dtNow = DateTime.Now; //values.Add("'" + dtNow + "'"); //block 2 //values.Add("'" + txtBirth.Text + "'"); //block 3 //values.Add("'27/11/1999'"); //block 4 //storing the date & time local to the client //DateTime dt; //dt = DateTime.Now; //string output = dt.ToString("G",DateTimeFormatInfo.CurrentInfo); //values.Add("'" + dt + "'"); //block 5 //dt = Convert.ToDateTime(txtBirth.Text); //output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); //values.Add("'" + output + "'"); //block 6 //values.Add("'" + txtBirth.Text + "'"); //test stuff Label1.Text = output; } else values.Add("Null");
I suggest either using DateTime.Parse, or using separate entry fields for day, month and year.
-
I suggest either using DateTime.Parse, or using separate entry fields for day, month and year.
I suggest use Globalization Classes in .Net
using System.Globalization using System.Threading
in page loadstring slang = Request.UserLanguages[0]; Thread.CurrentThread.CurrentCulture = new CultureInfo(slang);
With this code according to user preferences you should be able to save datetime properly. If user is american it expect american style, if it is turkish then it expects turkish style. Request.UserLanguages[0] gives you a string in this format en-UK or en-US first two chars is about language second two chars is about culture. You can check user culture and call different function according to that also. Education is no substitute for intelligence. That elusive quality is defined only in part by puzzle-solving ability. It is in the creation of new puzzles reflecting what your senses report that you round out the definition. Frank Herbert -
:doh: I am having trouble storing the date in UK format. I have an aspx page with a DateTime Validator control - works ok. If the Date Text field on the page is entered in US format: mm/dd/yyy my INSERT works fine. However iif the format is UK: dd/mm/yyyy, the INSERT fails. So i got to thinking i should be formatting the date myself after it passes through the Validator. i tried the code below: dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); Is this the correct approach? (full listing shown below) If i do: //values.Add("'" + txtBirth.Text + "'"); and the Date is in US format it works fine, but fails for UK. your help would be greatly appreciated... Carl Full Listing + stuff i've tried: SqlConnection con; string sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append("INSERT INTO [Users] "); sb.Append("(UserID,Login,Password,FirstName,LastName,PhoneNumber,Email,Address,"); sb.Append(" MobileNumber, DateOfBirth,IsAdministrator) "); sb.Append("VALUES ('{0}', '{1}', '{2}', '{3}','{4}','{5}','{6}','{7}','{8}',{9},{10})"); values.Add(Guid.NewGuid().ToString() ); values.Add(txtUser.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); //values.Add(0); if(txtBirth.Text != string.Empty) { //test vars string output="No Output"; DateTime dt; dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); //Test Blocks //block 1 //DateTime dtNow = DateTime.Now; //values.Add("'" + dtNow + "'"); //block 2 //values.Add("'" + txtBirth.Text + "'"); //block 3 //values.Add("'27/11/1999'"); //block 4 //storing the date & time local to the client //DateTime dt; //dt = DateTime.Now; //string output = dt.ToString("G",DateTimeFormatInfo.CurrentInfo); //values.Add("'" + dt + "'"); //block 5 //dt = Convert.ToDateTime(txtBirth.Text); //output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); //values.Add("'" + output + "'"); //block 6 //values.Add("'" + txtBirth.Text + "'"); //test stuff Label1.Text = output; } else values.Add("Null");