Solved

How do I pass a variable to a sqldatasource selectparameter?

Posted on 2006-11-05
2
1,042 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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