Solved

Display field contents using ASP.NET

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Exceptions 3 46
Message not shown 5 52
List<PaisEntity> - show some 1 24
insert value of checklistbox checked 4 27
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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