I want to get messages in case of success or failure. So both conditions should return me something from message pane.
我想在成功或失败的情况下收到消息。所以这两个条件都应该从消息窗格返回一些内容。
This is the code I am trying to use, but this never call the conn_InfoMessage
. It always skips that.
这是我尝试使用的代码,但这从不调用conn_InfoMessage。它总是会跳过它。
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sp.StoredProcedureName;
cmd.Parameters.AddRange(parameters.ToArray());
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage);
pnlResults.Visible = true;
grdResults.DataSource = dr;
grdResults.DataBind();
}
}
lblInfo.Text = myMsg;
pnlInfo.CssClass = "ui-state-active";
}
conn.Close();
}
static void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
myMsg += "/n" + e.Message ;
}
3 个解决方案
#1
3
By the time you can call dr.HasRows it is too late. You need to hook up the event handler at an earlier time.. say, right before you open the connection.
当你可以打电话给Dr.HasRows时,为时已晚。您需要在较早的时间连接事件处理程序..比如,在打开连接之前。
#2
0
Make sure you're enabling that event before you open the connection or make use of it in any other way:
确保在打开连接或以任何其他方式使用它之前启用该事件:
using (SqlConnection conn = new SqlConnection(connString))
{
conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage);
conn.FireInfoMessageEventOnUserErrors = true;
conn.Open();
// rest of code follows...
#3
0
Try setting the SqlInfoMessageEventHandler
delegate before you execute the command. I'd move it to the next line after the Conn.Open
method call.
在执行命令之前,请尝试设置SqlInfoMessageEventHandler委托。在Conn.Open方法调用之后,我将它移动到下一行。
#1
3
By the time you can call dr.HasRows it is too late. You need to hook up the event handler at an earlier time.. say, right before you open the connection.
当你可以打电话给Dr.HasRows时,为时已晚。您需要在较早的时间连接事件处理程序..比如,在打开连接之前。
#2
0
Make sure you're enabling that event before you open the connection or make use of it in any other way:
确保在打开连接或以任何其他方式使用它之前启用该事件:
using (SqlConnection conn = new SqlConnection(connString))
{
conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage);
conn.FireInfoMessageEventOnUserErrors = true;
conn.Open();
// rest of code follows...
#3
0
Try setting the SqlInfoMessageEventHandler
delegate before you execute the command. I'd move it to the next line after the Conn.Open
method call.
在执行命令之前,请尝试设置SqlInfoMessageEventHandler委托。在Conn.Open方法调用之后,我将它移动到下一行。