[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I pass a variable to a sqldatasource selectparameter?

Posted on 2006-11-05
2
Medium Priority
?
1,056 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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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