[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Trying to populate a textbox using a MySQL query

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

656 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