Reputation: 199
I'm doing a website in Asp.Net(Framework 4.0), using SQL server 2008 r2.
My Database fieldname "RegisterTime" & datatype for is time(7).
While inserting into database i'hv used TimeSpan time= DateTime.Now.TimeOfDay;
It insert successfully.
When I'm trying to retrieve my id using time as an input method, it gives error input string was not in correct format.
Code as follows.
cmd = new SqlCommand("SELECT OrderId FROM tbl_Orders WHERE UserId ='" + Convert.ToInt32(Session["UserId"].ToString()) + "' And RegisterTime='" + time.ToString(@"hh\:mm\:ss.ff") + "'", con);
con.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
orderid = "";
orderid = dr["OrderId"].ToString();
}
While adding CultureInfo ci = CultureInfo.InvariantCulture;
Gives error
Upvotes: 4
Views: 3353
Reputation: 31
Try this!
cmd = new SqlCommand("SELECT OrderId FROM tbl_Orders WHERE UserId ='" + Convert.ToInt32(Session["UserId"].ToString()) + "' And RegisterTime='" + time.ToString(@"hh\:mm\:ss\.ff") + "'", con);
In your code, there is a typo,
time.ToString(@"hh\:mm\:ss.ff") to time.ToString(@"hh\:mm\:ss\.ff")
Hope it helps.
Upvotes: 3
Reputation: 14024
You should try it like this
CultureInfo ci = CultureInfo.InvariantCulture;
"' And RegisterTime='" + time.ToString("hh:mm:ss.ff",ci) + "'", con);
for .Net 4.0 There is a configuration switch to restore the old behaviour of TimeSpan.
In .NET 3.5 and earlier, the TimeSpan
struct wasn't IFormattable
. When string.Format
and related methods see something like {0:something}
and the argument supplied isn't IFormattable
, they have nowhere to put that format string, and they just discard it.
Upvotes: 1