1

I used 4 checkbox in my application and created an integer list to add value if the checkbox is checked. The value will be stored in my database using sqlite. This is my code :

List<int> hobid = new List<int>();

 private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            if (Convert.ToBoolean(cricket.IsChecked))
            {
                hobid.Add(1);
            }
            else if (Convert.ToBoolean(football.IsChecked))
            {
                hobid.Add(2);
            }
            else if (Convert.ToBoolean(music.IsChecked))
            {
                hobid.Add(3);
            }
            else if (Convert.ToBoolean(reading.IsChecked))
            {
                hobid.Add(4);
            }

int rec;
   string strInserthob = "Insert into User_hobbies (User_id,Hobbies_id) values (@User_id,@Hobbies_id)";

//Here insertion is done..

 for (int i = 0; i < hobid.Count; i++)
                {
                    User_hobbies txt = new User_hobbies
                    {
                        User_id = userid,
                        Hobbies_id = hobid[i]
                    };
                    rec = (Application.Current as App).db.Insert<User_hobbies>(txt, strInserthob);
                }

}

Can you suggest me how to get the value in a list whatever checkbox is checked.

  • Only 4 checkbox? Just use 4 properties of type bool. – Fabrice Apr 08 '13 at 13:55
  • 1
    What value are you wanting to get from each `CheckBox`? The `IsChecked` value (which it looks like you've got already)? – lhan Apr 08 '13 at 14:07
  • Actually when i am clicking those check boxes i should get the value whatever i am adding in that list .. but i am fetching only one value which ever i am checking first.. i am not getting the rest.. for Example : 4 check boxes are 1.Cricket 2.football 3.music and 4.reading.. now i m checking football and music..in this case i m only getting 2 i.e. football. but I am not able to fetch 2 and 3 both the value.. cont... – Sanghati Mukherjee Apr 08 '13 at 21:39
  • I need to store these according to the userid that means if userid is suppose 4 and if he/she is checking 2 and 3 as hobbies then in database it should store 4 as userid contains two hobbies i.e. 2 and 3..but in my code i am not able to do that.. If you can suggest me it will be helpful... – Sanghati Mukherjee Apr 08 '13 at 21:39

1 Answers1

3

I'm assuming the button click is a 'Save' action.

In that case, you're only saving the first match due to the else if statements. The real quick and nasty way to do it is just remove the 'if else' statements and replace it with a straight if.

 if (Convert.ToBoolean(cricket.IsChecked))
            {
                hobid.Add(1);
            }
 if (Convert.ToBoolean(football.IsChecked))
            {
                hobid.Add(2);
            }
 if (Convert.ToBoolean(music.IsChecked))
            {
                hobid.Add(3);
            }
 if (Convert.ToBoolean(reading.IsChecked))
            {
                hobid.Add(4);
            }
David Gordon
  • 554
  • 2
  • 8
  • Thanks for your answer.. it really works.. thanks a lot again .. can you please help me one more time.. i am right now working on twitter app .. and i posted a question which you can find in this [link](http://stackoverflow.com/questions/15784428/hammock-package-tweetsharp-and-c-sharp-windows-phone-8).. please if you have any suggestion it would be a help .. – Sanghati Mukherjee Apr 09 '13 at 15:15