Link to home
Start Free TrialLog in
Avatar of Robin Harris
Robin HarrisFlag for United States of America

asked on

List box not populating based on MySQL query

I am attempting to query my database and return productID's based on the orderID. First I tried:
"SELECT productID FROM `Order` WHERE Order.orderID = Order.orderID"

But that returned all the productID's from all orders in the table. I currently have the following code but now the list box isn't returning any value.

 public static bool orderVerify(DropDownList ddlProductID)
    {
        MySqlConnection connection = new MySqlConnection("server=*****;user id=****;database=***;password=***;persistsecurityinfo=True");
        connection.Open();
        var cmd = new MySqlCommand("SELECT productID FROM `Order` WHERE orderID = 'orderID.Text'", connection);
        var reader = cmd.ExecuteReader();
        
        if (reader.HasRows)
         {
              DataTable dt = new DataTable();
              dt.Load(reader);
              ddlProductID.DataSource = dt;
              ddlProductID.DataValueField = "productID";
              ddlProductID.DataTextField = "productID";
              ddlProductID.DataBind();
    }
        connection.Close();
        return true;
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany 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 Robin Harris

ASKER

I got it. Here is the change I made:
var cmd = new MySqlCommand(String.Format("SELECT productID FROM `Order` WHERE orderID = '{0}'", orderID), connection);

Open in new window


I also had to reference OrderID:
public static bool orderVerify(DropDownList ddlProductID, string orderID) 

Open in new window


Finally I had to add the OrderID.Text reference:
clsDataLayer.orderVerify(ddlProductID, orderID.Text);

Open in new window




Thanks for your help.