Discussion:
InfoMessage Event with Fill not showing PRINT output after SELECT
(too old to reply)
c***@gmail.com
2016-05-13 14:01:50 UTC
Permalink
I am using the InfoMessageHandler to capture PRINT statements during Data Adapter's Fill execution. But I don't get any PRINT out put in C# after I have executed the SELECT. Please see code below: Can somebody help please?

this._txtPRINT.Clear();

string sSql = @"
PRINT '-------------------------------'
PRINT '-- START -- '
PRINT '-------------------------------'
SELECT GETDATE()
PRINT '-------------------------------'
PRINT '-- END -- '
PRINT '-------------------------------'";

SqlCommand oCmd = new SqlCommand(sSql, this.GetConnection());
oCmd.Connection.InfoMessage += this.Query_SqlInfoMessage;
SqlDataAdapter oAD = new SqlDataAdapter(oCmd);
DataTable oTable = new DataTable();
oAD.Fill(oTable);

I don't get the PRINT statement output after the SELECT GETDATE().

private void Query_SqlInfoMessage(object sender, SqlInfoMessageEventArgs e)
{
try
{
foreach (var oItem in e.Errors)
this._txtPRINT.Text += Environment.NewLine + e.Message;
}
catch (Exception oEx)
{
MessageBox.Show(oEx.Message);
}
}

Please note that I am developing a framework for executing queries and being able to capture all PRINT statements is crucial for me. I have no control over the SQL so I can't for example raise error instead of PRINT.
Arne Vajhøj
2016-05-14 02:02:44 UTC
Permalink
Post by c***@gmail.com
I am using the InfoMessageHandler to capture PRINT statements during
Data Adapter's Fill execution. But I don't get any PRINT out put in C#
after I have executed the SELECT. Please see code below: Can somebody
help please?
SqlCommand oCmd = new SqlCommand(sSql, this.GetConnection());
oCmd.Connection.InfoMessage += this.Query_SqlInfoMessage;
SqlDataAdapter oAD = new SqlDataAdapter(oCmd);
DataTable oTable = new DataTable();
oAD.Fill(oTable);
I don't get the PRINT statement output after the SELECT GETDATE().
Try filling a DataSet instead of a DataTable.

It seems to force it to continue through the SQL.
Post by c***@gmail.com
private void Query_SqlInfoMessage(object sender, SqlInfoMessageEventArgs e)
{
try
{
foreach (var oItem in e.Errors)
this._txtPRINT.Text += Environment.NewLine + e.Message;
I assume this is oItem.Message in your real code.
Post by c***@gmail.com
}
catch (Exception oEx)
{
MessageBox.Show(oEx.Message);
}
}
Arne
c***@gmail.com
2016-05-16 10:49:11 UTC
Permalink
Hi Arne, your suggestion works. Thanks for taking time to share the solution.
Loading...