Home > database >  c# and sql how to show selected item price in textbox
c# and sql how to show selected item price in textbox

Time:12-03

i selected item in combobox and showed in textbox but i whant to show also the price of that item in other textbox and add each price i selected

this is my code

public void fill_list()
        {
           // listBox1.Items.Clear();
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select TestName from TestTbl";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                TestidCb.Items.Add(dr["TestName"].ToString());
              // Cost = Convert.ToInt32(dr["TestCost"].ToString());
            }
            con.Close();

        }

CodePudding user response:

This will get you started. The ExecuteNonQuery has been removed because it serves no purpose.

I added "TestPrice" to the SELECT statement.

I have bound the DataTable to the combobox instead of added items in a loop.

I set both a DisplayMember and ValueMember. The user will only see TestName, but the value of the items is the price.

public void fill_list()
{
    try
    {
        con.Open();
        using(SqlCommand cmd = con.CreateCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select TestName, TestPrice from TestTbl";
        
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            
            TestidCb.DataSource = dt;
            TestidCb.DisplayMember = "TestName";
            TestidCb.ValueMember = "TestPrice";
        }           
    }
    finally
    {
        con.Close();
    }
}

Now, in the selected changed event for instance, you can get both TextName and the price and do whatever you need:

private void TestidCb_SelectedIndexChanged(object sender, EventArgs e)
{
    string name = ((ComboBox)sender).Text;
    string price = ((ComboBox)sender).SelectedValue.ToString();

    MessageBox.Show($"Name: {name}, Price: {price}");
}

CodePudding user response:

create a class myProductListItem having 2 properties, name and price

get both the name and the price in your query (select testname, price from TestTbl)

put these in an instance of myProductListItem and put that in your combobox.items

        public class myListItem
        {
            public string Name { get; set; }
            public decimal Price { get; set; }

            public override string ToString()
            {
                return Name;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add(new myListItem(){Name = "myname",Price = 10});
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selected = comboBox1.SelectedItem as myListItem;
            if (selected != null)
            {
                textBox1.Text = selected.Price.ToString();

            }
        }
  • Related