my dropdown is like this
here iam storing selected value of dropdown into variable of type 'TimeSpan' and saving in database. But it gives exception: 'String was not recognized as a valid TimeSpan.' my database fieldtype is also Time(7)
DateTime start_time = DateTime.ParseExact(starttime.SelectedItem.Text, "hh:mm tt", CultureInfo.InvariantCulture);
TimeSpan stt = start_time.TimeOfDay;
SqlCommand com = new SqlCommand("INSERT INTO IvrDatas starttime values #starttime",conn);
com.Parameters.AddWithValue("#starttime", stt)
Please help me out in this matter.
"11:00 AM" cannot be parsed with the TimeSpan.Parse function. You could use the ParseExact function as follows:
DateTime.ParseExact(starttime.SelectedItem.Text, "hh:mm tt", CultureInfo.InvariantCulture);
You can extract the time from the Parsed datetime with the ".TimeOfDay" function. That will give you the proper value and type
Related
im new to asp.net,im trying to display out my date output in terms of
MM/dd/yyyy
but it came out with
MM/dd/yyyy 12:00:00 AM
what can i do to remove the time?
my vb.net code is like this:
Dim date As String = myDataReader("date").ToString()
lbldate.Text = date
You can pass the format to ToString() method.
Dim date As String = myDataReader("date").ToString("MM/dd/yyyy")
You need to pass the format to the .ToString method, like this:
.ToString("MM/dd/yyyy")
It would also be a good idea to cast the myDataReader("date") to a DateTime object, like this:
Dim dateValue As DateTime = CType(myDataReader("date"), DateTime)
Dim date As String
If dateValue Is Not Nothing Then
date = dateValue.ToString("MM/dd/yyyy")
End If
Note - If the TryCast fails, then Nothing would be the value of dateValue, hence the If dateValue Is Not Nothing Then logic is there.
I want to import date value from excel cell. cell value having "10 October 2013" format. I want to convert it to datetime data type. My code getting error "string was not recognized as a valid datetime"
//code
OleDbCommand olecmd = new OleDbCommand("select * from [Sheet1$]", olecon);
OleDbDataReader olerdr = olecmd.ExecuteReader();
while (olerdr.Read())
{
deldate = olerdr.GetValue(13).ToString();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["irisdb"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("procdamandrugs", con);
cmd.CommandType = CommandType.StoredProcedure;
DateTime dt = DateTime.ParseExact(deldate, "MM/dd/yyyy", CultureInfo.InvariantCulture);//getting error in this line
SqlParameter par9 = new SqlParameter();
par9.ParameterName = "#deleffdate";
par9.SqlDbType = SqlDbType.DateTime;
par9.Value = dt;
cmd.Parameters.Add(par9);
cmd.ExecuteNonQuery();
}
}
Do any one help me to solve this issue.
cell value having "10 October 2013" format.
You are giving wrong format in ParseExact that does not match with the date string you are passing. You need different format than you gave. For day you need dd, for month you need MMMM and for year you need yyyy and you have to give spaces as separator.
It is worth the article Custom Date and Time Format Strings on MSDN for using the string formats for date conversion.
DateTime dt = DateTime.ParseExact(deldate, "dd MMMM yyyy", CultureInfo.InvariantCulture);
I recommend the utilizing the DateTime.TryParse method before constructing your SQL objects. Ensure you have quality input before having a conversation with your database.
http://msdn.microsoft.com/en-us/library/ch92fbc1%28v=vs.110%29.aspx
Below is a sample from my own code for an asp.net application
// Validation
DateTime dtOut_StartDate;
if (!DateTime.TryParse(txtStartDate.Text, out dtOut_StartDate))
{
Message = "Start date is not a valid format.";
txtStartDate.CssClass = ErrorCssClass.TextBox;
txtStartDate.Focus();
return false;
}
Select Date Time setting from Right lower bottom & Change the format from here......
I m using ajax calendar control to a textbox, have set its format as "dd/mm/yyyy" .
Bapur.Date = txtdate.Text;
in data access layer
cmd.Parameters.Add("#Date", SqlDbType.DateTime).Value = bapur.Date;
on saving, like above ( where Date is a string Bapur is object of businesslayer class)
in the database where datatable in database has date as datetime format.
m getting an error : cant convert string to datetime i dint get error when format was "mm/dd/yyyy" .
Basically, I want users to view date in dd/mm/yyyy but on saving i want it in
mm/dd/yyyy
Have tried a lot but not working.
here is my answer ---- https://stackoverflow.com/a/11720162/1445836 -----
You can use:
DateTime.ParseExact("yourDate","formatinWhichYouWant",culture of current string);
ex:
DateTime dt =DateTime.ParseExact("yourDate","formatinWhichYouWant",culture of current string);
string myDate = bapur.Text.Split("/");
string dateString = myDate[1] + "/" + myDate[0] + "/" + myDate[2];
got my answer
string old = txtdate.Text;
string newDate = DateTime.ParseExact(old, "dd/MM/yyyy", null).ToString("MM/dd/yyyy");
Bapur.Date = newDate.ToString();
Hello
I'm trying to update a ms access database. I've got a gridview with a date that i use:
Dim tBox As TextBox = CType(gridStaff.Rows(e.RowIndex).FindControl("sDate"), TextBox)
I then want to take the value from this textbox and assign it to a date variable.
Dim test As Date = CDate(tBox.Text)
The problem I have is that now when I put test variable into my sql update query it stores the date in this format. mm/dd/yyyy instead of the dd/mm/yyyy format I want.
Ive tried different ways to format it I read online but to no success yet. Any advice would be great!
Thanks
Is your problem the conversion from tBox.Text to Date, or is it at the sql update query level? -- You should probably give the sql statement a parameter with a strict type. Converting from tBox.Text into test should probably be done in a Culture aware manner:
'String to convert and target date:
Dim dateString As String = "02/15/2011"
Dim d As Date
' Specify English/US culture when converting string:
Dim cul As Globalization.CultureInfo = Globalization.CultureInfo.GetCultureInfo("en-US")
d = Date.Parse(dateString, cul)
You could also rely on the default culture and simply let Date.Parse do the job without specifying the culture.
try using
Dim test As Date = DateTime.ParseExact(tBox.Text, "d/M/yyyy", CultureInfo.InvariantCulture)
or if your System is set up right and CultureInfo.CurrentCulture has the right date format,
Dim test As Date = DateTime.Parse(tBox.Text, CultureInfo.CurrentCulture )
instead of CDate()
Dim strTime as String = FomatDateForSave("28/12/2010")
Public Shared Function FormatDateForSave(ByVal strDate As String) As Date
FormatDateForSave = Date.ParseExact(strDate,'dd/MM/yyyy', System.Globalization.CultureInfo.InvariantCulture)
End Function
I am expecting strTime to be "12/28/2010" .... But its getting converted to "28/12/2010" ....
The thing is when the operation is performed by FormatDateForSave ... it converts it to "12/28/2010" ...
But when it is returned it is again converted to "12/28/2010"
I have set the Date for Page.Culture to be "dd/mm/yyyy" and want the value to be "mm/dd/yyyy" to be saved in DB.
You should store the date in the db as its native type, not a string. When you fetch it you should format it then.
Why don't you convert your date in sql before insert/update operation?
e.g.
SELECT convert(varchar, getdate(), 101) will give you date in mm/dd/yyyy format.
Sql Convert