Solved

Trying to populate a textbox using a MySQL query

Posted on 2015-02-17
5
145 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Creating and Managing Databases with phpMyAdmin in cPanel.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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