我正在尝试将参数传递到我的文本框中,该文本框与我的数据库进行通信,并在按钮单击时运行存储过程。
如果我对参数值进行硬编码但是我需要在文本框中接受参数,我的按钮有效。
我有什么想法可以修复这段代码来实现这个目标吗?
这是我的课程之一
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";
}
}
答案 0 :(得分: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;
}