2014-06-12 44 views
0

獲取文本框的值,我想更新此窗體的登錄和註銷時間:無法在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 [email protected]"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][email protected],[LogOutTime][email protected] WHERE [DayLogId][email protected]"); 

    //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); 
     } 
    } 
} 
+0

gridview和表單之間的關係是什麼?一個人的更新應該觸發另一個人的更新嗎? – Christos

+1

這是一種症狀,通常與Page_Load事件 – Steve

+0

上缺少的IsPostBack測試相關聯,後者發佈您的頁面加載事件 –

回答

0

您應該確保文本框在單擊事件運行前被填充。正如史蒂夫所建議的那樣,通常情況下,當您在每次回發初始化數據時都會得到這個數據,如果數據沒有改變,這是不必要的。