用户控制运行SQL Server存储过程返回DataTable事件处理程序错误
本文关键字:DataTable 事件处理 程序 错误 返回 存储过程 控制 运行 SQL Server 用户 | 更新日期: 2023-09-27 17:54:03
我有一个运行存储过程的用户控件
public DataTable btnFind_Click(object sender, EventArgs e)
{
UtilitiesClass ru = new UtilitiesClass();
string connectionString = ru.getConnectionString();
DataTable dt = new DataTable();
SqlConnection myConnection = new SqlConnection(connectionString);
try
{
myConnection.Open();
SqlCommand cmd = new SqlCommand("FindCust", myConnection);
cmd.Parameters.AddWithValue("@cust", txtCust.Text.Trim());
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ta = new SqlDataAdapter(cmd);
ta.Fill(dt);
myConnection.Close();
}
catch (Exception x)
{
MessageBox.Show(e.ToString());
}
return (dt);
}
我得到一个错误在这个VS生成的代码:
this.btnFind.Click += new System.EventHandler(this.btnFind_Click);
告诉我
的DataTable FindCustControl。btnFind_Click(object, EventArgs)'的返回类型错误。
我不知道从哪里开始。我甚至不明白这个问题。
无法从负责处理按钮单击的事件返回数据表。
更新
要将数据表从用户控件返回到用户控件所在的主表单,您应该在用户控件中实现一个自定义事件。这里有一篇很棒的文章:
http://www.windowsdevcenter.com/pub/a/dotnet/2002/04/15/events.html//Generated code
this.btnFind.Click += new System.EventHandler(this.btnFind_Click);
//Button click handler
public void btnFind_Click(object sender, EventArgs e)
{
var resultset = GetData();
//Do whatever with data here
//...
}
//Query database
public DataTable GetData()
{
UtilitiesClass ru = new UtilitiesClass();
string connectionString = ru.getConnectionString();
DataTable dt = new DataTable();
SqlConnection myConnection = new SqlConnection(connectionString);
try
{
myConnection.Open();
SqlCommand cmd = new SqlCommand("FindCust", myConnection);
cmd.Parameters.AddWithValue("@cust", txtCust.Text.Trim());
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ta = new SqlDataAdapter(cmd);
ta.Fill(dt);
myConnection.Close();
}
catch (Exception x)
{
MessageBox.Show(e.ToString());
}
return (dt);
}