Link to home
Start Free TrialLog in
Avatar of rwarcup
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-data">
<%  Dim UName As String = "user3"%>
<div>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="UserName"
            DataSourceID="SqlDataSource1">
            <Fields>
                <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
            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
Avatar of MogalManic
MogalManic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwarcup
rwarcup

ASKER

Thanks MogalManic,

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(NameCookie)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="UserName"
            DataSourceID="SqlDataSource1">
            <Fields>
                <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
            SelectCommand="SELECT [UserName] FROM [tblUser] WHERE ([UserName] = @UName)">
            <SelectParameters>
                <asp:CookieParameter CookieName="UserName" Name="UName" Type="String" />
            </SelectParameters>    
        </asp:SqlDataSource>
   </div>
    </form>
</body>
</html>