Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting an error that this date is overflowing. It must be between the date specified in the question. Please help me. I can't understand how to solve this error.

What I have tried:

C#
private void bindSearchDetails()
        {
            DataSet dsGetData = new DataSet();
            SqlCommand sqlCmd = new SqlCommand();                                     
            if (connString.State == ConnectionState.Closed)
            {
                connString.Open();
            }
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.AddWithValue("@Origin",Convert.ToString(Request.QueryString["Origin"]));
            sqlCmd.Parameters.AddWithValue("@Destination", Convert.ToString(Request.QueryString["Destination"]));
            sqlCmd.Parameters.AddWithValue("@TravelDate", Convert.ToDateTime(Request.QueryString["TravelDate"]));
            sqlCmd.CommandText = "ispGetAvailableBusDetails";
            sqlCmd.Connection = connString;
            SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
            sda.Fill(dsGetData);
            if (dsGetData.Tables[0].Rows.Count > 0)
            {
                hlinkSearch.Visible = false;
                gvBusDetails.DataSource = dsGetData.Tables[0];
                gvBusDetails.DataBind();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Trip not available, Please search again with different date')", true);
                hlinkSearch.Visible = true;
            }
        }
Posted
Updated 26-Jan-22 4:56am
v2

1 solution

Parse the date and check whether it is within the acceptable range:
C#
DateTime travelDate;
if (DateTime.TryParse(Request.QueryString["TravelDate"], out travelDate))
{
    sqlCmd.Parameters.AddWithValue("@TravelDate", travelDate);
}
else
{
    sqlCmd.Parameters.AddWithValue("@TravelDate", DBNull.Value);
}

Also, the QueryString indexer already returns a string; there's no need to call Convert.ToString on those values:
C#
sqlCmd.Parameters.AddWithValue("@Origin", Request.QueryString["Origin"]);
sqlCmd.Parameters.AddWithValue("@Destination", Request.QueryString["Destination"]);

NB: If you want to allow dates outside of that range, change your SQL datatype from datetime to either datetime2 or date, both of which allow the same range of values as the .NET DateTime type:
Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Docs[^]
 
Share this answer
 
Comments
Ashantia Clarke 26-Jan-22 9:06am    
@RichardDeeming your solution fixed the error but why is it still rejecting the user input and giving the alert message ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Trip not available, Please search again with different date')", true);
Richard Deeming 26-Jan-22 9:07am    
Either your input isn't a valid date, or there are no matching records. Since I can't see your input, and I have no access to your database, I can't tell you which.
Ashantia Clarke 26-Jan-22 9:20am    
can i have your email address so I can share the sql and code with you please. It should be submitted today on the school portal before 5pm. I need help with this please.
Richard Deeming 26-Jan-22 9:25am    
No, that's not how this site works. You ask your questions publicly, and wait to see if anyone can answer. You don't get to have private email help / tuition for your homework assignments.
Ashantia Clarke 26-Jan-22 9:48am    
okay thanks, it's not like I'm asking you to do the whole website, I just want this error out the way. Thanks for your solution though, I appreciated it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900