Combination of data binding showing system.data.datarowview
I am linking combobox with datasource, displaymember, valuemember. It works fine on my computer, but it doesn't work on client PCs. Below is my source code:
The cbxAlloyBinding method is called from the UserControl constructor.
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 has bothered me for the last two days and I've almost tried all the tricks, but it still doesn't work.
The client PC uses Windows 7 ultimate, sql server 2005 and .net framework 3.5.
source to share
This definitely happens if yours cbxMetal_SelectedIndexChanged
is called before it is cbxAlloyBinding()
called in your constructor.
For example (see code below), you may have other combobox bindings in the constructor that may be before cbxAlloyBinding()
in the constructor, and those bindings are called 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();
}
I suspect yours is cbxMetal.DataSource
installed from some other point in your code and well before DisplayMember
and ValueMember
;
Just remember System.DataRow.DataRowView
will only happen if
ComboBox.SelectedValue
called before assignmentValueMember
.
source to share
It seems the problem is not related to the code you are pasting here, it could be with the client environment, connection, privileges, etc. You should read more about it, "it doesn't work on client PC". What systems are they using, what is the error, have you tried client side debugging?
* Edit: * then you need to find the problem tracing the whole operation from the beginning. My advice is to create a dummy window application, just a standard form, a dropdown name and a button. In case of pressing the button, press only what you did. JUst binds the combo and sends this temporary app to the client. If it works, then do it 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 works for me.
source to share
I solved the same way:
/*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 of data sources
source to share
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;
}
}
source to share
THIS DEFINITELY HELPS YOU
on load Event you want to just log this code
onformload()
{
cmb_dept.Items.Clear();
SqlConnection conn = new SqlConnection(@"DATA SOURCE=(localdb)\MSSQLLocalDB;INTEGRATED SECURITY=true;INITIAL CATALOG=EMPLOYEE;");
conn.Open();
SqlCommand command = new SqlCommand("select dept_id, dept_name from department", conn);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
cmb_dept.ValueMember = "dept_id";
cmb_dept.DisplayMember = "dept_name";
cmb_dept.DataSource = ds.Tables[0];
}
try using Use the code where you want to access the values ........
string dept = cmb_dept.Text;
MessageBox.Show("val=" + dept);
Your combobox.text = System.Data.DataRowView ## will be resolved
source to share
"comboBox1.SelectedValue" returns an object and you want as a string to convert it Convert.ToString (comboBox1.SelectedValue) and then use
eg:
tempcmd = new SqlCommand("SELECT specification,alloyCode FROM alloy where alloyId='" + Convert.ToString(cbxMetal.SelectedValue) + "'", con);
source to share