Question Update Statement has no Errors but it Doesn't Work

jriz25

New member
Joined
Feb 26, 2014
Messages
3
Programming Experience
Beginner
when i run my update code it says successfully updated but when i try to see my database , nothing has been updated.
will someone please help me what to do. im just a beginner i don't know how will i fix this

Code:
[FONT=Consolas] 
                try
                {




                    this.time2.Text = DateTime.Now.ToString("hh:mm tt");
                    this.label8.Text = System.DateTime.Now.ToShortDateString();




                    ad.UpdateCommand = new OleDbCommand("update Attendance_Records set " + " [Time_Out] = @Time_Out " + " where  Emp_id = @Emp_id and Date = @Date", cn);


                    ad.UpdateCommand.Parameters.AddWithValue("@Emp_id", label4.Text.ToString());
                    ad.UpdateCommand.Parameters.AddWithValue("@Time_Out", time2.Text.ToString());
                    ad.UpdateCommand.Parameters.AddWithValue("@Date", dateTimePicker1.Value.ToShortDateString());
                    if (DateTime.Now.ToString("hh tt") == "12 PM")
                    {
                        cn.Open();
                        ad.UpdateCommand.ExecuteNonQuery();
                        MessageBox.Show("Time OUT Successfull");
                        cn.Close();
                    }
                    else
                    {
                        MessageBox.Show("Sorry TimeOUT Time IS AT 12:00 PM");


                    }


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);


                }
[/FONT]
 

jriz25

New member
Joined
Feb 26, 2014
Messages
3
Programming Experience
Beginner
my database table has
ID,
Emp_id,
Lname,
Fname,
Mname,
Time_In,(this is for AM time)
Time_Out, (For AM time)
TimeIn,(for PM time)
TimeOut,(for PM time)
Date

i like to save time in and time out for both AM and PM in one Row for a specific Emp_id per day
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,049
Location
Sydney, Australia
Programming Experience
10+
The first thing to do is to test the value returned by ExecuteNonQuery. If it's not zero then data was saved. In that case you are simply looking in the wrong place or at the wrong time for the data. Can we see your connection string?
 

jriz25

New member
Joined
Feb 26, 2014
Messages
3
Programming Experience
Beginner
yes i can see but it doesn't save.. it says it has been updated but when i look at my database nothing has change.

Code:
 [COLOR=#333333][FONT=Consolas] ad.UpdateCommand = new OleDbCommand("update Attendance_Records set " + " [Time_Out] = @Time_Out " + " where  Emp_id = @Emp_id and Date = @Date", cn);[/FONT][/COLOR][FONT=Consolas]
[/FONT]
thats my connection string
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,049
Location
Sydney, Australia
Programming Experience
10+
That is not your connection string. That is your SQL code. Your connection string is the string that you pass to your connection object. You also didn't mention anything about the value returned by ExecuteNonQuery.
 
Top Bottom