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

asked on

Populate ASP Label from SQL Database

I'm a super nubie to Visual Studio and I've been working on this but I can't seem to retrieve the correct data to show in my ASP Label. I want to use the querystring ("OrderID") in my sql string to retrieve the orderdate field of my table. It keeps populating 12:00AM


If Not IsPostBack Then
            Dim orderdate As Date
            If Not String.IsNullOrEmpty(Request.QueryString("OrderID")) AndAlso Date.TryParse(Request.QueryString("OrderID"), orderdate) Then
                Dim sql As String = "select * from [Orders] where OrderID = " & orderdate.ToString()
            End If
            lblOrderDate.Text = orderdate
        End If

Open in new window

Avatar of Pratima
Pratima
Flag of India image

Did you mean to say you want to send Orderid in query and then retrive Order date ?

try this

  Dim orderdate As Date
 If Not String.IsNullOrEmpty(Request.QueryString("OrderID")) AndAlso Date.TryParse(Request.QueryString("OrderID"), orderdate) Then

 Dim command As SqlCommand = New SqlCommand("select OrderDate from [Orders] where OrderID = " & Request.QueryString("OrderID"),connection)
        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()
        If reader.HasRows Then
            Do While reader.Read()
            orderdate =reader.GetDateTime(0)
            Loop
       
        End If

        reader.Close()
End If
Your code looks a little confused. In this line:
If Not String.IsNullOrEmpty(Request.QueryString("OrderID")) AndAlso Date.TryParse(Request.QueryString("OrderID"), orderdate) Then

Open in new window

You are checking two Querystring values, one of which the name suggests is a date.

Which field are you trying to use to query the database, and what field do you want to display in your label?
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India 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
Avatar of Southern_Gentleman

ASKER

Ok, so I got the sqlconnection but how do I assign my label (lblOrderDate) to the connection string.
I was finally able to figure it out. Thanks again