ASPX SelectParameters From aspx.vb Code Behind - asp.net

I am using ASP Classic 2.0 so I know some of this code is a bit outdated but it is what I inherited. I am trying to use a variable from my backing aspx.vb page in the select query.
I have the following query (truncated)
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="Data Source=.;Initial Catalog=dataSQL;Persist Security Info=True;User ID=user;Password=$$$$"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT ClID, Client_Name
FROM [tblClient]
WHERE (tblClient.ClID = #ClID )"
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="ClID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
I want to replace the ControlParameter with a variable from the aspx.vb that backs this aspx page.
I know I have access to the variable because <%=ClID%> produces the correct ID but adding something .Add("ClID", DbType.Guid, clID) does not work.
I am guessing there is an easy way to do this.
If it needs to be a control I think I can bind ClID to the a form control but I cannot figure out the correct syntax for that.
Thanks,
Steve

First, for the <%=CLID%> remark remember that <%= is just shorthand for Response.Write(), meaning the result is only written into the final html rendered to the browser. It does not and never will insert values into aspx markup.
The .Add() code, however, is on the right track, but we still need to make two adjustments.
First, in the aspx markup use an <asp:Parameter instead of <asp:ControlParameter.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="Data Source=.;Initial Catalog=dataSQL;Persist Security Info=True;User ID=user;Password=$$$$"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT ClID, Client_Name
FROM [tblClient]
WHERE tblClient.ClID = #ClID"
<SelectParameters>
<asp:Parameter Name="ClID" DbType="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Then, in the aspx.vb code-behind instead of .Add() you must remember the parameter already exists. If you try to add it again you'll get an exception. Instead, you need to set the Value for the existing parameter in your code-behind:
SqlDataSource1.SelectCommand.Parameters["ClID"].Value = ClID
It's been a loooong time since I've used code like this, so you may also need to call .DataBind()

Related

Use Connection Strings in the aspx page from the MasterPage

I have had to implement a custom connection string handler for my asp.net project.
They are stored normally in the web.config file, however, depending on the host header of the page it could be one of 5 connection strings. What I did was build a custom class to look at the host header and store the desired connection string in the master page via a [SESSION_VARIABLE]. In the page's .cs file I can access it with Master.conString, however I have discovered many pages where a GridView is bound to a datasource with sort, paging, edit, and delete enabled.
It would be no small chore to convert them all to the code page and recreate every event in C#, so I would really rather find a way around it.
So how can I take:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:i_v2ConnectionString %>"
SelectCommand="SELECT [id], [Carrier]FROM [Orders_Carriers] ORDER BY [Carrier]"
UpdateCommand="UPDATE [Orders_Carriers] SET Carrier=#Carrier WHERE id=#id"
OnUpdated="OnDSUpdatedHandler"
DeleteCommand="DELETE FROM [Orders_Carriers] WHERE id=#id"
OnDeleting="OnRecordDeleting"
OnDeleted="OnRecordDeleted"
InsertCommand="INSERT INTO [Orders_Carriers] ([Carrier]) VALUES (#Carrier">
</asp:SqlDataSource>
and change
ConnectionString="<%$ ConnectionStrings:i_v2ConnectionString %>"
to use Master.conString instead.
The value stored in Master.conString is a string value "i_v2ConnectionString".
I should add that I get "The ConnectionString property has not been initialized." error if I do:
ConnectionString="<%# Eval(Master.conString) %>"

Why does SharePoint Designer 2010 overwrite my SQLDataSource?

I have a SharePoint page that uses a SPSqlDataSource to get data from SQL Server. I noticed that an existing SharePoint page with similar function had much better performance, and it used a SqlDataSource.
I replaced the SPSqlDataSource with a SqlDataSource, saved my changes, tested the results, and found them superior to the SPSqlDataSouce. I closed SharePoint Designer, and then things seemed to slow a bit. I opened up my file and was surprised to find my updates had been overwritten, and the original SPSqlDataSouce there.
How did my changes get overwritten? How do I make my change persistent?
<SharePoint:SPSqlDataSource runat="server" AllowIntegratedSecurity="False" ConnectionString="Data Source=dbserver;
User ID=XXXXX;
Password=XXXXX;
Initial Catalog=XXXXX;
" ProviderName="System.Data.SqlClient" SelectCommand="SELECT ... FROM ... WHERE... " ID="BlueFolderPartsConnection1">
<SelectParameters>
<WebPartPages:DataFormParameter ParameterKey="AllParam" PropertyName="ParameterValues" DefaultValue="*" Name="AllParam">
</WebPartPages:DataFormParameter>
</SelectParameters>
</SharePoint:SPSqlDataSource>
I changed it to this:
<asp:SqlDataSource
ID="BlueFolderPartsConnection1"
runat="server"
__designer:commandsync="true"
ProviderName="System.Data.SqlClient"
ConnectionString="XXXXX;
User ID=XXXXX;
Password=XXXXX;
Initial Catalog=XXXXX;
" SelectCommand="SELECT ... FROM ... WHERE... ">
<SelectParameters>
<WebPartPages:DataFormParameter ParameterKey="AllParam" PropertyName="ParameterValues" DefaultValue="*" Name="AllParam">
</WebPartPages:DataFormParameter>
</SelectParameters>
</asp:SqlDataSource>
__designer:commandsync="true"
Set this property to "false" or remove it from the tag altogether.

Select Parameter Anomaly

This is driving me crazy, and even though it doesn't really matter right now I'd like to know what's going on.
I have a Stored Procedure called ClientSelect:
SELECT * FROM dbo.Client
That's all that's in it (I've stripped it right back to try and figure out what's going on!)
I also have an ASP.NET page with a GridView. Its DataSource looks like this:
<asp:SqlDataSource ID="SQLClient" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
SelectCommand="EXEC ClientSelect" >
<SelectParameters>
<asp:Parameter Name="ClientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Note that the SelectCommand does not make any reference to the SelectParameter, and the Stored Procedure does not ask for any parameters. This page loads, but the GridView is empty - no data returned.
However, if I remove the SelectParameter completely, or add a default value so that it looks like:
<asp:Parameter Name="ClientID" Type="Int32" DefaultValue="1" />
It returns all the records in the table (not filtering by the specified default value).
Why is the explicit coding of a SelectParameter causing no data to be returned, even though neither my SelectCommand or my Stored Procedure are making any reference to it at all? It doesn't make sense to me!
can you please try this ??
<asp:SqlDataSource ID="SQLClient" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
SelectCommand="ClientSelect" SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:Parameter Name="ClientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Well, I'm not a specialist about ASP.NET, but I see, that if your SP doesn't have any parameters, but still you specify them on the query - it should throw an exception, unless you've turned this kind of exceptions (SQL SERVER) off.
Besides, if there is a parameter specified, but no value applied, it also causes an error, because normally any code is transformed by the driver you access the SQL Server into the T-SQL code. So in the first time you will get this:
EXEC ClientSelect #ClietntId=
And next one is that
EXEC ClientSelect #ClietntId=1
which is ok.
Well, people say if there are parameters specified, that are not exist, they are ignored. DefaultValue should be. Otherwise its better to try code #Dhaval suggested and give a feedback.
I think you need to specify a way to get the actual value for the defined parameter.
For example; if you are getting parameter value from a session variable you have to define your SelectParameter as
<!-- To get ClientID from a session variable called myClientId -->
<asp:SessionParameter Name="ClientID" DbType="Int16"
DefaultValue="0" SessionField="myClientId"/>
<!-- To Get ClientID from a dropdown List called ddlClients -->
<asp:ControlParameter Name="ClientID" DbType="Int16"
DefaultValue="0" ControlID="ddlClients"/>
<!-- YOUR CODE -->
<asp:SqlDataSource ID="SQLClient" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
SelectCommand="ClientSelect" SelectCommandType="StoredProcedure" >
<SelectParameters>
<!-- Add correct parameter type as appropriate -->
</SelectParameters>
</asp:SqlDataSource>

asp:QueryStringParameter and empty query string parameter

I haveasp:GridView displaying client requests using asp:SqlDataSource. I want to limit displayed information by client:
View.aspx has to display everything, View.aspx?client=1 has to display only requests from client ID #1.
So I'm using <asp:QueryStringParameter Name="client" QueryStringField="client" /> for query "EXEC getRequests #client".
Everything works properly when some client is specified. But don't - if not.
I tested my SP using SSMS - it works properly in both cases - when parameter is specified and when it isn't (NULL passed explicitly).
What have I do?
SqlDataSource won't fire if any of it's parameters are null, unless you specify otherwise:
<asp:SqlDataSource CancelSelectOnNullParameter="False" />
It might also be necessary to add a null default value to your querystring parameter:
<asp:QueryStringParameter Name="client" QueryStringField="client" DefaultValue="" ConvertEmptyStringToNull="True" />
You need to define a Default value to the parameter for those situations, for example:
<asp:QueryStringParameter Name="client" QueryStringField="client" DefaultValue="0"/>
and then in the SP you need verify if the client is 0, return all the clients, otherwise the specific one.

Incorrect syntax near 'nvarchar' in update ASP.NET Gridview

Very simple question but all the answer I read over the web doesn't apply.
I try to do an update on a ASP.NET Gridview but when I click on update, I get this error:
Incorrect Syntax near 'nvarchar'. The scalar variable #intID must be declare.
Here is my datasource. I guess the problem come from here but I can't see where...
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connexionDev %>"
DeleteCommand="DELETE FROM [tbl_Bug] WHERE intID = #intID"
SelectCommand="SELECT [intID],[strTitre],[strDescription],[intStatus_FK],[intType_FK],[intSeriousness_FK] FROM [tbl_Bug]"
UpdateCommand="UPDATE [tbl_Bug] SET [strTitre] = #strTitre ,[strDescription] = #strDescription ,[intStatus_FK] = #intStatus_FK ,[intType_FK] = #intType_FK ,[intSeriousness_FK] = #intSeriousness_FK WHERE [intID] = #intID">
<DeleteParameters>
<asp:Parameter Name="intID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="intID" Type="Int32" />
<asp:Parameter Name="strTitre" Type="String" />
<asp:Parameter Name="strDescription" Type="String" />
<asp:Parameter Name="intStatus_FK" Type="Int32" />
<asp:Parameter Name="intType_FK" Type="Int32" />
<asp:Parameter Name="intSeriousness_FK" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Thanks for your help in advance!
EDIT - EDIT - EDIT
Well, I wanted to use SQL Profiler but it seems that it's not in my version (SQL server 2008 Express) so I tried another sql profiler that is open source but I never understood how it worked and it was always crashing ...
Is there any other way to know the query that are used so I can track my problem?
Hi everyone and thanks for your help
My query was good, it was the binding in the GridView that was bad, for a very very simple detail
This will work: Text='<%# Bind("myValue") %>'
While this wont: Text='<%# Bind("[myValue]") %>'
So watch out! :) Now everything is working!
Thanks again!
In cases like this the answer is often to run SQL Profiler and see what SQL is being sent, it's often a mismatch in variable names or something equally simple once you see what being sent to your SQL Server.
You can register an event handler for SqlDataSource.Updating event - it will be raised when the command is prepared, and before executing it. The command will be a DbCommand object, which can be retrieved from an instance of SqlDataSourceCommandEventArgs that your event handler will get via its Command property. Dump both the text, and the actual names and values of all command parameters (in DbCommand.Parameters collection), and check it for errors.
I think you need <SelectParameters>...</SelectParameters> section where you declare all your fields.
This is always the case when you have spaces in your field/column names - rename the DB columns and remove spaces - it will work .
If you look at the SQL server monitor and inspect the generated SQL code for Update - it is messed up due to spaces in column names.

Resources