Southern_Gentleman
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
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().
If the type of OrderID is numeric ,convert the value into int. If the type of orderId is varchar then add ToString().
>SqlCommand.Parameters.Add WithValue( "@OrderID" , Request.QueryString("PaymentAmount"))
Are you sure you are looking at the right query string parameter?
Are you sure you are looking at the right query string parameter?
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.