I want to using a variable from the GET (page.aspx?var=val) in the Select statement using
I tried the ":" i'm getting error : Oracle.DataAccess.Client.OracleException: ORA-01008: not all variables bound
and when using "#" 'm getting this : Oracle.DataAccess.Client.OracleException: ORA-00936: missing expression
<asp:SqlDataSource
ID="SqlDataSource1" Runat="server"
SelectCommand="SELECT * from table where field=:var"
ConnectionString="<%$ ConnectionStrings:ProdConnectionString%>"
ProviderName="Oracle.DataAccess.Client" />
<SelectParameters>
<asp:QueryStringParameter Name="var" QueryStringField="var" DefaultValue = "0" />
</SelectParameters>
</asp:SqlDataSource>
Related
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GSProjectConnectionString %>" SelectCommand="SELECT * FROM [Table] where Owner like '%#username%' ">
<SelectParameters>
<asp:Parameter Name="username" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
The above is my code wherein i am trying to use a variable username which is defined in .cs file.
It does not take the value of username.
Where am I going wrong ?
Any idea anyone ?
change it as
SelectCommand="SELECT * FROM [Table] where Owner like #username
when you set parameter value you can add the '%%' as below
SqlDataSource1.SelectParameters["username"].DefaultValue = string.Format("%{0}%", paramvalue);
I have a SQL table named doctor search and have 2 dropdownlists one shows expertise of the doctor and i want to fill dropdownlist 2 with the information of those doctor which are related to that expertise section.
This can be done easily if you are using the designer interface.
Try using the below code after making necessary changes (Connection String, Table name and Column name). If that doesn't work, I will share the steps
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [Expertise] FROM [Your Table]"></asp:SqlDataSource>
<asp:DropDownList ID="ddlExpertise" runat="server" DataSourceID="SqlDataSource1" DataTextField="Expertise" DataValueField="Expertise">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [DoctorDetails] FROM [Your Table] WHERE ([Expertise] = #Expertise)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlExpertise" DefaultValue="" Name="Expertise" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddlDoctorDetails" runat="server" DataSourceID="SqlDataSource2" DataTextField="DoctorDetails" DataValueField="DoctorDetails">
</asp:DropDownList>
I have the sqldatasource in aspx page and in the query I want to use one parameter i.e. coming from session.
Below is my code.Please help me out.
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id="Here I want to use session variable"" />
Try this
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id=#SessionVar">
<SelectParameters>
<asp:SessionParameter Name="SessionVar" SessionField="SessionVariableName" ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
This MSDN article should get you what you need. Basically you would define your SelectCommand with the parameter placeholder, "?", and then define your SelectParameters collection with an entry for your SessionParameter.
Using parameters is rather simple:
<asp:SqlDataSource id="Employees" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind%>"
SelectCommand="SELECT LastName FROM Employees WHERE Title = #Title">
<SelectParameters>
<asp:ControlParameter Name="Title"
ControlID="DropDownList1"
PropertyName="SelectedValue"/>
</SelectParameters>
</asp:sqldatasource>
Just replace the value of parameter with your variable:
<%= Sessiom[variable_name] %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=LAZY-PC;Initial Catalog=Test;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [C] WHERE ([C#] = #column1)">
<SelectParameters>
<asp:SessionParameter Name="column1" SessionField="id" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Johnny_D's answer is pretty much spot on, but I'd like to point out that there's a SessionParameter class that you can use for this:
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id= ?" />
<SelectParameters>
<asp:SessionParameter
Name="id"
SessionField="SessionVariableName"
DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
I have a sqldatasource, in select command i have #status parameter. The parameter take the value from the textbox at runtime.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details] WHERE ([status] = #status)"
FilterExpression="title='{4}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
my problem is that when i run the page without entring the parameter in text box sqldatasource is not returing any row.
Looking at the documentation on MSDN, you have to alter how you have setup the SqlDataSource.
Try this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details]"
FilterExpression="title='{0}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
I have removed the Where clause from the query as this will be applied by the filter expression. Also, I have changed the filter expression from title='{4}' to title='{0}'. The documentation states that the number is a placeholder to an item in the FilterParameters collection.
Updated
I have also changed the SelectParameters to FilterParameters
Update 2
I have created a working example to finish of this answer. This will filter the Title column using the text from the text box. If this text box is empty it will return all the rows from the table (a scary thought but OK for this example). It is querying the AdventureWorks database for which I set a connection string called AWorks.
<asp:SqlDataSource ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:AWorks %>"
SelectCommand="SELECT ContactId, Title, FirstName, LastName FROM Person.Contact"
FilterExpression="Title='{0}'"
runat="server">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlID="txtTitle" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
<asp:TextBox runat="server" Id="txtTitle"></asp:TextBox>
<asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />
<asp:GridView
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:BoundField Visible="false" DataField="ContactId"></asp:BoundField>
<asp:BoundField Visible="true" DataField="Title"></asp:BoundField>
<asp:BoundField Visible="true" DataField="FirstName"></asp:BoundField>
<asp:BoundField Visible="true" DataField="LastName"></asp:BoundField>
</Columns>
</asp:GridView>
try a condition like this:
(#Status is null or #Status ='' Or Status = #Status)
set ConvertEmptyStringToNull="true" and then try....
Add CancelSelectOnNullParameter="false" to your SqlDataSource.
I have a database connection which takes an input from the querystring to access the appropriate data. However i have now upgraded things by encoding this data. As a result i now need to run the QueryString value through a function to unencode it.
At present i have this code for the DataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:spareathoughtConnectionString %>"
SelectCommand="campaign_Statistics" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="tmp_Campaign"
QueryStringField="camp" Type="Int64" />
</SelectParameters>
</asp:SqlDataSource>
The QueryString value is 'camp'.
In my code behind i would process this value via the following code;
Convert.ToInt64(HttpUtility.UrlDecode(TamperProofQueryString.decode(Request.QueryString["camp"])))
So, how can incorporate the above line of code into the datasource? ie i need to effectively replace 'camp' with 'Convert.ToInt64(HttpUtility.UrlDecode(TamperProofQueryString.decode(Request.QueryString["camp"])))'
I hope this makes sense?
Thanks
Change it to a plain <asp:Parameter rather than a <asp:QueryStringParameter. Then handle the OnSelecting event for the datasource. You should be able to set the parameter value for the SqlCommand in your code-behind there.
In the aspx markup:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:spareathoughtConnectionString %>"
SelectCommand="campaign_Statistics" SelectCommandType="StoredProcedure"
OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="tmp_Campaign" Type="Int64" />
</SelectParameters>
</asp:SqlDataSource>
In the code-behind:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["#tmp_Campaign"].Value = Convert.ToInt64(TamperProofQueryString.decode(HttpUtility.UrlDecode(Request.QueryString["camp"])));
}
Also, looking at that, shouldn't you UrlDecode before the TamperProof decode?
If you want to do all of it without a codebehind, you could do something like:
<asp:SqlDataSource
ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:spareathoughtConnectionString %>"
SelectCommand="campaign_Statistics" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
<%
SqlDataSource1.SelectParameters.Add(
"tmp_Campaign",
Convert.ToString(HttpUtility.UrlDecode(TamperProofQueryString.decode(Request.QueryString["camp"]))));
%>