Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I retrieve a field for comparison with sqldatasource ?

Posted on 2007-11-15
3
Medium Priority
?
856 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 2000 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

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

927 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