I´m developing a Web Forms application using VS 2013. In "MyReports.aspx" I want the user to see only the reports that were issued by him/her. In the SqlDataSource SelectParameters I defined the UserID Parameter and then pass the UserId in the codebehind. But, How can I do it directly in the aspx?
I´ve tried adding the Microsoft.AspNet.Identity namespace trough
<%# Import Namespace="Microsoft.AspNet.Identity" %>
And then setting the parameter like:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [ReportsByIssuingUserView] WHERE ([UserID]=#UserID)">
<SelectParameters>
<asp:Parameter Name="UserID" Type="String" DefaultValue="<%: Context.User.Identity.GetUserId() %>"/>
</SelectParameters>
</asp:SqlDataSource>
With no luck...
My code right now is the following and works great:
ASPX:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [ReportsByIssuingUserView] WHERE ([UserID]=#UserID)">
<SelectParameters>
<asp:Parameter Name="UserID" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
CS:
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters["UserID"].DefaultValue = Context.User.Identity.GetUserId();
}
Is there any way to do it without using the codebehind?
Thanks!
Use the <%= Context.User.Identity.GetUserId() %> expression syntax inline in your ASPX page.
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 new to .net/c# and visual studio. I have been looking all over the internet for an answer but couldn't find it. Thanks for your help.
I'm populating a drop down list from a database table, I want to pass a dynamic parameter to the asp server control (the logged in persons username). The 2 areas I want to put this dynamic string I added PUT_LOGGED_IN_USERNAME_HERE".
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="PK_Task" AppendDataBoundItems="true">
<asp:ListItem Selected="True">Supportive</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [PK_Task], [Name] FROM [Task] WHERE ([PointPerson] LIKE '%' + #PointPerson + '%') AND [Status] LIKE 'Done'">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="PUT_LOGGED_IN_USERNAME_HERE" Name="PointPerson"
QueryStringField="PUT_LOGGED_IN_USERNAME_HERE"" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
How do I accomplish this?
I found the code to display username:
<%= Page.User.Identity.Name %>
However it is not working when I use it as shown:
<asp:QueryStringParameter DefaultValue="<%= Page.User.Identity.Name %>" Name="PointPerson"
QueryStringField="<%= Page.User.Identity.Name %>"" Type="String" />
You can access properties of SqlDataSource, including Parameters in code behind, like so:
SqlDataSource1.SelectParameters["PointPerson"].DefaultValue = "User";
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 am trying to populate a data table using the SQL datasource based upon parametrized inputs .The tricky thing is that the value of the parametrized input is available only in the code behind so I am forced to write the SQL datasource in the code behind page
How do I go about it ( I am using C# and ASP.Net 4.0)
The current code in the asp.net page to create the sql datasource connection is :
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LicensingConnectionString %>"
SelectCommand="SELECT * FROM [commissions] where username=#username "></asp:SqlDataSource>
The value I want to use in #username is retrieved in the code behind by this code
IPrincipal p = HttpContext.Current.User;
// p.Identity.Name : this is what we will use to call the stored procedure to get the data and populate it
string username = p.Identity.Name;
Thanks !
You need to handle the OnSelecting event of the data source, and in there you can set the parameter's value.
In your page:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LicensingConnectionString %>"
SelectCommand="SELECT * FROM [commissions] where username=#username"
OnSelecting="SqlDataSource1_Selecting" >
<SelectParameters>
<asp:Parameter Name="username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
In your codebehind:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["#username"].Value = HttpContext.Current.User.Identity.Name;
}
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"]))));
%>