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
Solved

Display field contents using ASP.NET

Posted on 2014-01-17
4
265 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
4 Comments
 
LVL 52

Expert Comment

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

Open in new window

0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 250 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 250 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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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