将参数传递给文本框
本文关键字:文本 参数传递 | 更新日期: 2023-09-27 18:17:53
我正在尝试将参数传递到我的文本框中,该文本框与我的数据库通信,并在按钮单击上运行存储过程。
我有按钮工作,如果我硬编码一个参数值,但我需要在文本框中接受参数。
有什么想法我可以修复这个代码来完成这个吗?
这是我的一个类
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";
}
}
你的回购请求。PayrollMonth需要一个参数来传递文本框的值。在按钮点击事件中你可以输入:
var result = repo.PayrollMonth(txtPay.Text);
你的repo需要修改如下:
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;
}