comobox.SelectedValue
-
i assigne a number from Databaseto ValueMemeber in combobox.and i tryed to use it from ComboBox.SelectedValue but it return an object type but i need the number on this object for select a value in another Table but is not working. Thnaks i need help please:(
-
i assigne a number from Databaseto ValueMemeber in combobox.and i tryed to use it from ComboBox.SelectedValue but it return an object type but i need the number on this object for select a value in another Table but is not working. Thnaks i need help please:(
You need to cast the value. E.g. int theInt = (int) ComboBox.selectedValue;
-
You need to cast the value. E.g. int theInt = (int) ComboBox.selectedValue;
-
Thanks to reply me but i did this casting but it give me this message specified cast is not Valide Thanks for your time please help me
I'm not quite sure what you are trying to do but you can always use the debugger watch windows to determine the type of the object.
-
Thanks to reply me but i did this casting but it give me this message specified cast is not Valide Thanks for your time please help me
How are you loading the combo?
-
How are you loading the combo?
this my code: private void cob_SelectedValueChanged(object sender, System.EventArgs e) { label1.Text = cbo.SelectedValue.ToString(); SqlConnection cnn = new SqlConnection(); cnn.ConnectionString = "workstation id=UNKNOW;packet size=4096;user id=sa;data source=UNKNOW;persist security info=False;initial catalog=TestDB;"; SqlCommand cmd = cnn.CreateCommand(); cmd.CommandType = CommandType.Text; //int x = (int)cbo.SelectedValue; cmd.CommandText = "Select * from Val Where Cat_ID = "+cbo.SelectedValue+""; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); lst.DataSource = ds.Tables[0]; lst.DisplayMember = "Val_Name"; lst.ValueMember = "Cat_ID"; } Thanks for your time .... haytham
-
this my code: private void cob_SelectedValueChanged(object sender, System.EventArgs e) { label1.Text = cbo.SelectedValue.ToString(); SqlConnection cnn = new SqlConnection(); cnn.ConnectionString = "workstation id=UNKNOW;packet size=4096;user id=sa;data source=UNKNOW;persist security info=False;initial catalog=TestDB;"; SqlCommand cmd = cnn.CreateCommand(); cmd.CommandType = CommandType.Text; //int x = (int)cbo.SelectedValue; cmd.CommandText = "Select * from Val Where Cat_ID = "+cbo.SelectedValue+""; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); lst.DataSource = ds.Tables[0]; lst.DisplayMember = "Val_Name"; lst.ValueMember = "Cat_ID"; } Thanks for your time .... haytham
I assume that this is working ok? Or is it Returning the object name? If not, how are you filling the combobox, databind or by item? SelectedValue is only used with databinding, a ValueMemeber must be set. webhay wrote: label1.Text = cbo.SelectedValue.ToString(); The following will not work because it will return the object name. webhay wrote: cmd.CommandText = "Select * from Val Where Cat_ID = "+cbo.SelectedValue+"";
-
I assume that this is working ok? Or is it Returning the object name? If not, how are you filling the combobox, databind or by item? SelectedValue is only used with databinding, a ValueMemeber must be set. webhay wrote: label1.Text = cbo.SelectedValue.ToString(); The following will not work because it will return the object name. webhay wrote: cmd.CommandText = "Select * from Val Where Cat_ID = "+cbo.SelectedValue+"";
i filled the combobox with databind : SqlConnection cnn = new SqlConnection(); cnn.ConnectionString = this.ConnStr; SqlCommand cmd = cnn.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = this.SelectCommand; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); combobox.DataSource = ds.Tables[0]; combobox.DisplayMember = ds.Tables[0].Columns["Client_Name"].ToString(); combobox.ValueMember = ds.Tables[0].Columns["Client_ID"].ToString();
-
i filled the combobox with databind : SqlConnection cnn = new SqlConnection(); cnn.ConnectionString = this.ConnStr; SqlCommand cmd = cnn.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = this.SelectCommand; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); combobox.DataSource = ds.Tables[0]; combobox.DisplayMember = ds.Tables[0].Columns["Client_Name"].ToString(); combobox.ValueMember = ds.Tables[0].Columns["Client_ID"].ToString();
webhay wrote: combobox.DataSource = ds.Tables[0]; combobox.DisplayMember = ds.Tables[0].Columns["Client_Name"].ToString(); combobox.ValueMember = ds.Tables[0].Columns["Client_ID"].ToString(); You are not properly binding your combobox. Try this: combobox.DataSource = ds; combobox.DisplayMember = "TableName.Client_Name"; combobox.ValueMember = "TableName.Client_ID"; Note it is not recomended to use `ds.TableName' for the datasource.
-
webhay wrote: combobox.DataSource = ds.Tables[0]; combobox.DisplayMember = ds.Tables[0].Columns["Client_Name"].ToString(); combobox.ValueMember = ds.Tables[0].Columns["Client_ID"].ToString(); You are not properly binding your combobox. Try this: combobox.DataSource = ds; combobox.DisplayMember = "TableName.Client_Name"; combobox.ValueMember = "TableName.Client_ID"; Note it is not recomended to use `ds.TableName' for the datasource.
-
webhay wrote: combobox.DataSource = ds.Tables[0]; combobox.DisplayMember = ds.Tables[0].Columns["Client_Name"].ToString(); combobox.ValueMember = ds.Tables[0].Columns["Client_ID"].ToString(); You are not properly binding your combobox. Try this: combobox.DataSource = ds; combobox.DisplayMember = "TableName.Client_Name"; combobox.ValueMember = "TableName.Client_ID"; Note it is not recomended to use `ds.TableName' for the datasource.