Solved

How do I pass a variable to a sqldatasource selectparameter?

Posted on 2006-11-05
2
1,036 Views
Last Modified: 2012-06-21
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>
0
Comment
Question by:rwarcup
2 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 125 total points
Comment Utility
When using the objectDataSource you can use the following parameters
            <asp:ControlParameter />
            <asp:CookieParameter />
            <asp:FormParameter />
            <asp:Parameter />
            <asp:ProfileParameter />
            <asp:QueryStringParameter />
            <asp:SessionParameter />
I would suggest using the <asp:formParamter />
and store the username in a hidden field in the form.  If you don't want to store the name in such an easily hackable field, then create a cookie and store the username in the cookie, and use the <asp:CookieParameter/>
0
 

Author Comment

by:rwarcup
Comment Utility
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>
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now