467,915 Members | 1,274 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

odd SqlInfoMessageEventHandler problem (source code provided)

Hi
I'm trying to get the InfoMessage code below working. What I'm
finding is that the print statements have to be before the select
statement, which kind of defies the point in my scenario.
e.g. here the event will fire for 'hi mum' but not for 'hi again'
What am I doing wrong here?

thanks
<add name="Northwind"
connectionString="Integrated
Security=SSPI;database=Northwind;server=localhost; Connect Timeout=30;
Persist Security Info=False"
/>

private void ShowPrintStatements()
{
DbProviderFactory factory = SqlClientFactory.Instance;

ConnectionStringSettings settings =
ConfigurationManager.ConnectionStrings["Northwind"];
string theConnectionString = settings.ConnectionString;

DbConnection connection = factory.CreateConnection();
if (connection is SqlConnection)
{
((SqlConnection)connection).InfoMessage += new
SqlInfoMessageEventHandler(Chapter4_InfoMessage);
}

connection.ConnectionString = theConnectionString;
connection.Open();

DbCommand command = factory.CreateCommand();
command.Connection = connection; command.CommandType =
CommandType.Text;
command.CommandText = "print 'hi mum';select 1 as somecolumn;print
'hi again'";
DbDataReader reader = command.ExecuteReader();

reader.Close();
}

void Chapter4_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
string message = e.Message;
}
Nov 5 '08 #1
  • viewed: 3279
Share:
2 Replies
co**********@googlemail.com (co**********@googlemail.com) writes:
I'm trying to get the InfoMessage code below working. What I'm
finding is that the print statements have to be before the select
statement, which kind of defies the point in my scenario.
e.g. here the event will fire for 'hi mum' but not for 'hi again'
What am I doing wrong here?
You need to call .NextResult to get the second message. To do thing
properly, you should always iterate over .NextResult until returns NULL,
to be sure that you get all results and messages.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Nov 5 '08 #2
You need to call .NextResult to get the second message. To do thing
properly, you should always iterate over .NextResult until returns NULL,
to be sure that you get all results and messages.
Yep, that did it, thanks :-)
Nov 7 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Milos Prudek | last post: by
5 posts views Thread by Mitchell Vincent | last post: by
135 posts views Thread by Xah Lee | last post: by
158 posts views Thread by Giovanni Bajo | last post: by
66 posts views Thread by Jon Skeet [C# MVP] | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.