Solved

Display field contents using ASP.NET

Posted on 2014-01-17
4
266 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

Technology Partners: 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

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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