2011-12-15 138 views
0

我正在嘗試將一個參數傳遞到我的文本框中,該文本框與我的數據庫進行通信,並在按鈕單擊時運行存儲過程。將參數傳遞給文本框

如果我硬編碼參數值,但我需要接受文本框中的參數,我有按鈕的工作。

任何想法如何修復此代碼來完成此操作?

這是我的一個類

public FixPayrollMonth PayrollMonth() 
{ 
    return StoreProcPayrollMonth("fix_Payroll_PayingMonth"); 
} 

private FixPayrollMonth StoreProcPayrollMonth(string storeprocedurename) 
{ 
    FixPayrollMonth result = new FixPayrollMonth() {IsSuccess = false }; 
    SqlCommand cmd = new SqlCommand(storeprocedurename, Connection); 
    cmd.Parameters.Add(new SqlParameter("@Month_Change", 123456));   
    cmd.CommandType = System.Data.CommandType.StoredProcedure; 
    cmd.Connection.Open(); 

    using (var data = cmd.ExecuteReader()) 
    { 
     while (data.Read()) 
     { 
      result.MonthChanged = Convert.ToInt32(data["MonthChanged"]); 
      result.IsSuccess = Convert.ToBoolean(data["IsSuccess"]); 
     } 
    } 
    return result; 
} 

這是我的按鈕點擊...我需要將其鏈接到我的文本框稱爲txtPay

protected void btnFixMnth_Click(object sender, EventArgs e) 
{ 
    var result = repo.PayrollMonth(); 
    if (result.IsSuccess) 
    { 
     lblMessageBoxMnthChg.Text = "Succesful Month has been changed to: " + result.MonthChanged; 
    } 
    else 
    { 
     lblMessageBoxMnthChg.Text = "Failed to change month"; 
    } 
} 
+3

你說的意思是「傳遞參數到我的文本框」 ?你只是想設定它的價值? `txtPay.Text =「一些值」`會實現這一點。 – David 2011-12-15 14:17:18

回答

0

會要求您對repo.PayrollMonth通話一個參數傳入文本框的值。因此,在您的按鈕單擊事件,你會怎麼做:

var result = repo.PayrollMonth(txtPay.Text); 

你回購將需要被修改爲如下所示:

public FixPayrollMonth(string pay) 
{ 
     StoreProcPayrollMonth("fix_Payroll_PayingMonth", pay); 
} 

private FixPayrollMonth StoreProcPayrollMonth(string storeprocedurename, string pay) 
{ 
    FixPayrollMonth result = new FixPayrollMonth() {IsSuccess = false }; 
    SqlCommand cmd = new SqlCommand(storeprocedurename, Connection); 
    cmd.Parameters.Add(new SqlParameter("@Month_Change", pay));   
    cmd.CommandType = System.Data.CommandType.StoredProcedure; 
    cmd.Connection.Open(); 



    using (var data = cmd.ExecuteReader()) 
    { 
     while (data.Read()) 
     { 
      result.MonthChanged = Convert.ToInt32(data["MonthChanged"]); 
      result.IsSuccess = Convert.ToBoolean(data["IsSuccess"]); 
     } 
    } 

    return result; 
} 
+0

謝謝你這個工作:) – user1100021 2011-12-15 14:33:02