none
Retrieve Value From DataSet RRS feed

  • Question

  • Hi,

     

    I have a datatable which is populated with two rows from a SQL DB.  One row of the datatable is used as a drop down list in a combobox control.

     

    I was looking to populate a textbox control based on the value selected in the combobox, for example the combobox contains a code, and the txtbox should look up that code and return the description from the datatable.  Any ideas?

     

    By the way, my datatable does not have a primary key.

     

    Thanks in advance

    Thursday, September 27, 2007 3:28 PM

Answers

  • Code Block

    DataRow[] rows = DataSet.Tables[0].Select("code = " + ComboBox.Text);


    Thursday, September 27, 2007 3:37 PM
  • Hi Tony,

     

    Thanks for your help on this.  I have managed to resolve the issue.  I used a modified version of your original code, i.e.

     

    txtPublic.Text = dsLookup.Tables[0].Select("prom_code = '" + cmbPromCode.Text + "'")[0].ItemArray[1].ToString();

     

    Where txtPublic is the control I was trying to populate, "prom_code" is the 1st column in the datatable.

     

    Thanks for all your help tony.

     

    Thanks

    Sukh

    Monday, October 1, 2007 8:03 AM

All replies

  • Code Block

    DataRow[] rows = DataSet.Tables[0].Select("code = " + ComboBox.Text);


    Thursday, September 27, 2007 3:37 PM
  • Thanks for that but I get the following error;

     

    Syntax error: Missing operand after 'SC' operator.

     

    This is the code I am Running;

     

    private void cmbPromCode_SelectedValueChanged(object sender, System.EventArgs e)

    {

    DataRow[] rows = dsLookup.Tables[0].Select("public = " + cmbPromCode.Text);

    MessageBox.Show(rows.ToString());

     

    }

    Friday, September 28, 2007 11:54 AM
  • if it's not numeric data in the select, wrap it with single quote '

    DataRow[] rows = dsLookup.Tables[0].Select("public = '" + cmbPromCode.Text + “‘”);

    Btw, rows.ToString() doesn't show content of the row.

     Sukh1983 wrote:

    Thanks for that but I get the following error;

     

    Syntax error: Missing operand after 'SC' operator.

     

    This is the code I am Running;

     

    private void cmbPromCode_SelectedValueChanged(object sender, System.EventArgs e)

    {

    DataRow[] rows = dsLookup.Tables[0].Select("public = " + cmbPromCode.Text);

    MessageBox.Show(rows.ToString());

     

    }

    Friday, September 28, 2007 2:13 PM
  • Hi Tony,

     

    Sorry that did not work.  Let me explain further.  The DataSet extracts two columns (prom_code, public) and populates a datatable, tblLookup.  The prom_code values are used as the drop down list in cmbPromCode.  Once a user selects a value from cmbPromCode I need another control on my form, txtPublic to be populated based on the selected value, i.e. the cmbPromCode_SelectedValueChanged event procedure should lookup the selected value in cmbPromCode, and return the corresponding public value from the datatable?

     

    Thanks

    Friday, September 28, 2007 3:18 PM
  • you can bind table with display member, value member to your combobox like I mentioned here

    or you can select the data from datable like I mention above, it works fine for my own little test.

    Code Block

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                DataRow[] rows = dt.Select("NAME = '" + comboBox1.Text + "'");
                label1.Text = string.Format("display: {0} value: {1}",
                    comboBox1.Text, rows[0]["ID"].ToString());
            }


    since you got the syntax error, that must come from the select statement, if that's fixed, why it still doesnt work?

     Sukh1983 wrote:

    Hi Tony,

     

    Sorry that did not work.  Let me explain further.  The DataSet extracts two columns (prom_code, public) and populates a datatable, tblLookup.  The prom_code values are used as the drop down list in cmbPromCode.  Once a user selects a value from cmbPromCode I need another control on my form, txtPublic to be populated based on the selected value, i.e. the cmbPromCode_SelectedValueChanged event procedure should lookup the selected value in cmbPromCode, and return the corresponding public value from the datatable?

     

    Thanks

    Friday, September 28, 2007 3:42 PM
  • Hi Tony,

     

    Thanks for your help on this.  I have managed to resolve the issue.  I used a modified version of your original code, i.e.

     

    txtPublic.Text = dsLookup.Tables[0].Select("prom_code = '" + cmbPromCode.Text + "'")[0].ItemArray[1].ToString();

     

    Where txtPublic is the control I was trying to populate, "prom_code" is the 1st column in the datatable.

     

    Thanks for all your help tony.

     

    Thanks

    Sukh

    Monday, October 1, 2007 8:03 AM