Solved

How do I pass a variable to a sqldatasource selectparameter?

Posted on 2006-11-05
2
1,049 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 125 total points
ID: 17876356
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
ID: 17877363
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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