Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have simple code in which I have Inserted data in the Specific column,I have placed a check here in the code that If CheckInTime field is filled Then fill the Checkout Field with current system Date time.But It just fill The checkInTime column and If click again then id Add Another row But I want to update that row.
Looking forward for guidance
Following is the code that I have
C#
protected void imgbtn_Click(object sender, ImageClickEventArgs e)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            CheckInCheckOut chkInOut = new CheckInCheckOut();
            if (chkInOut.CheckInTime != null)
            {
                chkInOut.CheckOutTime = DateTime.Now;
                 
            }
            else
            {
                chkInOut.CheckInTime = DateTime.Now;
                db.CheckInCheckOuts.InsertOnSubmit(chkInOut);
            }
            chkInOut.Name = txtName.Text;
          
            db.SubmitChanges();
Posted

1 solution

You should read the chkInOut record from the database by the key (Name?) and only create a new
CheckInCheckOut object when it fails. You're code could look like:

C#
CheckInCheckOut chkInOut = db.CheckInCheckOuts.GetChange(txtName.Text); // if txtName.Text is the key value
if (chkInOut != null)
{
    chkInOut.CheckOutTime = DateTime.Now;
}
else
{
    chkInOut = new CheckInCheckOut();
    chkInOut.CheckOutTime = DateTime.Now;
    chkInOut.Name = txtName.Text;
    db.CheckInCheckOuts.InsertOnSubmit(chkInOut);
}
db.SubmitChanges();


Regards
Piet
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900