1

I am having problems with a sql query from C# to an Sql Server, not sure what is causing it....

The code looks like this:

SqlCommand cmd = new SqlCommand(
    "SELECT tscoc_Name, 
            tscoc_Start, 
            tscoc_End 
            FROM 
            tbl_SchedulerOnCall 
            WHERE (
                tscoc_Start > @fd 
                AND 
                tscoc_End < @ld) 
                AND 
                tscoc_Start = @state", 
    con);
cmd.Parameters.AddWithValue("fd", SqlDbType.DateTime).Value = 
    startDate.ToString("yyyy-MM-dd");
cmd.Parameters.AddWithValue("ld", SqlDbType.DateTime).Value = 
    startDate.AddDays(14).ToString("yyyy-MM-dd");

The SQL query is this:

exec sp_executesql 
N'SELECT tscoc_Name, 
         tscoc_Start, 
         tscoc_End 
FROM 
tbl_SchedulerOnCall 
WHERE (
    tscoc_Start > @fd 
    AND 
    tscoc_End < @ld) 
    AND 
    tscoc_Start = @state', 
N'@fd datetime, @ld datetime, @state nvarchar(2)', 
@fd = 'Oct  8 2012 12:00:00:000AM', 
@ld = 'Oct 22 2012 12:00:00:000AM', 
@state = N'SA'

And the error is:

Syntax error converting datetime from character string.

Anyone have any idea what is happening?

1
  • what is the current format of your date in string? Commented Oct 10, 2012 at 7:06

4 Answers 4

4

You're converting your DateTime values (assuming startDate is indeed of type DateTime in your .NET code) to string when assigning to the SqlParameter - what for? That's totally useless and pointless... and it causes potential issues with date/time and language and regional settings - as you're seeing...

Just use:

cmd.Parameters.AddWithValue("fd", SqlDbType.DateTime).Value = startDate;
cmd.Parameters.AddWithValue("ld", SqlDbType.DateTime).Value = startDate.AddDays(14);

and then you should be fine.

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

1 Comment

Yes, you are right. That was test code. The sql statement I posted, was when my code was identical to yours.
1

You shouldn't need to convert the datetime to a string in order to add it as a parameter value. Just add the raw DateTime object itself, and the .NET SQL code will do the rest.

1 Comment

I agree, the ToString was there for testing, I got the error on lots of combinations I tried.
0

use DateTime.ParseExact. This is useful when you have non standard format for your date in string.

  string dateString = "Oct  8 2012 12:00:00:000AM";
  string format = "MMM dd yyyy hh:mm:ssFFFtt";
  CultureInfo provider = CultureInfo.InvariantCulture;
  DateTime result = DateTime.ParseExact(dateString, format, provider);

  cmd.Parameters.AddWithValue("fd", SqlDbType.DateTime).Value = result;

Comments

0

Why are you using AddWithValue? The second parameter to that is meant to be the value you want to pass, not the data type.

Just do:

SqlCommand cmd = new SqlCommand("SELECT tscoc_Name, tscoc_Start, tscoc_End FROM tbl_SchedulerOnCall WHERE (tscoc_Start > @fd AND tscoc_End < @ld) AND tscoc_Start = @state", con);
cmd.Parameters.AddWithValue("fd", startDate);
cmd.Parameters.AddWithValue("ld", startDate.AddDays(14));

Converting dates to strings is usually the first step (as here) to creating a bug.

Comments

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.