0

I want to get Id on an selected Item in combBox. ComboBox has multiple items. I have tried like this.

public void getSaleDetail()
{
            ClassConnection objConnection = new ClassConnection();
            string getString = objConnection.conMethod();
            SqlConnection con = new SqlConnection(getString);
            try
            {
                SqlCommand command = new SqlCommand("SELECT S.sID, (CONVERT(VARCHAR,S.sDate,101)+' '+V.vName+' '+P.pType) AS SaleInfo FROM sale AS S INNER JOIN vendor AS V ON V.vID=S.vID INNER JOIN plant AS P ON P.pID=S.pID WHERE S.sPayment_Status='In Progress' ORDER BY S.sDate ASC", con);
                con.Open();
                SqlDataReader reader;
                reader = command.ExecuteReader();
                ArrayList SaleInfo = new ArrayList();
                while (reader.Read())
                {
                    SaleInfo.Add(reader[1].ToString());
                }
                cmbSale.Items.Clear();
                cmbSale.Items.AddRange(SaleInfo.ToArray());                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                con.Close();
            }
}

in selectedIndexChanged event

string saleID = (string)cmbSale.SelectedValue.ToString();
                SqlCommand cmd = new SqlCommand("SELECT sWeight FROM sale WHERE sID='"+saleID+"'", con);
                con.Open();
                float Weight = (float)cmd.ExecuteScalar();
                txtWeight.Text = Weight.ToString();

please slove this issue.

Alenros
  • 816
  • 7
  • 23
Pavithra
  • 1
  • 1
  • Please explain what is the problem with your current code, what error you get or how the programs behavior differs from your intention. – Alenros Jan 08 '21 at 06:20
  • 1
    `SaleInfo.Add(reader[1].ToString());` should be `reader[0]` – Circle Hsiao Jan 08 '21 at 06:22
  • @CircleHsiao I was thinking the same, not sure if this is the only problem – Alenros Jan 08 '21 at 06:25
  • @Alenros thank you for your response. I am the beginner to the c# programming. my problem is combobox showing data correctly. but in the SelectedIndexChanged event there is problem System.NullReferenceException: Object reference not set as an instance of an object. – Pavithra Jan 08 '21 at 06:56
  • which line did the null ref exception occur? Use debug mode and breakpoint to see what exact variable is null but not suppose to be. – Circle Hsiao Jan 08 '21 at 07:03
  • By the way, it's redundant to cast with both (string) and use ToString() – Alenros Jan 08 '21 at 07:05
  • again same problem occurred. can you both tell me how to get ID on multiple column data in comboBox? – Pavithra Jan 08 '21 at 07:30
  • Try stepping through the lines to find out which line causes the exception. Are you choosing more than one line in the combo when the error happens? – Alenros Jan 08 '21 at 07:33
  • when I try to find the Sale Id using <> it returns null ref exception. and I have select only one row. the row has three columns. – Pavithra Jan 08 '21 at 07:37
  • 1
    No, use something like [Populating a ComboBox using C#](https://stackoverflow.com/questions/2417960/populating-a-combobox-using-c-sharp?noredirect=1&lq=1) or similar approaches. – dr.null Jan 08 '21 at 07:40

1 Answers1

0

First, you need to create a Data Transfer Object:

        public class Sale
        {
            public Sale(int newId, string newValue)
            {
                Id = newId;
                Value = newValue;
            }
            public int Id{ get; set; }
            public string Value { get; set; }
        }

Then populate your combobox:

                SqlCommand command = new SqlCommand("SELECT S.sID, (CONVERT(VARCHAR,S.sDate,101)+' '+V.vName+' '+P.pType) AS SaleInfo FROM sale AS S INNER JOIN vendor AS V ON V.vID=S.vID INNER JOIN plant AS P ON P.pID=S.pID WHERE S.sPayment_Status='In Progress' ORDER BY S.sDate ASC", con);
                con.Open();
                SqlDataReader reader;
                reader = command.ExecuteReader();
                var SaleInfo = new List<Sale>();
                while (reader.Read())
                {
                    var sale = new Sale(reader[0], reader[1]);
                    SaleInfo.Add(sale );
                }
                cmbSale.Items.Clear();

                this.comboBox1.DataSource = SaleInfo;
                this.comboBox1.DisplayMember = "Value";
                this.comboBox1.ValueMember = "Id";

And lastly to get your item:

// This will get your the sale item
   var chosenSale = comboBox1.Items[comboBox1.SelectedIndex];
// This will get the Id, as you wanted
   var chosenSaleID = comboBox1.Items[comboBox1.SelectedIndex].Id;
Alenros
  • 816
  • 7
  • 23
  • it shows the item selected on combobox. but I need the ID of the selected item. – Pavithra Jan 08 '21 at 08:08
  • you mean the Index? – Alenros Jan 08 '21 at 08:17
  • no.. the primary key of the data. in my case I have SALE table. when I select the item from comboBox, the saleID(primary key of my table) of the item need to to be show in the textbox – Pavithra Jan 08 '21 at 08:27
  • Got it, take a look at the revised answer. – Alenros Jan 09 '21 at 06:22
  • plz do not mistake me. where do I have to create the Data Transfer Object (Sale class)?? – Pavithra Jan 09 '21 at 11:04
  • Put the class in another file, named after the class, so if the class is Sale, call the file Sale.cs – Alenros Jan 09 '21 at 14:39
  • Ok I have created Sale.cs class file. now how to call the file? Sale s = new Sale(); is that correct? when I call like this it shows error as sale class does not contain constructor. – Pavithra Jan 09 '21 at 15:43
  • Look at the code I've written for the sale class, it includes a constructor. – Alenros Jan 09 '21 at 16:13
  • I know I'm irritating you. but I have no other way to solve the problem. I have copy your code and created the class. I think I make mistake on calling the class. will you please show me how to call Sale class? – Pavithra Jan 09 '21 at 17:31
  • It's fine, I'm glad to help. If you are having trouble with having the class in another folder try either '' 'using' '' the namespace the Sale class is in the file with the SQL and event, or try putting it in the same file as your other code – Alenros Jan 09 '21 at 18:06
  • If you do not mistake me can I have your email ID? If so, I can send a screen shot to you. Thank you. – Pavithra Jan 10 '21 at 12:36
  • Better yet - add a screenshot and any clarifications to your Question. That way whoever reads your question later could benefit from it. – Alenros Jan 10 '21 at 13:08