2
votes

I have comboBox name as cmbContactType. I binded this combo with dataSource as DisplayMember ValueName(string type) and valueMember as ValueID(number type). When user change value in cmbContactType then cmbContactType.SelectValue get set and I am able to save document with this Selectedvalue. I facing problem while setting cmbContactType value from database which is of type number. Every time I am trying to set value, null value set to the cmbContactType.SelectValue. Following is the code by which I am binding datasource to cmbContactType and for setting SelectedValue of cmbContactType. I am using VS2010 and MS-Access. Please help.

    //method to bind dataSource.
    public static void BindDataSourceWithCombo(ref ComboBox cmb, string query, string valueMember, string displayMember)
    {
        DataTable _tableSource = (new AccessConnectionManager()).GetDataTableBySQLQuery(query);

        var _dataSource = (from DataRow _row in _tableSource.Rows
                           select new
                           {
                               ValueMember = _row[valueMember],
                               DisplayMember = _row[displayMember].ToString()

                           }).ToList();

        cmb.DisplayMember = "DisplayMember";
        cmb.ValueMember = "ValueMember";
        cmb.DataSource = _dataSource;
    }

    // Method to set values in document.
    public void SetDocumentProperties()
    {
        string _query = string.Format("Select ContactCode,ContactName,ContactType from ContactMaster where ContactCode = '{0}'", Convert.ToString(cmbContactCode.Text));
        DataTable _table = AccessConnectionManagers.GetDataTableBySQLQuery(_query);

        if (_table.Rows.Count > 0)
        {
            DataRow _row = _table.Rows[0];
            txtContactCode.Text = Convert.ToString(_row["ContactCode"]);
            txtContactName.Text = Convert.ToString(_row["ContactName"]);
            cmbContactType.SelectedValue = _row["ContactType"];
        }
        else
        {
            txtContactCode.Text = string.Empty;
            txtContactName.Text = string.Empty;
            cmbContactType.SelectedValue = 1;
        }
    }

Here is code by which I bind dataSource with cmbConactType. This method call on Form_Load event.

    private void LoadContactTypeCombo()
    {
        //Table: PicklistValues(ID,MasterID,ValueID,ValueName) 
        string _query = string.Format("select ValueID,ValueName from PicklistValues where MasterID = {0}", 1);
        BindDataSourceWithCombo(ref cmbContactType, _query, "ValueID", "ValueName");
    }
3
first check if _row["ContactType"] is having any value. If yes then try casting it to int before assigning it to SelectedValue - vendettamit
Yes, _row["ContactType"] has value. I tried _row["ContactType"] != null ? Convert.ToInt32(_row["ContactType"]) : -1 this also. But wasnt work. - Ankush Madankar
@AnkushMadankar what is the datasource of cmbContactType? - King King
@King King, Yes check dataSource. It has 6 values with Display member and valueMember - Ankush Madankar
@AnkushMadankar I don't see you assign some DataSource to your cmbContactType anywhere in your code. It's important to solve your problem. - King King

3 Answers

3
votes

try by this method

int index;
// Search the Item that matches the string
index=cmbContactType.FindString(_row["ContactType"]);
// Select the Item in the Combo
cmbContactType.SelectedIndex=index;
2
votes

You should notice the type of the actual ValueMember in your DataSource, when assign the SelectedValue to some value, it should be castable to the type of the actual ValueMember in your DataSource.

You have to ensure that _row["ContactType"] has to be contained by the item values of your cmbContactType. If not, the SelectedItem will be null. You should perform some check and see if it helps.

0
votes

Finally I found solution to my question, when I look for Type of cmbContactType.SelectedValue, I found it is typeof Int16...! and by using following statement cmbContactType.SelectedValue get set.

cmbContactType.SelectedValue =_row["ContactType"] != null ? Convert.ToInt16(_row["ContactType"]) : (Int16)(-1) ;