问题
I am trying to fetch number of records for a particular date by feeding the calender extender selection into a textbox and fetching the corresponding count from the database. The datatype of the checkdate column is DateTime. We tried :
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
string result = "select count(*) from <TableName> where Checkdate= GETDATE() and sub_code=@sub_code";
SqlCommand cmd = new SqlCommand(result, connection);
connection.Open();
Label3.Visible = true;
Label3.Text = cmd.ExecuteScalar().ToString();
connection.Close();
}
}
protected void Button1_Click(object sender, EventArgs e) {
Label5.Visible = true;
Label3.Visible = true;
string query = "select count(*) from <TableName> where Checkdate= @checkdate and sub_code=@sub_code";
SqlCommand cmd = new SqlCommand(query, connection);
cmd.Parameters.AddWithValue("@checkdate", tbdate.Text);
connection.Open();
Label5.Text = cmd.ExecuteScalar().ToString();
connection.Close();
}
But I get the Following error : The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
Source Error:
Line 125: connection.Open(); Line 126: Line 127: Label5.Text = cmd.ExecuteScalar().ToString(); Line 128: Line 129: connection.Close();
The format of the date stored in DB is as :2018-04-24 12:00:22.803
回答1:
You have at least two issues here.
First, in the button click event, you have the line
cmd.Parameters.AddWithValue("@checkdate", tbdate.Text);
This is putting the visual (text) representation of the date as a text string into the parameter - you need the date as a date, not the text display of it. This is why you are getting the conversion error. The comments on your question discussed this, so this may already be fixed.
Secondly, in both the SQL queries, you are not checking the date correctly, which is why you are getting the zero count. In the button click query you have "where Checkdate= @checkdate". You said checkdate is a datetime, with a time portion of 12:00 in the example. Your date coming from the program has no time section, (or really it has a time of 00:00), so you are not going to match. you need to check against just the date part of checkdate, like "where CAST(Checkdate as date) = @checkdate".
In the page load, you have same issue but on both sides; you have "where Checkdate= GETDATE()". Getdate returns a time as well, so you won't load anything that was not created the same millisecond you do the page load. You need "where Cast(Checkdate as date) = Cast(GETDATE() as date)"
回答2:
I managed to resolve the issue by the following approach where I just save the date part of get date as : checkdate= convert(date, GETDATE()) and then call it like this :
SqlCommand cmd = new SqlCommand(query, connection);
string textboxdate = tbdate.Text;
DateTime lastdate = DateTime.ParseExact(textboxdate,
"dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
string newFormat = lastdate.ToString("yyyy-MM-dd");
cmd.Parameters.AddWithValue("@checkdate", newFormat);
connection.Open();
Label5.Text = cmd.ExecuteScalar().ToString();
connection.Close();
来源:https://stackoverflow.com/questions/49997398/the-conversion-of-a-calender-extender-selection-date-format-to-a-datetime-data-t