2

I'm having trouble getting my SqlParameter to work in my SqlDataSource select command. I'm trying to run a query when a new calendar date on a calendar control is selected to filter the query by the date selected. Here's what I have on the C# end:

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    SqlParameter para1 = new SqlParameter("@mydate", SqlDbType.DateTime);
    para1.Value = Calendar1.SelectedDate;
    SqlDataSource1.SelectParameters.Add(para1);
}

I /almost/ found what I need via this thread: How can I set the sqldatasource parameter's value?

but the solution offered by the top answer gives me the following error for the SelectParameters.Add line: "The best overloaded match ... has some invalid arguments."

How can I fix this error and get the parameter to work in my select query?

Thanks.

5
  • Possibly try SqlDbType.DateTime2 Commented Mar 6, 2013 at 13:29
  • @jordanhill123 Thanks for the idea; sadly, nothing changed. Commented Mar 6, 2013 at 13:36
  • What's the value for Calendar1.SelectedDate when this event fires? Commented Mar 6, 2013 at 13:45
  • When the page loads the calendar is set to today's date in this format: 3/6/2013 12:00:00 AM Commented Mar 6, 2013 at 13:58
  • Might sound silly, but can you confirm that Calendar1.SelectedDate is returning a DateTime object? Commented Mar 6, 2013 at 14:24

1 Answer 1

1

Per the comment in regards to Jon Skeet's answer on this link: Setting DateTime as a SqlDataSource parameter for Gridview, try changing this line to

para1.Value = Calendar1.SelectedDate.ToString();

and see if that works.

Edit Per comment from jadarnel27, try the following:

SqlDataSource1.SelectParameters.Add("@mydate", SqlDbType.DateTime, Calendar1.SelectedDate.ToString());

As SQLParameter.Value is of type object, the overload may be doing some validation on the Calendar1.SelectedDate.ToString() to match with the SQLDbType

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

9 Comments

As much as I hate to even contemplate going against Jon Skeet... I cannot see the logic in turning a date object into a string representation. There's just too much to go wrong when converting back to a date object
@freefaller Jon Skeet is saying not to convert from DateTime to string in the linked answer (so agreeing with what you say). jordanhill123 here is referring to the comments by the OP of the other question below Skeet's answer that say they had to convert to string, because the DateTime wasn't valid as a parameter to the method he was using.
@freefaller I don't see the logic either but not sure where else its going wrong. It worked for the OP but I don't like it either...is it possibly something with its use in asp.net or maybe the specific calendar implementation? Just wanted to test to see if it works
And the OP here might be better off using the overload that Jon used in the linked question: SqlDataSource1.SelectParameters.Add("@mydate", SqlDbType.DateTime, Calendar1.SelectedDate.ToString());
Fair enough guys (@jadarnel27) - didn't quite follow exactly what was going on. my only guess is that this calendar control isn't returning a .net standard DateTime object
|

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.