GridView Troubles

Discussion in 'ASP' started by CaJack, Apr 24, 2007.

  1. CaJack

    CaJack New Member

    Joined:
    Mar 21, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I’m having trouble with my grid view. When the user is signed into my site and clicks button1, the users name is turned into a string and used in a query to display all the information for that user, but unfortunately it doesn’t work. It should output all the CD’s the user has input into the database. Here’s the code I have…

    Code:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myString As String
             myString = Page.User.Identity.Name.ToString()
    
            Dim DBConn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=""|DataDirectory|\aspnetdb.mdf"";Integrated Security=True;User Instance=True")
    
            Dim DBCmd As New SqlCommand
            Dim DBAdap As New SqlDataAdapter
            Dim DS As New DataSet
            Dim dt As New DataTable
            DBConn.Open()
    
                DBAdap = New SqlDataAdapter("SELECT * FROM Table1 WHERE UserName = " & myString, DBConn)
                
                DBAdap.Fill(dt)
    
                GridView1.DataSource = dt
                GridView1.DataBind()
    
            DBCmd.Dispose()
            DBAdap.Dispose()
            DBConn.Close()
            DBConn = Nothing
        End Sub
    
    When I run it this error comes up “SQLExeption was unhandled by usercode” and it also says “Invalid column name 'Rachel'.”. Rachel is actually the user I’m signed into the site and is the value in myString which is fine, but I don’t see what the problem is. Could it be that UserName is set to NVARCHAR in the sql database? Can someone help me out, I’d really like to get this working. Any advice or help would be great.

    Thanks,
    CaJack

    P.S. English isn’t my first language and I’m sorry if I haven’t explained my problem fully, if I haven’t explained it fully just let me know and I’ll try and explain it better. Thanks for reading.
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    I guess there's a problem with your SQL query, now your query looks like this
    Code:
    SELECT * FROM Table1 WHERE UserName = Rachel
    Where as it should look like this,
    Code:
    SELECT * FROM Table1 WHERE UserName = 'Rachel'

    Solution:
    Code:
    DBAdap = New SqlDataAdapter("SELECT * FROM Table1 WHERE UserName = '" & myString &"'", DBConn)
    
     
  3. CaJack

    CaJack New Member

    Joined:
    Mar 21, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Thank you so much, thats fixed it and it's working fine now.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice