?
Solved

What is wrong with this code?

Posted on 2005-04-17
4
Medium Priority
?
211 Views
Last Modified: 2010-04-17
I have this code:

        Dim oConnection As New Data.SqlClient.SqlConnection("Data Source=****;User Id=****;Password=****;Initial Catalog=****;")
        oConnection.Open()
        'First, get userID from HTTPCONTEXT.
        Dim UserName As String
        UserName = HttpContext.Current.User.Identity.Name()
        Dim users As New Users
        Dim UserID As String
        UserID = users.GetUserID(UserName)
        Me.Label1.Text = UserID
        Dim strSQL As String
        strSQL = "SELECT ApplicationID, ApplicationName FROM KY_Applications WHERE UserID=@UserID"
        Dim cmd As New SqlClient.SqlCommand
        cmd.CommandText = strSQL
        cmd.Parameters.Add("@UserID", UserID)
        Dim da As New SqlClient.SqlDataAdapter
        da.SelectCommand = cmd
        Dim ds As New DataSet
        da.Fill(ds)
        Me.ddlApplications.DataSource = ds.Tables(0)
        Me.ddlApplications.DataBind()

I am trying to databind a list of applications that a user has access to onto the drop-down list on my .aspx page. I want to just have the application names from KY_Applications where the USERID matches the UserName that the user logged in with. The code to get the current user's userID is below:

        Dim oConnection As New Data.SqlClient.SqlConnection("Data Source=****;User Id=****;Password=****;Initial Catalog=*****;")
        oConnection.Open()

        Dim strSQL As String
        strSQL = "SELECT UserID FROM KY_Users WHERE UserName = @UserName"

        Dim oCommand As New Data.SqlClient.SqlCommand(strSQL, oConnection)
        oCommand.Parameters.Add("@UserName", UserName)
        Dim UserID As String
        UserID = CStr(oCommand.ExecuteScalar())
        Return UserID
        oConnection.Close()
        oConnection.Dispose()
        oCommand.Dispose()

What is wrong with either of these code snippets, because I keep getting an error (and I can't tell the exact error because It won't let me see error messages on a remote server).

Any help would be appreciated. Thanks!
0
Comment
Question by:itprochris
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
NetworkArchitek earned 400 total points
ID: 13802324
Well for instance, this won't work:

strSQL = "SELECT ApplicationID, ApplicationName FROM KY_Applications WHERE UserID=@UserID"

You don't use commandtext AND a stored procedure, so if you have a stored procedure the commandtext is the name of the procedure. But if you want to use that SQL you need to change that to:

strSQL = "SELECT ApplicationID, ApplicationName FROM KY_Applications WHERE UserID= " & "'" & UserID & "'"

Before you were just passing "@UserID" with no correlation to the variable named UserID.

0
 

Author Comment

by:itprochris
ID: 13802689
Does my function to get the user's ID based on the current user that is connected look okay? I have no way of debugging because I am not hosting locally.
0
 
LVL 4

Expert Comment

by:Diane258
ID: 13803712
is there anyway you can get to the host?

or have someone tell you the error that the host is generating?
0
 

Author Comment

by:itprochris
ID: 13805304
I found what I needed, but I'll still give you the points if you want:-D
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Progress
Screencast - Getting to Know the Pipeline

807 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