Solved

Display field contents using ASP.NET

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now