<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);
Related
<asp:SqlDataSource ID="UsersUsernameSQL" runat="server"
ConnectionString="<%$ ConnectionStrings:UserQueries %>"
ProviderName="<%$ ConnectionStrings:UserQueries.ProviderName %>"
SelectCommand="SELECT "FIRSTNAME" FROM "USERS" WHERE ("USERNAME" = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="UsersNameLabel" Name="USERNAME" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
This is my code. My query is that I want to select a name where the username equals with the result of the other SqlDataSource (this one works fine, I checked it)
So can I set the result of the datasource as a parameter? And if yes, then how?
I am trying to Parameterize the from statement of an asp:SqlDatasource Select Command. My current command is hard coded:
<asp:SqlDataSource ID="sdsSPs" runat="server"
ConnectionString="<%$ ConnectionStrings:AAPs_DevConnectionString %>"
SelectCommand="select SPECIFIC_NAME from WIN_Dev.INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE = 'Procedure'">
</asp:SqlDataSource>
However I am looking for something more like this:
<asp:SqlDataSource ID="sdsSPs" runat="server"
ConnectionString="<%$ ConnectionStrings:AAPs_DevConnectionString %>"
SelectCommand="select SPECIFIC_NAME from #Path where ROUTINE_TYPE = 'Procedure'">
<SelectParameters>
<asp:ControlParameter ControlID="lblDB" PropertyName="text" Name="Path" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Or even more preferable would be something like this:
<asp:SqlDataSource ID="sdsSPs" runat="server"
ConnectionString="<%$ ConnectionStrings:AAPs_DevConnectionString %>"
SelectCommand="select SPECIFIC_NAME from #Path.INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE = 'Procedure'">
<SelectParameters>
<asp:ControlParameter ControlID="lblDB" PropertyName="text" Name="Path" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
All of these trys have come back failing. There is no way for me to know ahead of time what the DB is as the user can select it. Is there a way to do this? and if so what is it? Thanks for your help!
You may have to write Dynamic Query/Procedure for your requirement,
Create Proc Get_From_Dynamic_Table
#tablename varchar(50)
as
begin
declare #sqlQuery nvarchar(100)
Set #sqlQuery = 'Select * from '+#tablename
exec sp_executesql #sqlQuery
end
Note: Code not tested.
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>
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>
here's the logic i want to perform.
When the textbox is empty, i want the datagrid showing no record.
When the textbox is not empty, then the datagrid will filter the data.
Right now, when the textbox is empty, it shows all the records.
How can i fix that? Thanks in advance!
here's the code block:
<asp:SqlDataSource ID="dsGridview" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT UserName, gender, age FROM users"
FilterExpression="UserName like '%{0}%'">
<FilterParameters>
<asp:ControlParameter Name="UserName" ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsGridview" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT UserName, gender, age FROM users
UserName like '%' +#UserName + '%' and #UserName is not null">
<SelectParameters>
<asp:ControlParameter Name="UserName" ControlID="txtSearch" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
I noticed FilterExpression behaves a little different (it wraps the parameter name or value with brackets, actually it does SQL escaping), so to check #UserName for null it works with SelectParameters.
Just use a RequiredFieldValidator on txtSearch or on the server side use the below check where appropriate. No need to run a query when you don't want any data returned:
if(String.IsNullOrEmpty(txtSearch.Text))
{
//don't databind and use validation to tell the user to enter data
}