0

I am working on POS System.I am selecting product descreptions from textbox but how I select the same product price ??

public void autoFill(TextBox abc) {
            SqlCommand cmd = new SqlCommand("SELECT * FROM pProduct",cnn.con);
            SqlDataReader rd;      

            try
            {

                cnn.con.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read()) {
                    abc.AutoCompleteCustomSource.Add(rd["Descreption"].ToString());
                }
                rd.Close();                
                cnn.con.Close();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }
        }
1
  • What do you mean by same product price? Commented Oct 16, 2016 at 10:36

2 Answers 2

2

use another TextBox for Price

public void autoFill(TextBox abc, TextBox prc) {
            SqlCommand cmd = new SqlCommand("SELECT * FROM pProduct",cnn.con);
            SqlDataReader rd;      

            try
            {

                cnn.con.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read()) {
                    abc.AutoCompleteCustomSource.Add(rd["Descreption"].ToString());
                    prc.AutoCompleteCustomSource.Add(rd["Price"].ToString());
                }
                rd.Close();                
                cnn.con.Close();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }
        }
Sign up to request clarification or add additional context in comments.

2 Comments

It is showing list of prices but how to select the price for the selected description?
ok so you don't want to crate a another price selecttion textbox, but wnat to select the price for the selected description ?
0

Add field to your Form class:

Dictionary<string, decimal> products = new Dictionary<string, decimal>();

Of course you should use your own types instead of string and decimal.

Insert data into dictionary while reading from database:

using (var conn = new SqlConnection(connectionString))
{
    conn.Open();
    using (var cmd = new SqlCommand("SELECT * FROM pProduct", conn))
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            string description = (string)reader["Description"];
            decimal price = (decimal)reader["Price"];

            products.Add(description, price);
            descriptionTextBox.AutoCompleteCustomSource.Add(description);
        }
    }
}

Note the using statement. It will close and dispose resources even in case of exceptions.

Subscribe to TextChanged event.

private void DescriptionTextBox_TextChanged(object sender, EventArgs e)
{
    decimal price;
    if (products.TryGetValue(descriptionTextBox.Text, out price))
    {
        priceTextBox.Text = price.ToString();
    }
    else
    {
        priceTextBox.Text = string.Empty;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.