Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. comobox.SelectedValue

comobox.SelectedValue

Scheduled Pinned Locked Moved C#
help
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    webhay
    wrote on last edited by
    #1

    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:(

    B 1 Reply Last reply
    0
    • W webhay

      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:(

      B Offline
      B Offline
      Brett Slaski
      wrote on last edited by
      #2

      You need to cast the value. E.g. int theInt = (int) ComboBox.selectedValue;

      W 1 Reply Last reply
      0
      • B Brett Slaski

        You need to cast the value. E.g. int theInt = (int) ComboBox.selectedValue;

        W Offline
        W Offline
        webhay
        wrote on last edited by
        #3

        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

        A B 2 Replies Last reply
        0
        • W webhay

          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

          A Offline
          A Offline
          Alex Korchemniy
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • W webhay

            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

            B Offline
            B Offline
            Brett Slaski
            wrote on last edited by
            #5

            How are you loading the combo?

            W 1 Reply Last reply
            0
            • B Brett Slaski

              How are you loading the combo?

              W Offline
              W Offline
              webhay
              wrote on last edited by
              #6

              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

              A 1 Reply Last reply
              0
              • W webhay

                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

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                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+"";

                W 1 Reply Last reply
                0
                • A Anonymous

                  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+"";

                  W Offline
                  W Offline
                  webhay
                  wrote on last edited by
                  #8

                  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();

                  B 1 Reply Last reply
                  0
                  • W webhay

                    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();

                    B Offline
                    B Offline
                    Brett Slaski
                    wrote on last edited by
                    #9

                    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.

                    W 2 Replies Last reply
                    0
                    • B Brett Slaski

                      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.

                      W Offline
                      W Offline
                      webhay
                      wrote on last edited by
                      #10

                      Thanks Brett :rose: it is working now you are so helpful thanks for your time and it is working in early binding too... :-D:laugh::rose: Regards webhay

                      1 Reply Last reply
                      0
                      • B Brett Slaski

                        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.

                        W Offline
                        W Offline
                        webhay
                        wrote on last edited by
                        #11

                        Thanks Brett:rose: it is working fine now.you are so helpful.... Thanks for your time.... it is working in Erely binding tooo... :);):-D:-O:laugh::laugh::rose: regards webhay

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups