1

I get a Conversion failed error when converting DateTime from character string, any help?

sqlcon.Open();
sqlcmd = new SqlCommand("select [Uzsak_Nr],[Preke],[Uzsak_Kiekis],[Gaut_Kiekis],[MyDate],[Gaut_Data] from emp where MyDate between '" + TextBoxData1  + "'  and  '" + TextBoxData2 + "' ", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
GridViewRodyti.DataSource = dt;
GridViewRodyti.DataBind();
sqlcon.Close();
4
  • What are the values of your TextBoxData1 and TextBoxData2 variables? Are they strings? Commented Jan 11, 2013 at 11:54
  • No i slelected it from callendar control TextBoxData1.Text = CalendarRodyti.SelectedDate.ToString("yyyy/MM/dd"); this.CalendarRodyti.Visible = false; Commented Jan 11, 2013 at 11:55
  • That's great but you would be best to show the code which populates those variables. Its likely that the TexBoxDataX variables are in a date format that your database cannot parse. What DB are you using? Commented Jan 11, 2013 at 11:58
  • Im using sql server express 2012 and i want to select tada beetween one date and other and those dates i want to choose from textboxes and those text boxes are connected with callendar when i select date on callendar the text wrotes on textboxdate1 and so on.. Sry for my bad english grama :( i hope u understand what i want to say :) Commented Jan 11, 2013 at 12:04

2 Answers 2

2

This has got SQL Injection written all over it...

Use parameterized queries e.g.

var sqlCmd = new SqlCommand("select [Uzsak_Nr],[Preke],[Uzsak_Kiekis],[Gaut_Kiekis],[MyDate],[Gaut_Data] from emp where MyDate between '@startDate' and '@endDate'", sqlcon);
sqlCmd.Parameters.Add(new SqlParameter("@startDate", DateTime.Parse(TextBoxData1)));
sqlCmd.Parameters.Add(new SqlParameter("@endDate", DateTime.Parse(TextBoxData2)));

Your issue is probably due to an invalid date format, if you use SqlParameter and convert the string to a DateTime it should take care of the conversion for you.

Sign up to request clarification or add additional context in comments.

6 Comments

user1945561 is not asking for advice on how to make their code more secure
@KevinBrydon maybe not, but I am giving the advice anyway. Not only will it make his code more secure but it will fix his conversion issue. The problem is trying to pass dates to SQL as strings is never a good idea due to potential localization issues. Best letting the framework do that for you.
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1026: ) expected
@user1945561 looks like you have an extra closing bracket somewhere in your code.
You probably have other issues. See this
|
0

I would advise you do it like this...

sqlcon.Open();
DateTime date1;
DateTime date2;
DateTime.TryParse(TextBoxData1.Text, out date1); //is TextBoxData1 a TextBox?
DateTime.TryParse(TextBoxData2.Text, out date2);


if (date1 != null && date2 != null) {    
    sqlcmd = new SqlCommand(
      "select [Uzsak_Nr],[Preke],[Uzsak_Kiekis],[Gaut_Kiekis],[MyDate],[Gaut_Data] " +
      "from emp where MyDate between @dt1 and  @dt2 ", sqlcon);
    sqlcmd.Parameters.AddWithValue("@dt1", date1);
    sqlcmd.Parameters.AddWithValue("@dt2", date2);
    da = new SqlDataAdapter(sqlcmd);
    da.Fill(dt);
    GridViewRodyti.DataSource = dt;
    GridViewRodyti.DataBind();
    sqlcon.Close();
}

Using parameterised queries is much nicer and doesn't run the risk of injection.

I have assumed you are using SQLServer.

1 Comment

@user1945561 If answer has solved your problem, please mark it as correct!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.