Solved

Trying to populate a textbox using a MySQL query

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now