Sunday, 8 September 2013

Date parameter in SQL Server stored procedure from ASP.NET

Date parameter in SQL Server stored procedure from ASP.NET

I've written an SQL Server stored procedure that, amongst other things,
accepts a date parameter:
CREATE PROCEDURE data.addScore
... (some parameters),
@testDate date
AS
On an ASP.NET page I have a textbox with a Calender Extender control (from
the AJAX Toolkit) that allows the user to select a date; the date is
stored in the textbox in the format DD/MM/YYYY. I have some code that is
supposed to execute the stored procedure with the parameters, using the
date value from the textbox:
cmd.Parameters["@testDate"].Value =
Convert.ToDateTime(((TextBox)li.FindControl("date")).Text);
I know I can definitely get to the textbox's value using
((TextBox)li.FindControl("date")).Text, but for the life of me I can't get
it into a format that doesn't throw up one of a few errors, mainly 'Input
string was not in the correct format'.
When I run the stored procedure directly on the server I usually bind the
parameter like @testDate = 'YYYYMMDD'.

No comments:

Post a Comment