Ask a questionAsk a question
 

AnswerSaving a combobox item to the database.

  • Wednesday, November 04, 2009 9:08 PMGarvander Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a combobox with hard coded combobox items. Each combobox item has a tag which represent the value that I would like to save to the database. How do I pull the tag from a combobox item?
    Is there an easier way to bind a comboboxitem?
    Certified Geek. Data dude.

Answers

  • Thursday, November 05, 2009 2:34 PMGarvander Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

     

    ComboBoxItem cbi = (ComboBoxItem)ComboType.SelectedItem;
    if (cbi != null){
    int type = Convert.ToInt32(cbi.Tag.ToString());
    item.InventoryTypeId = type;
    }
    The above code works fine.


    Certified Geek. Data dude.
    • Marked As Answer byGarvander Thursday, November 05, 2009 3:11 PM
    •  

All Replies

  • Thursday, November 05, 2009 4:08 AMRahul P Nath Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    Could you please tell your scenario bit more clear?
    What are you trying to do here?Some code sample should help

    Thank You
    Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem
  • Thursday, November 05, 2009 8:32 AMZhi-Xin YeMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Garvander,

    I'm not sure whether I understand your problem, if the ComboBox is set up in this way:

            <ComboBox name="comboBox1">
                <ComboBoxItem Content="a1" Tag="bbb1"/>
                <ComboBoxItem Content="a2" Tag="bbb2"/>
                <ComboBoxItem Content="a3" Tag="bbb3"/>
                <ComboBoxItem Content="a4" Tag="bbb4"/>
            </ComboBox>

    Then you can get the Tag value directly from the ComboBoxItem:

     private void button1_Click(object sender, RoutedEventArgs e)
            {
                foreach (ComboBoxItem item in comboBox1.Items)
                {
                    Console.WriteLine(item.Tag .ToString());
                }
            }

    If anything is unclear, please feel free to let me know.

    Best Regards,
    Zhi-Xin Ye
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com






    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
  • Thursday, November 05, 2009 12:39 PMGarvander Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The user needs to pick a value from a combobox. The selection is text and its id needs to be saved in the database. I am open to any solution the works.
    Certified Geek. Data dude.
    • Edited byGarvander Thursday, November 05, 2009 1:19 PM
    •  
  • Thursday, November 05, 2009 12:44 PMRahul P Nath Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    Get the Combobox SelectedItem whenever you want to save to the database.
    Kindly tell what is the issue you are facing?

    Thank You
    Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem
  • Thursday, November 05, 2009 1:04 PMGarvander Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    which item in the foreach loop is the selected item?
    Certified Geek. Data dude.
  • Thursday, November 05, 2009 2:20 PMGarvander Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    THe selected item is text and I want to save the corresponding id. Same way a select box works in HTML.
    Certified Geek. Data dude.
  • Thursday, November 05, 2009 2:34 PMGarvander Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

     

    ComboBoxItem cbi = (ComboBoxItem)ComboType.SelectedItem;
    if (cbi != null){
    int type = Convert.ToInt32(cbi.Tag.ToString());
    item.InventoryTypeId = type;
    }
    The above code works fine.


    Certified Geek. Data dude.
    • Marked As Answer byGarvander Thursday, November 05, 2009 3:11 PM
    •