Solved

Trying to populate a textbox using a MySQL query

Posted on 2015-02-17
5
149 Views
Last Modified: 2015-02-18
I'm trying to populate a textbox using a query. The information I found is confusing to say the least. Here is my code:
public static bool orderQuantity( string txtorderQuantity, string orderQuantity)
    {
        MySqlConnection connection = new MySqlConnection("server=;user id=;database=;password=;persistsecurityinfo=True");
        connection.Open();
        var cmd = new MySqlCommand(String.Format("SELECT orderQuantity FROM `Order` WHERE orderID = '{0}'", orderQuantity), connection);
        var reader = cmd.ExecuteReader();
        
        if (reader.HasRows)
         {
              DataTable dt = new DataTable();
              dt.Load(reader);
              txtOrderQuantity.DataSource = dt;
              txtOrderQuantity.DataValueField = "orderQuantity";
              txtOrderQuantity.DataTextField = "orderQuantity";
              txtOrderQuantity.DataBind();
    }
        connection.Close();
        return true;
    }

Open in new window


This is on the page I want to load the data on:

clsDataLayer.orderQuantity( orderQuantity.Text);

Open in new window


VS is telling me that orderQuantity and txtorderQuantity do not exist in it's current context.
0
Comment
Question by:Robin Harris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 40616518
Your code doesn't list the server or database to connect to. According to http://www.connectionstrings.com/mysql/, your connection string should look like this:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Open in new window


In your code, you have nothing after the equal signs.
0
 

Author Comment

by:Robin Harris
ID: 40616551
I have this in the code:
 MySqlConnection connection = new MySqlConnection("server=;user id=;database=;password=;persistsecurityinfo=True");

Open in new window

0
 

Author Comment

by:Robin Harris
ID: 40616560
I gave up on my original idea and tried the following:
 public static bool orderQuantity(System.Web.UI.WebControls.TextBox txtQuantity, string orderID)
    {
        DataTable dt = new DataTable(); 
        MySqlConnection connection = new MySqlConnection("server=***;user id=***;database=***;password=***;persistsecurityinfo=True");
        connection.Open();
        MySqlCommand cmd = new MySqlCommand(string.Format("SELECT orderQuantity FROM `Order` WHERE orderID = @orderID", orderID), connection);
       
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        
        cmd.Parameters.AddWithValue("@orderID", orderID);

        da.Fill(dt);
        //if (reader.HasRows)
        if (dt.Rows.Count > 0)
        { 
            txtQuantity.Text = "orderQuantity";
            
        }
        connection.Close();
        return true;
    }

Open in new window


It's displaying the word "orderQuantity" instead of the actual data.

I also changed the target page code to:
 clsDataLayer.orderVerify(ddlProductID, orderID.Text);
            if (clsDataLayer.orderQuantity(txtQuantity, orderID.Text) == true) ;
            {
                txtQuantity.Text = Request.QueryString["orderQuantity"];
            }

Open in new window

0
 
LVL 14

Accepted Solution

by:
ThomasMcA2 earned 500 total points
ID: 40617011
I think your first attempt was on the right path.

Once your recordset is in a reader object, you need to retrieve the data from that reader object.

So instead of this:

txtOrderQuantity.DataValueField = "orderQuantity";

Open in new window


...try this:

txtOrderQuantity.DataValueField = reader["orderQuantity"];

Open in new window


As an FYI, most values "within quotes" are text strings. So if a program has a field called FieldA, the value "FieldA" (using the quotes) is a text string containing the letters "FieldA" - it is not the contents of the field called FieldA.
0
 

Author Comment

by:Robin Harris
ID: 40617359
That worked. Thanks for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question