无法在Update方法上检索Textbox的值

时间:2014-06-12 20:51:49

标签: c# gridview sql-update

我想更新此表单的登录和退出时间:

enter image description here

我的代码是:

protected void btnUpdate_Click(object sender, EventArgs e)
{
    string LoginTime = txtIn.Text;
    string LogOutTime = txtOut.Text;
    long DayLogId = Convert.ToInt64(Request.QueryString["ID"]); 

    System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection();
    dataConnection.ConnectionString =@"Data Source=DELL\SQLSERVER1;Initial Catalog=LoginSystem;Integrated Security=True";

    System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand();
    dataCommand.Connection = dataConnection;

    //tell the compiler and database that we're using parameters (thus the @first, @last, @nick)  
    dataCommand.CommandText = ("UPDATE [DayLog] SET [LoginTime]=@LoginTime,[LogOutTime]=@LogOutTime WHERE [DayLogId]=@DayLogId");

    //add our parameters to our command object  
    dataCommand.Parameters.AddWithValue("@LoginTime", LoginTime);
    dataCommand.Parameters.AddWithValue("@LogOutTime", LogOutTime);
    dataCommand.Parameters.AddWithValue("@DayLogId", DayLogId);


    dataConnection.Open();
    dataCommand.ExecuteNonQuery();
    dataConnection.Close();
}

在前两行方法中,

    string LoginTime = txtIn.Text;
    string LogOutTime = txtOut.Text;

当我调试时,它没有显示我重新插入的值。如果我手写

,此代码有效
    string LoginTime = "11:44:11";
    string LogOutTime = "12:44:11";   

注意:

文本框中表单的值来自另一个页面GridView。

protected void grdEmployee_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "View")
    {
        GridViewRow grRow = ((Control)e.CommandSource).NamingContainer as GridViewRow;
        Label DayLogId = grRow.FindControl("lblDayLogId") as Label;
        if (Convert.ToInt16(DayLogId.Text) > 0)
        {
            Response.Redirect("~/Employee/OutLog_Day.aspx?ID=" + DayLogId.Text, false);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应该确保在单击事件运行之前填充文本框。正如史蒂夫建议的那样,通常在初始化每次回发时的数据时都会得到这个,如果数据没有改变则这是不必要的。