Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My date time has an error where it gives me the error

C#
string d = Request.QueryString["ava"];
string c = Request.QueryString["to"];
string strConnString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
int term = Convert.ToInt32(Request.QueryString["term"]);
double rate = Convert.ToDouble(Request.QueryString["rate"]);
DateTime a = Convert.ToDateTime(d);
DateTime b = Convert.ToDateTime(c);
txtEff.Text = d;
txtExp.Text = c;
txtrate.Text = Convert.ToString(rate);
txtTerm.Text = Convert.ToString(term);
txtType.Text = Request.QueryString["type"];

string str = "Select id from LoanRates where LoanTerm=@term and LoanRate=@rate and LoanAvailableFrom=@LoanAvailableFrom and LoanAvailableTo=@LoanAvailableTo and LoanType=@type";
SqlCommand insert = new SqlCommand(str, con);

insert.Parameters.AddWithValue("@term", int.Parse(txtTerm.Text));
insert.Parameters.AddWithValue("@rate", double.Parse(txtrate.Text));
insert.Parameters.AddWithValue("@type", txtType.Text);
//insert.Parameters.AddWithValue("@LoanAvailableFrom", DateTime.Parse(txtEff.Text));
//insert.Parameters.AddWithValue("@LoanAvailableTo", DateTime.Parse(txtExp.Text));
con.Open();

SqlDataReader reader = insert.ExecuteReader();
if (reader.Read())
{
    Lblid.Text = reader["Id"].ToString();
    con.Close();
}
Posted
Updated 7-Aug-16 21:37pm
v3
Comments
Philippe Mori 6-Aug-16 11:38am    
Use code block for your code to improve readability.
Maciej Los 7-Aug-16 14:52pm    
What's exact error message? Improve your question!
Patrice T 7-Aug-16 14:59pm    
Which error message and where in code ?
Use Improve question to update your question.

1 solution

Why do you do such weird things like:
C#
string d = Request.QueryString["ava"];
DateTime a = Convert.ToDateTime(d);
txtEff.Text = d;
//insert.Parameters.AddWithValue("@LoanAvailableFrom", DateTime.Parse(txtEff.Text));

Instead of Convert.ToDateTime use DateTime.ParseExact, and then use that value directly for your SQL query instead of a TextBox content:
insert.Parameters.AddWithValue("@LoanAvailableFrom", a);
Of course, you should also take care of better naming of your variables.
 
Share this answer
 

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