user1596993
user1596993

Reputation: 21

UPDATE statement is not updating results in SQL Server

I am trying to update result in SQL server with below query but its not updating. If i write (update lunTime set lunOut = '2014-12-08 23:23:23.120' where empName='Mike' and date='2014-12-08') it is updating it.

protected void btnLunOut_Click(object sender, EventArgs e)
{
    SqlConnection conn1 = new SqlConnection(
       "Data Source=myServer;Initial Catalog=MY_Srv;Integrated Security=True");

    conn1.Open();

    SqlCommand cmd = new SqlCommand(
     "Update [lunTime] SET lunOut = @LunOUT where (empName=@EmpName and date=@Date)", 
     conn1);
    cmd.Parameters.AddWithValue("@EmpName", drpDwnEmp.Text);
    cmd.Parameters.AddWithValue("@LunOUT", DateTime.Now);
    cmd.Parameters.AddWithValue("@Date", DateTime.Now);

    cmd.ExecuteNonQuery();

    conn1.Close();

    drpDwnEmp.Text = string.Empty;
}

Upvotes: 0

Views: 292

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

Most likely it simple does not find the record for data == DateTime.Now case.

It is unlclear what you want to achieve, but maybe some range conditions on date variable or Now.Date is the solution (assuming date is actually date only, no time portion):

 cmd.Parameters.AddWithValue("@Date", DateTime.Now.Date); 

Upvotes: 2

Related Questions