Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP (http://www.go4expert.com/forums/asp/)
-   -   GridView Troubles (http://www.go4expert.com/forums/gridview-troubles-t4007/)

CaJack 25Apr2007 02:43

GridView Troubles
 
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.

pradeep 25Apr2007 10:12

Re: GridView Troubles
 
I guess there's a problem with your SQL query, now your query looks like this
Code: SQL

SELECT * FROM Table1 WHERE UserName = Rachel


Where as it should look like this,
Code: SQL

SELECT * FROM Table1 WHERE UserName = 'Rachel'



Solution:
Code:

DBAdap = New SqlDataAdapter("SELECT * FROM Table1 WHERE UserName = '" & myString &"'", DBConn)

CaJack 25Apr2007 16:15

Re: GridView Troubles
 
Thank you so much, thats fixed it and it's working fine now.


All times are GMT +5.5. The time now is 13:03.