?
Solved

How do I retrieve a field for comparison with sqldatasource ?

Posted on 2007-11-15
3
Medium Priority
?
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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