Solved

How do I retrieve a field for comparison with sqldatasource ?

Posted on 2007-11-15
3
824 Views
Last Modified: 2008-02-01
I have placed an sqldatasouce on my page and set the datasource mode to datareader....

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NewUserList %>"
                        SelectCommand="SELECT [UserID] FROM [tblIDList] WHERE ([UserID] = @UserID)" DataSourceMode="DataReader">
                        <SelectParameters>
                            <asp:ProfileParameter Name="UserID" PropertyName="UserName" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
I want to compare the userid to a text field to see if they match.  I can not figure out how to access the datareader  part of the sqldatasource.  Can someone point me in the right direction?  I am using vs2005 and visual basic.

I have tried this but get error...

Dim sdata As String
        Dim reader As SqlDataReader = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), SqlDataReader)
        If reader.Read() Then
            sdata = reader("UserID").ToString
        End If
Thanks,
John
0
Comment
Question by:leachj
  • 2
3 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 20291665
John,
It looks like you're trying to return just one value from the query...and you don't intend to use the sqldatasource for binding to anything...in which case, I'd recommend you do it all in codebehind and use executescalar...
http://www.aspfree.com/c/a/ASP-Code/Using-ExecuteScalar-Method-without-Stored-Procedures/
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 20291886
try something like this:
    Private Function CompareUserName() As Boolean

        Dim MySQLStatement As String = "SELECT [UserID] FROM [tblIDList] WHERE ([UserID] = @UserID)"

        Dim ProfileUserName As String = Profile.UserName

        Dim DBUserName As String = String.Empty

        Using MyConn As New SqlConnection(MyConnString)

            MyConn.Open()

            Dim MyCmd As New SqlCommand(MySQLStatement, MyConn)

            MyCmd.Parameters.Add(New SqlParameter("@UserID", ProfileUserName))

            Try

                DBUserName = MyCmd.ExecuteScalar

            Catch ex As Exception

                Response.Write("Error with db check: " & ex.Message.ToString)

            End Try

        End Using

        Try

            If DBUserName = ProfileUserName Then

                Return True

            Else

                Return False

            End If

        Catch ex As Exception

            Response.Write("Error with comparison: " & ex.Message.ToString)

        End Try

    End Function

Open in new window

0
 

Author Comment

by:leachj
ID: 20299126
Ok, I finally got this to work.   I had to change some variable assignments etc.  Interestingly, I was getting a false comparison on DBUserName and ProfileUserName.  For some reason the ID stored in the database had trailing spaces.  I can not find where that is coming from.  It may have to do with the field definition when I created the table.  I used Trim() function on DBUserName after retrieving it and it works ok.

Thanks,
John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

24 Experts available now in Live!

Get 1:1 Help Now