I need to get an employees start date. What I am trying to do is when a check box is checked make a call to the database get the date and return it into my C# code. I think I have everything set-up close to perfect, but my call procedure returns an error. Can someone assist me with setting up syntax so that when a check box is checked it will make a call to the database, run a stored procedure, and return the result (as a datetime) from the stored procedure back into my C# syntax?
EDIT --- The error being thrown is: cannot implicitly convert type 'System.DateTime' to 'string'
//Calling Procedure
this.txtStartDate.Text = getHireDate(Constants.Database, employeename);
//Actual Procedure
private static string getHireDate(string Database, string employeename)
{
SqlConnection connection = new SqlConnection(Database);
SqlCommand command = new SqlCommand("_uspGetHireDate", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
//This line throws an error of can not implicitly convert 'System.DateTime' to 'string'
return Convert.ToDateTime(returnValue.Value);
}
//Stored Procedure Being Called
alter procedure [dbo].[_uspGetHireDate]
(
@employeename varchar(100)
)
as
declare @StartDate datetime
set NOCOUNT ON
Set @StartDate = (SELECT CONVERT(VARCHAR(10), HireDate, 101)
FROM tbl_employeeinformation
where hiredate is not null
AND active = 1
AND employeename = @employeename
return @StartDate
HireDate(which I assume is adatetimecolumn) and converting it tovarchar(10)before setting it into adatetimevariable??SqlParameter("returnVal", SqlDbType.Int). But your SP is returning stringSELECT CONVERT(VARCHAR(10), HireDate, 101)datetimebut your observation of theSqlParametertype is spot on