Monday, August 16, 2010

How to print out sql statement in asp.net 2.0 Gridview?

I 'd like to print out the print out sql statement in asp.net 2.0 Gridviewprint out sql statement in asp.net 2.0 Gridview. Tried to use SqlDataSource1.UpdateCommand, but it doesn't print out the values of parameters.


thanks,How to print out sql statement in asp.net 2.0 Gridview?
You can catch the code before the update in the SqlDataSource.Updating Event. Open the SqlDataSource1 properties window and double-click in the Updating field. Write this code (or similar):





protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)


{


string strSQL = SqlDataSource1.UpdateCommand;


foreach(DbParameter parameter in e.Command.Parameters)


{


strSQL = strSQL.Replace(


parameter.ParameterName,


parameter.Value.ToString());


}


lblSQL.Text = strSQL;


}





For string types, you may want to wrap the value in single quotes depending on whether you are actually going to run this SQL against the database once you retrieve it.

No comments:

Post a Comment