Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox databinding showing system.data.datarowview

I am binding combobox with datasource, displaymember, valuemember. It is working fine in my computer but it is not working in clients pc. Following is my source code:

cbxAlloyBinding method is called from the Constructor of the UserControl.

private void cbxAlloyBinding()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter("SELECT alloyName,alloyId FROM alloy", con);
        adp.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            cbxMetal.DisplayMember = "alloyName";
            cbxMetal.ValueMember = "alloyId";
            cbxMetal.DataSource = dt;
        }
        else
        {
            cbxMetal.Text = "";
        }
    }

    private void cbxMetal_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cbxMetal.SelectedIndex != -1)
        {
            DataTable dt = new DataTable();
            tempcmd = new SqlCommand("SELECT specification,alloyCode FROM alloy where alloyId='" + cbxMetal.SelectedValue + "'", con);
            SqlDataAdapter adp = new SqlDataAdapter(tempcmd);
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                txtSpecification.Text = dt.Rows[0]["alloyCode"].ToString();
                txtSupplyConditions.Text = dt.Rows[0]["specification"].ToString();
                cbxheatBinding();
            }
            else
            {
                txtSpecification.Text = "";
            }

        }
    }

This is bothering me from last two days and i almost tried all tricks but it is still not working.

Client's PC is using Windows 7 ultimate, sql server 2005 and .net framework 3.5.

like image 389
Saral Doshi Avatar asked Jan 31 '13 07:01

Saral Doshi


2 Answers

This definitely happens if your cbxMetal_SelectedIndexChanged is called before cbxAlloyBinding() is called in your constructor.

For instance (see the code below), you may have other combobox bindings in constructor which may come before cbxAlloyBinding() in constructor, and those bindings are calling cbxMetal_SelectedIndexChanged.

public Constructor()
{
        InitializeComponent();

        cbxheatBinding();      //1st Three Binding Methods may be somehow related to your cbxMetal,
        dtpStartDateBinding(); //which leads them to call cbxMetal_SelectedIndexChanged method.
        dtpEndDateBinding();
        cbxAlloyBinding();
}

What I suspect is your cbxMetal.DataSource is set from some other point in your code and well before DisplayMember and ValueMember are assigned;

Just remember, System.DataRow.DataRowView will occur only if

ComboBox.SelectedValue is called before ValueMember assignment.

like image 57
Marshal Avatar answered Nov 16 '22 07:11

Marshal


setting the DisplayMember and ValueMemeber after setting the DataSource fixed this issue for me.

cbxMetal.DataSource = dt;
cbxMetal.DisplayMember = "alloyName";
cbxMetal.ValueMember = "alloyId";
like image 26
philx_x Avatar answered Nov 16 '22 08:11

philx_x