View SQLDataSource’s SQL statement in ASP.NET


It is pretty easy to view what has been sent to SQL Server in SQL Profiler within SSMS. In case you want to access this information directly in your ASP.NET page for the SQLDataSource you are using, you can work within -ING events of SQLDataSouce to view this information. Here is an example with  OnInserting event:
VB.NET:

Protected Sub SqlDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting

Dim mystring As String = e.Command.CommandText.ToString()

Dim i As Integer = 0

For i = 0 To e.Command.Parameters.Count – 1

mystring +=

"<Br />" + e.Command.Parameters(i).ToString() + ": " + e.Command.Parameters(i).Value.ToString()

Next

Me.lblDebug.Text = mystring.ToString

End Sub

 

C#:

protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)

{

string mystring=e.Command.CommandText.ToString();

        for (int i = 0; i < e.Command.Parameters.Count; i++)

            mystring += "<Br />" + e.Command.Parameters[i].ToString()
       + ": " + e.Command.Parameters[i].Value.ToString();

        Response.Write(mystring);

}

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s