?
Solved

Trying to populate a textbox using a MySQL query

Posted on 2015-02-17
5
Medium Priority
?
150 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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 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