Solved

How do I retrieve a field for comparison with sqldatasource ?

Posted on 2007-11-15
3
821 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

17 Experts available now in Live!

Get 1:1 Help Now