Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Display field contents using ASP.NET

Posted on 2014-01-17
4
Medium Priority
?
276 Views
Last Modified: 2014-01-20
Still trying to get my head around the basics of ASP.NET at the moment but I have a question about querying a database for a field value and it's bugging me somewhat so hopefully someone a lot smarter than me can help.

I'm looking to obtain the First Name of a person based upon a query lookup to the database using the persons user id but can't seem to get it to work or display on my ASP.NET page.

Here is my code behind function
Public Function UserDetails(ByVal UserID As String) As String
        Dim FirstName As String = ""
        Dim strConnection As String = ConfigurationManager.ConnectionStrings("aspnet-WebSite1-13bb7ef4-4736-49de-9eb7-9b2d7393640dConnectionString1").ConnectionString
        Dim SQL As String = "SELECT dbo.AspNetUsers.FirstName FROM dbo.AspNetUsers WHERE ([Id] = @NewUserID)"

        Using conn As New SqlConnection(strConnection)
            Dim cmd As New SqlCommand(SQL, conn)
            cmd.Parameters.AddWithValue("@NewUserID", FirstName)

            conn.Open()
            FirstName = Convert.ToString(cmd.ExecuteScalar())

        End Using

        Return FirstName
    End Function

Open in new window


and I call this on my aspx page using
<%: UserDetails(User.Identity.GetUserId()) %>

Open in new window


but alas I don't get anything displayed on my page.

Anyone able to offer some assistance to this dullard lol

Thanx guys
0
Comment
Question by:Steven O'Neill
[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
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39788374
Try it as:
<%= UserDetails(User.Identity.GetUserId()) %>

Open in new window

0
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 1000 total points
ID: 39792735
You're setting the wrong parameter to pass into the sql command:

cmd.Parameters.AddWithValue("@NewUserID", FirstName)


You WANT the firstname, you need to pass in the target user id:
cmd.Parameters.AddWithValue("@NewUserID", UserID)
0
 
LVL 2

Assisted Solution

by:Aijaz Chauhan
Aijaz Chauhan earned 1000 total points
ID: 39793405
You need to do this.

Using conn As New SqlConnection(strConnection)
            Dim cmd As New SqlCommand(SQL, conn)
            cmd.Parameters.AddWithValue("@NewUserID", UserID)

            conn.Open()
            FirstName = Convert.ToString(cmd.ExecuteScalar())

        End Using

put a debug point on "FirstName = Convert.ToString(cmd.ExecuteScalar())". See what result comes up.
0
 
LVL 2

Author Comment

by:Steven O'Neill
ID: 39793670
Hi all

Thanx for getting back to me on this. I got to this point on Friday after posting this and it appears to have worked but the methods provided above seem better to me:

Using conn As New SqlConnection(strConnection)
            Dim cmd As New SqlCommand(SQL, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.Add("@NewUserID", SqlDbType.NVarChar).Value = User.Identity.GetUserId()

            conn.Open()
            FirstName = Convert.ToString(cmd.ExecuteScalar())

        End Using

Open in new window

Thanx again everyone
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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

688 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