Getting the parameter values being passed to SQL database via SQLDataSource control - asp.net

I am debugging code written by a previous developer :-)
I have a SQLDataSource control that is calling a database stored procedure, and passing quite a large number of paramaters, and the stored proc returns records that populates a gridview control.
When I walk through the code behind to determine the values being passed, is there any way that I can see exactly how the parameters are being formated and passed to the stored proc?

You can use SqlDataSource's Updating event for this. It's parameter SqlDataSourceCommandEventArgs has a property Command that contains the parameters for the query.

Instead of trying to unpick them from Code-behind, use Sql Profiler. If you are using the Express version, there's a Profiler for that too: AnjLab Sql Profiler

Related

Cannot bind result of HTTPUtility method to session variables

I am using ASP.NET Web Forms. I am trying to bind some data from a previous page (including the URL and some specific figures stored in the URL) to session variables for use in a SQL insert statement to a SQL Server database.
An example of one of the assignments is below:
this.Session["URL"] = HttpUtility.ParseQueryString(Request.UrlReferrer.Query)["helpurl"];
The problem I am having is that on executing the SQL insert, the value does not seem to be present and the default value is recorded. SessionState is enabled. I am able to print the result of the HttpUtility method to a label, and I am also able to assign to session variables for a SQL statement if I assign them directly, eg:
this.session["Variable"] = Variable;
Any ideas why the result of the HttpUtility function may not be assigned to the session variable prior to the execution of the SQL insert statement?
It's ok - I placed the assignments inside an
if(!IsPostBack)
And they now work ok and are being recorded to the database. I also removed the 'this' keyword from them.
Thanks anyway

Populating ASP.NET SqlDataSource from a Firebird 2.0 Stored Procedure

I am working on a project where I do not have any control over the versions of products that I have been asked to work with.
I am building an ASP.NET user control. Data is retrieved via SqlDataSource controls working against Firebired 2.0 databases. Everything was fine until I needed to populate one of the SqlDataSource controls via a stored procedure rather than a SELECT statement.
I have verified that the procedure is returning data in EMS SQL Manager for Interbase and Firebird. However, when I set the SqlDataSource's SelectCommand property to EXECUTE PROCEDURE myProc and call Select(), I am getting no data.
My procedure takes no input parameters so it seems to me that it should be straight-forward.
The reality of the situation is that I could use a simple SELECT statement if I were able to move to even 2.1 as I would have access to the List() aggregate function, but that is not a possibility.
Does anyone have any experience trying to do this? Does the procedure call need to be modified in any way? Any help would be greatly appreciated.
If it is selectable stored procedure (it has SUSPEND command in it) then you should call it with SELECT statement, not with EXECUTE PROCEDURE statement. So set your SelectCommand to
SELECT * FROM spName
and it should work.
I would normally do this like so (against SQL Server) :
// set the SqlDataSource's SelectCommand to the *name* of the stored procedure
myDataSource.SelectCommand = "myProc";
// tell the SqlDataSource that this is a *stored procedure* now
myDataSource.SelectCommandType = CommandType.StoredProcedure;
Since your stored procedure takes no input parameters at all, this should really be all!
With SQL Server, selecting the data works just fine with this setup.

Using a static recordset as temporary storage with field updates

I'm calling a stored procedure to generate an ADO recordset and I'd like to be able to update the data before outputting it (but not write those updates back to the DB).
Is this even possible and if so, how?
I've tried a number of cursor and lock types but they either error out immediately or when I try to update a recordset field.
io_oRecordSet.CursorLocation = adUseClient
io_oRecordSet.Open oDataCmd, , adOpenStatic, adLockOptimistic, adCmdStoredProc
... iterate through RS ...
io_oRecordSet("myCol").value = "foo"
This one generate the following error
Microsoft Cursor Engine error '80040e21'
Multiple-step operation generated errors. Check each status value.
From what I've seen I suspect that ADO might not like that the data is coming from a stored procedure since it wouldn't know how to do a DB update.
If you absolutely don't need to update the database, you can use a disconnected Recordset. Just make sure you use a client-side cursor and set the Recordset's ActiveConnection property to Nothing after you open it.

GridView does not load schema from the SQL stored Procedure

I have gridview, when I connect the sqldatasource and run the application, data from stored procedure is shown but when i switch to design mode, the schema does not loads in gridview. I need that schema to rearrange columns and for formatting purpose.
I never had this problem before, All of sudden it is a problem.
What I tried so far "Refresh Schema" brings up the dialog which list all the parameters I am passing to the storedprocedure. After I changed DBType for every single parameter, it still give me an error, says "check your connection and storedprocedure". While the storedprodure does run, shouldn't it automatically update the schema in gridview. I still have the default 3 columns of gridview.
Note: I may get around the problem by adding each column manually but I am doing this to fix problem in the page, in the first place, dont want to run again in gridview problem. This is a new page that I am building. Thanks
It looks like, Schema is not updated just from StoredProcedure because simple there is not enough information how many columns will be returned.
To work around this issue and sort of quick fix, do the following.
Copy the query from you stored procedure and change the SQLDatasource from stored procedure to Select statement. Run your select statement and finish. Notice that your schema will now be updated.
You can now set your SQLDataSource back to stored procedure and the schema will remain the same.
It also occured to me that when you run a stored procedure, the column names are updated in the query using the name 'Databound col0' 'Databound Col1' and so on
I am not sure sometime it does, sometime it does not. Right click on Gridview and click Referesh Schema does not work as it gives some error.
Along the lines of #hmd, when there is not enough info, especially if there is another perceived dataset being return, i.e you are doing something that returns blank or debug data (rows affected, etc.). A workaround for this is to put "SET NOCOUNT OFF" in the beginning of your procedure and then set it back "ON" again before you return the data. This is also a problem on SSRS.

ASP.NET SqlDataSource update and create FK reference

The short version:
I have a grid view bound to a data source which has a SelectCommand with a left join in it because the FK can be null. On Update I want to create a record in the FK table if the FK is null and then update the parent table with the new records ID. Is this possible to do with just SqlDataSources?
The detailed version:
I have two tables: Company and Address. The column Company.AddressId can be null. On my ascx page I am using a SqlDataSource to select a left join of company and address and a GridView to display the results. By having my UpdateCommand and DeleteCommand of the SqlDataSource execute two statements separated by a semi-colon I am able to use the GridView's Edit and Delete functionality to update both table simultaneously.
The problem I have is when the Company.AddressId is null. What I need to have happen is have the data source create a record in the Address table and then update the Company table with the new Address.ID then proceed with the update as usual. I would like to do this with just data sources if possible for consistency/simplicity sake. Is it possible to have my data source do this, or perhaps add a second data source to the page to handle some of this?
Once I have that working I can probably figure out how to make it work with the InsertCommand as well but if you are on a roll and have an answer for how to make that fly as well feel free to provide it.
Thanks.
execute two statements separated by a
semi-colon
I don't see any reason why it wouldn't be possible to do both an INSERT and UPDATE in two statements with SqlDataSource just like you are doing here.
However, just so you know, if you have a lot of traffic or users using the application at the same time, you can run into concurrently issues where one user does something that affects another user and unexpected results can cascade and mess up your data. In general, for things like what you are doing - INSERT and UPDATE involving primary or foreign keys, usually SQL TRANSACTIONs are used. But, you must execute them as SQL stored procedures (or functions), on your SQL database. You are still able to call them from your SqlDataSource however by simply telling it that you are calling a stored procedure.

Resources