薪资系统 - “闹钟”功能

时间:2013-05-03 09:59:25

标签: c# asp.net database ms-access oledb

我正在设计一个使用访问数据库作为后端的工资单系统。

目前,我在实施'时钟输出'功能方面遇到了问题。

'clock in'按钮在数据库中插入时间。但是,单击“时钟输出”按钮时,它会将数据插入新行。有没有办法避免这种情况,并考虑到员工姓名来更新现有的最后一行。我试图用不同的方法来解决这个问题,到目前为止我还没能解决它。

有没有办法纠正这个问题?

我想我必须在某个地方实现一个循环来检查emp名称,时钟输入并在clock out字段中找到空值。但不确定。

请告知。

我的代码如下;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class timecards : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        {
            if (Session["ID"] != null && Session["ID"].ToString() != "")
            {
                PanelUsersInfo.Visible = true;

            }
            else
            {
                PanelUsersInfo.Visible = false;
            }
            empLabel.Visible = false;
            mainPagebutton.Visible = false;
            logoutButton.Visible = false;
            if (Session["Title"] == null || Session["Names"] == null)
            {
                clockPanel.Visible = false;
                return;
            }
            else
            {
                mainPagebutton.Visible = true;
                logoutButton.Visible = true;
                empLabel.Visible = true;
                empLabel.Text = "<b>Hi, " + Session["Names"].ToString() + "</b>";
                nameLabel.Text = Session["Names"].ToString();
                clockinLabel.Text = DateTime.Now.ToString("HH:mm");
                Label1.Text = clockinLabel.Text;

            }
        }
    }




    protected void mainPage_Click(object sender, EventArgs e)
    {
        Response.Redirect("employeeLoggedin.aspx");
    }
    protected void logout_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("default.aspx");
    }
    protected void clockinButton_Click(object sender, EventArgs e)
    {
        informLabel.Text = "Clocked in at " + Label1.Text + "";
        Label1.Visible = false;
        Label2.Visible = false;
        clockinButton.Enabled = false;
        clockoutButton.Enabled = true;

        string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" + "data source=" + Page.Server.MapPath("App_Data\\database.mdb");
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString);
        System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand();


        cmd.Parameters.Add("Employee", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["Employee"].Value = nameLabel.Text;

        cmd.Parameters.Add("Clockin", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["Clockin"].Value = clockinLabel.Text;

        cmd.CommandText = "INSERT INTO [timecard] ([Employee], [Clockin]) VALUES (@Employee, @Clockin)";

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();




    }
    protected void clockoutButton_Click(object sender, EventArgs e)
    {
        clockoutLabel.Visible = true;
        clockoutLabel.Text = "You have now been clocked out.";
        informLabel.Visible = false;
        Label1.Visible = false;
        clockoutButton.Enabled = false;

        infoLabel.Visible = true;
        infoLabel.Text = "If you want to clock in again, please select timecard option from the main employee page.";


        string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" + "data source=" + Page.Server.MapPath("App_Data\\database.mdb");
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString);
        System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand();

        cmd.Parameters.Add("Employee", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["Employee"].Value = this.nameLabel.Text;
        //String x = DateTime.Now.ToString("HH:mm");
        cmd.Parameters.Add("Clockout", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["Clockout"].Value = this.clockinLabel.Text;


        cmd.CommandText = "UPDATE [timecard] SET [Employee]=@Employee, [Clockout]=@Clockout";




            conn.Open();
            int numberOfRows = cmd.ExecuteNonQuery();
            conn.Close();



    }
}

2 个答案:

答案 0 :(得分:0)

如果两个按钮都是INSERTing记录,则两个按钮都运行相同的代码。检查您的.aspx页面,看看这两个按钮是否恰好具有相同的onClick=属性。

答案 1 :(得分:0)

感谢所有帮助过的人,并提出了克服我遇到的困难的建议。

显然,我找到了解决方案。最好将值存储为字符串,然后使用WHERE子句来更新它。出于某种原因,它不会直接从文本字段中获取值。