none
WPF ComboBox bind with database RRS feed

  • Question

  • Now,I want To Save selected value of that combobox i.e C1 B1 F1 into the database..

    private void btnSave_Click(object sender, RoutedEventArgs e)
            {
                con = new OleDbConnection(@"Provider=Microsoft.ACE.Oledb.12.0; Data Source=C:\Documents and Settings\@Mit\Desktop\Project\KonnetNew\KonnetNew\CAD_DB1.mdb");
                con.Open();
                cmd.Connection = con;
                var selectedItem = comboBoxHR.SelectedItem;
                ComboBoxItem cbi = comboBoxHR.ItemContainerGenerator.ContainerFromItem(selectedItem) as ComboBoxItem;
              
                string Id = HubRoomIDtxt.Text;
                if (cbi != null)
                {
                    StackPanel sp = cbi.Content as StackPanel;
                    TextBlock tb = sp.Children[0] as TextBlock;  //Object reference not set to an instance of an object. // here i got the exception....
                 
                    string a = tb.Text;
                cmd.CommandText = "Insert into HubRoom values('" + Id + "','" + a + "','" + a + "','" + a + "')";
                cmd.ExecuteNonQuery();
                }    
            }

    Plz help m..... i just want to save C1 into database table which contain column Campus_ID. and B1 into Building_ID...like that.....

    Wednesday, February 22, 2012 6:39 PM

Answers

  • Your actual problem probably is that this is not how to access the value of the combobox.

    If you put a breakpoint when "selectedItem" is created, what is the selected item? Can you cast it back to it's class and use it's properties?

    In other words, what are you binding to the combobox ItemsSource?

    If you built the combobox items manually (with textboxes into a staackpanel) you're probably not leaveraging the power of WPF binding and templates.

    http://msdn.microsoft.com/en-us/library/aa480226.aspx

    If you have a legitimate reason to use this "control orientated data extraction" style method, then I'd put a breakpoint on this code and step through it, then you'll see what cbi and sp are, and why sp is null, or doesn't have children.

    If that doesn't help, you need to show a complete example.

    Regards,
    Pete


    #PEJL

    Thursday, February 23, 2012 12:28 AM
    Moderator

All replies

  • Your actual problem probably is that this is not how to access the value of the combobox.

    If you put a breakpoint when "selectedItem" is created, what is the selected item? Can you cast it back to it's class and use it's properties?

    In other words, what are you binding to the combobox ItemsSource?

    If you built the combobox items manually (with textboxes into a staackpanel) you're probably not leaveraging the power of WPF binding and templates.

    http://msdn.microsoft.com/en-us/library/aa480226.aspx

    If you have a legitimate reason to use this "control orientated data extraction" style method, then I'd put a breakpoint on this code and step through it, then you'll see what cbi and sp are, and why sp is null, or doesn't have children.

    If that doesn't help, you need to show a complete example.

    Regards,
    Pete


    #PEJL

    Thursday, February 23, 2012 12:28 AM
    Moderator
  • Hi Pete,

    Thank u so much.....its help me....

    thanks a lot once again....:) :)

    Regards,

    Amit


    Amit Roman

    Thursday, February 23, 2012 5:08 AM
  • Hi Amit Sopan Roman,

    If you have got what you need, could you close your thread as "Answered", thank you.

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 24, 2012 8:06 AM
    Moderator