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:
<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 beforeValueMember
assignment.
I resolved same this:
/*First get DataSource*/
comboBox1.DataSource = dt;
/*Then determine DisplayMember y ValueMember*/
comboBox1.DisplayMember = "YOUR_FIELD_NAME";
comboBox1.ValueMember = "YOUR_OTHER_FIELD_NAME";
This only works with System.Data.DataTable or List data sources
It was showing me the same exception, so I did this and it worked
private void cb_category_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable mydt = new DataTable();
try
{
mydt = request.GetItem(int.Parse(cb_category.SelectedValue.ToString()));
}
catch { }
if(mydt.Rows.Count>0)
{
cb_Item.DataSource = mydt;
cb_Item.DisplayMember = "dispmember";
cb_Item.ValueMember = "valmember";
}
else
{
cb_Item.DataSource = null;
}
}
"comboBox1.SelectedValue" returns object and you want as string so convert it Convert.ToString(comboBox1.SelectedValue) and then use
for example:
tempcmd = new SqlCommand("SELECT specification,alloyCode FROM alloy where alloyId='" + Convert.ToString(cbxMetal.SelectedValue) + "'", con);
It seems problem is not with the code you pasted here, it may be with client environment, connection, privileges etc. You must give more info about that 'it is not working in clients pc'. What systems they use, what is the error, have you tried debugging in client side?
*Edit:*then you have to find the problem tracing all the operation from the beginning. My advice is that make a dummy windows form application, just a standard form, a combobox and a button. ON the click event of button just do what you did. JUst bind the combo and send this temp app to the client. If it works then do step by step.Ex:
private void button1_Click(object sender, EventArgs e)
{
string conStr = "Data Source=PC-303\\SQLEXPRESS;Initial Catalog=sokaklar;User ID=sa;Password=*****";
SqlDataAdapter adapter = new SqlDataAdapter("SELECT DISTINCT IL, IL_ID FROM sokaklar ORDER BY IL", new SqlConnection(conStr));
DataTable dt = new System.Data.DataTable();
adapter.Fill(dt);
comboBox1.DisplayMember = "IL";
comboBox1.ValueMember = "IL_ID";
comboBox1.DataSource = dt;
}
I created this app in one minute, and it is working for me.
setting the DisplayMember
and ValueMemeber
after setting the DataSource
fixed this issue for me.
cbxMetal.DataSource = dt;
cbxMetal.DisplayMember = "alloyName";
cbxMetal.ValueMember = "alloyId";