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) %>"
Related
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()
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.
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>
I'm required to print a value of a string which within a resource files (.resx) within asp.net tags
i used the following approaches
for example :
<ext:ComboBox ID="cmbProgram" runat="server" FieldLabel="<%= Resource.StringName %>"
<ext:ComboBox ID="cmbProgram" runat="server" FieldLabel="<%= Response.Write(Resource.StringName) %>"
But both approaches didin't provide the value of the string, but provides the ID. How to get the values within the ASP.NET tags?
Assuming you have Control.ascx:
<ext:Tag runat="server" meta:resourcekey="fooBar" />
Then put Controls.ascx.resx in App_LocalResources containing appropriate strings:
fooBar.PropetyName = "x"
I have an asp.net page that has several SqlDataSources defined that feed data into some graphs. The graph product does not handle "no data" gracefully, and throws an exception. I'd like this to handle the situation -- so I need to check whether the SqlDataSource returned data before rendering the graph (and if not, just post a message saying "No Data" or something).
Is there an easy way to check if the data source returned data, and do this if/then without a bunch of code behind?
The following is taken from devcurry, which is pretty much what you are looking for.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName],
[ContactTitle], [Address] FROM [Customers]"
onselected="SqlDataSource1_Selected">
</asp:SqlDataSource>
And in code behind:
Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
If e.AffectedRows < 1 Then
' perform action
End If
End Sub
try this
http://www.devcurry.com/2009/02/how-do-you-check-if-sqldatasource.html
i hope if it helps you ..