Link to home
Start Free TrialLog in
Avatar of Southern_Gentleman
Southern_GentlemanFlag for United States of America

asked on

Databind Label to SQL Dataconnection and QueryString

I'm working on just databinding my Label to my SQL connection. I want to use my querystring to filter out my data. Can't seem to get this right. Probably simple fix.
Public Class DatabindLabel
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Using sqlConnection As New SqlConnection("...")
            sqlConnection.Open()
            Dim con As New SqlConnection("Data Source=WEB-PC\GL;Initial Catalog=GL;Persist Security Info=True;User ID=Web;Password=Password!")
            Dim strSQL As String = "select PaymentAmount from Orders Where ([OrderID]= @OrderID)"
            Using SqlCommand As New SqlCommand(strSQL, sqlConnection)
                SqlCommand.Parameters.AddWithValue("@OrderID", Request.QueryString("PaymentAmount"))
                Label2.Text = SqlCommand.ExecuteScalar() & ""
            End Using

        End Using
    End Sub


End Class

Open in new window

Avatar of Brijesh Gandhi
Brijesh Gandhi
Flag of India image

I have checked your code and it works perfectly for me. I don't find any error.so make sure value passing to command is perfect and check out QueryString value. And make sure out table and its field name..
Hi ,

         If the type of OrderID is numeric ,convert the value into int. If the type of orderId is varchar then add ToString().
Avatar of Nasir Razzaq
>SqlCommand.Parameters.AddWithValue("@OrderID", Request.QueryString("PaymentAmount"))

Are you sure you are looking at the right query string parameter?
Avatar of Southern_Gentleman

ASKER

Ok, I changed the query string parameter since I realized that I'm looking up the OrderID. I also changed the As String to Integer. Still not getting any results. It comes up as blank or null value.


Public Class DatabindLabel
    Inherits System.Web.UI.Page
    Private OrderID As Integer

    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Using sqlConnection As New SqlConnection("...")
            sqlConnection.Open()
            Dim con As New SqlConnection("Data Source=WEB-PC\GL;Initial Catalog=GL;Persist Security Info=True;User ID=Web;Password=Password1")
            Dim strSQL As Integer = "select PaymentAmount from Orders Where ([OrderID]= @OrderID)"
            Using SqlCommand As New SqlCommand(strSQL, sqlConnection)
                SqlCommand.Parameters.AddWithValue("@OrderID", Request.QueryString("OrderID"))
                Label2.Text = SqlCommand.ExecuteScalar() & ""
            End Using

        End Using
    End Sub


End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial