Solved

How do I retrieve a field for comparison with sqldatasource ?

Posted on 2007-11-15
3
833 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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