I have a stored procedure that has an update statement. It takes 3 parameters: ID,Status,Date
I have written a C# program to call this procedure. If the status is -1, I want the date to be null in the table and if status is 1, date should be current date.
Int32 rowsAffected = 0;
string datenow =null;
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand(
"usp_UpdateProc", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@status", SqlDbType.Int);
cmd.Parameters["@status"].Value = status;
if(status != 1)
datenow = DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt");
cmd.Parameters.AddWithValue("@datenow", @datenow);
cmd.Parameters.Add(
new SqlParameter("@ID", @ID));
try
{
rowsAffected = cmd.ExecuteNonQuery();
}
catch (Exception ep)
{ //throw ep
}
When I do this status and date fields are both nulls in Database. I am not sure why this is happening? Thanks Rashmi
@to the front of a c# variable name - it's used to use reserved words as variable names (like@class)status? If it'snull, thendatenowwill benull, and that's your answer.