views:

81

answers:

2

I got this errormessage when I use Linq and and stored procedure as follow. I got this message when the DateTime parameter is NULL. How will I do to solve this?

Errormessage Serverfel i tillämpningsprogrammet. SqlDateTime-spill: Det måste vara mellan 1/1/1753 12:00:00 AM och 12/31/9999 11:59:59 PM.

Codeblock in C#

protected void ButtonSearch_Click(object sender, EventArgs e)
{
    Postit p = new Postit(); 
    DateTime? dt;

    if(TextBoxDate.Text=="")
    {
        dt=null;
    }
    else
    {
        dt=System.Convert.ToDateTime(TextBoxDate.Text);
    }

    GridView1.DataSource = p.SearchPostit(TextBoxMessage.Text, TextBoxWriter.Text, TextBoxMailto.Text, System.Convert.ToDateTime(dt));  
    GridView1.DataBind();
}



public ISingleResult<Entities.SearchPostitResult> SearchPostit([Parameter
(DbType="VarChar(1000)")] string message, [Parameter(DbType="VarChar(50)")] string writer, [Parameter(DbType="VarChar(100)")] string mailto, [Parameter(DbType="DateTime")] System.Nullable<System.DateTime> date)
{           
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), message, writer, mailto, date);

return ((ISingleResult<Entities.SearchPostitResult>)(result.ReturnValue));

Procedure
ALTER PROCEDURE [dbo].[SearchPostit]
    (
    @message varchar(1000)='', 
    @writer varchar(50)='',
    @mailto varchar(100)='',
    @date Datetime=null
    )
AS
    SELECT P.Message AS Postit, P.Mailto AS 'Mailad till', P.[Date] AS Datum , U.UserName AS Användare  
    FROM PostIt P   

    INNER JOIN [User] U ON P.UserId=U.UserId

        WHERE   P.message LIKE '%'+@message+'%' 
AND     P.Mailto LIKE '%'+@mailto+'%'  
AND     (
                @date IS NULL 
            OR  P.Date = @date
        )
AND     U.UserName LIKE '%'+@writer+'%'
    GROUP BY P.Message, P.Mailto, P.[Date], U.UserName
}
A: 

By Assigning NULL to datetime

if(TextBoxDate.Text=="")
{
    dt=null;
}

your Convert statement will fail

System.Convert.ToDateTime(dt)

I'm not sure but i think the default value for datetime is used ("0001/01/01"), hence the query will fail since this date does not fall between

1/1/1753 12:00:00 AM och 12/31/9999

i suggest you assign a minimum date to "dt" when there is no date entered from the UI

if(TextBoxDate.Text=="")
{
    dt= new DateTime(1753,01,01);
}

[EDIT]

All you have todo is remove the redundant Convert statement when calling the stored procedure

    GridView1.DataSource = p.SearchPostit(TextBoxMessage.Text, 
       TextBoxWriter.Text, TextBoxMailto.Text, dt);  

its redundant since you declare dt as DateTime? already.

Neil
Ok, but how to handle this in the procedure then? Have I to set it to null in the procedure to get the right return from it?
CM
Ignore my code above, and just remove this "System.Convert.ToDateTime(dt)" statement from the call to the sp, and change it to just "dt". No convert.
Neil
But I have to convert dt in codebehind....otherwise it will be wrong...
CM
you are converting it in in the else statement if there is a value, otherwise it will use null, and will work,trust me
Neil
Ok, but my method will have Datetime for this varaiable....and how will I solve that then?
CM
your variable is declared as "DateTime? dt;" and the variable the stored procedure takes in is "System.Nullable<System.DateTime> date" which is the same as "DateTime?", unless im missing something obvious, this should work, do you get a compile time error?
Neil
I have this method which take datetime as last varaible. And I have to have this methos cause of an interface:public DataSet SearchPostit(string message, string writer, string mailto, DateTime date){ PostItMethod p = new PostItMethod(); return (p.SearchPostit(message, writer, mailto, date)); }
CM
This Method goes to PostItMethod to the Linqlayer....and in this layer I get error...datatype error
CM
can you post the exact error message, and can you edit your question to include the linq calls, etc. The reason for removing the convert statement is since it is absolutely redundant and whats causing 0001/01/01 to be assigned to your dt when it is null
Neil
A: 
ALTER PROCEDURE [dbo].[SearchPostit]
    (
    @message varchar(1000)='', 
    @writer varchar(50)='',
    @mailto varchar(100)='',
    @date Datetime=null
    )
AS
    SELECT P.Message AS Postit, P.Mailto AS 'Mailad till', P.[Date] AS Datum , U.UserName AS Användare  
    FROM PostIt P   

    INNER JOIN [User] U ON P.UserId=U.UserId

        WHERE   P.message LIKE '%'+@message+'%' 
AND     P.Mailto LIKE '%'+@mailto+'%'  
AND     ISNULL(@Date, 0) IN (P.Date, 0)
AND     U.UserName LIKE '%'+@writer+'%'
    GROUP BY P.Message, P.Mailto, P.[Date], U.UserName
}
Don
This procedure works fine...but how will I do in codebehind...see my problem above....
CM