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