rwarcup
asked on
How do I pass a variable to a sqldatasource selectparameter?
Hi Experts,
I'm new to .Net 2.0. How do I pass a variable encapsulated in <% %> to a sqldatasource select parameter?
I have a table 'tblUser' that has one column - 'UserName' and contains one row, the content being user3.
When I run the following code it selects no rows. So I'm doing something wrong trying to pass a variable using DefaultValue="<%=UName%>" . The select works ok when I use DefaultValue="user3".
Any ideas?
Thanks-in-advance
Rob
<form id="form1" runat="server" enctype="multipart/form-da ta">
<% Dim UName As String = "user3"%>
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="UserName"
DataSourceID="SqlDataSourc e1">
<Fields>
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Database Connection String %>"
SelectCommand="SELECT [UserName] FROM [tblUser] WHERE ([UserName] = @UName)">
<SelectParameters>
<asp:Parameter Name="UName" DefaultValue="<%=UName%>" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
I'm new to .Net 2.0. How do I pass a variable encapsulated in <% %> to a sqldatasource select parameter?
I have a table 'tblUser' that has one column - 'UserName' and contains one row, the content being user3.
When I run the following code it selects no rows. So I'm doing something wrong trying to pass a variable using DefaultValue="<%=UName%>" . The select works ok when I use DefaultValue="user3".
Any ideas?
Thanks-in-advance
Rob
<form id="form1" runat="server" enctype="multipart/form-da
<% Dim UName As String = "user3"%>
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="UserName"
DataSourceID="SqlDataSourc
<Fields>
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Database
SelectCommand="SELECT [UserName] FROM [tblUser] WHERE ([UserName] = @UName)">
<SelectParameters>
<asp:Parameter Name="UName" DefaultValue="<%=UName%>" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've used a cookieparameter. Here's the code I've used:
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim NameCookie As New HttpCookie("UserName", "user3")
NameCookie.Expires = Now.AddMinutes(5)
Response.Cookies.Add(NameC
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<form id="form1" runat="server" enctype="multipart/form-da
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="UserName"
DataSourceID="SqlDataSourc
<Fields>
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Database
SelectCommand="SELECT [UserName] FROM [tblUser] WHERE ([UserName] = @UName)">
<SelectParameters>
<asp:CookieParameter CookieName="UserName" Name="UName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>