0

I got a error when I do the follow:

DateFrom has as datatype of datetime

enter image description here

My C# code:

string strQuery = "Select * FROM Agenda Where   DateFrom='" + Calendar1.SelectedDate.Date.Date + "' ";
SqlCommand command = new SqlCommand(strQuery, Global.myConn);
da = new SqlDataAdapter(command);
da.Fill(Global.dsAgenda, "Tabel");
ddlActivity.DataSource = Global.dtAgenda;
ddlActivity.DataBind();

Calendar1 type:

enter image description here

Error:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

2
  • What format is Calendar1.SelectedDate.Date.Date in? Print it out and check. It's trying to cast it to a date (to compare) and failing. Please indicate what kind of database and version. SQL Server? Commented Apr 26, 2013 at 7:49
  • @realnumber3012 How do you do this? Commented Apr 26, 2013 at 7:57

2 Answers 2

4

You should prefer using parameters:

string strQuery = "SELECT * FROM Agenda WHERE DateFrom = @Date";
SqlCommand command = new SqlCommand(strQuery, Global.myConn);
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = Calendar1.SelectedDate;
Sign up to request clarification or add additional context in comments.

Comments

1

Use this

 Convert(DateTime,Calendar1.SelectedDate.Date.Date,103)

1 Comment

I use this: Convert.ToDateTime(Calendar1.SelectedDate.Date.Date) The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

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.