Thread: Checkboxlist selected property

  1. #1
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71

    Exclamation Checkboxlist selected property

    hi guys
    i want to use checkboxlist.selecteditem.selected property.

    when i clicked the checkbox a int value must be written to db, when i clicked again the checkbox this value must be changed.

    i wrote code like that but i have error -> Object reference not set to an instance of an object

    what can i do? i m waiting your answers...

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    It usually occurs when your using a null reference. Make sure your object is being properly initialized.
    Spidey out!

  3. #3
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71
    i dont think i m using a null reference, cause when i clicked the random checkbox the element's informations come

  4. #4
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71
    my code is below

    Code:
    if (cblNames.SelectedItem == null)
                {
                    if (cblNames.SelectedItem.Selected==true)    //ERROR OMES HERE   (Object reference not set to an instance of an object)
                    {
                        OleDbCommand com = new OleDbCommand("UPDATE kisi SET kisi_check=@p1 where kisi_id=@id", connect);
                        com.Parameters.AddWithValue("@id", cblNames.SelectedValue);
                        com.Parameters.AddWithValue("@p1", 1);
                        com.ExecuteNonQuery();
                    }
    
                    else if (cblNames.SelectedItem.Selected == false)
                    {
                        OleDbCommand ucom = new OleDbCommand("UPDATE kisi SET kisi_check=@pc WHERE kisi_id=@i", connect);
                        ucom.Parameters.AddWithValue("@i", cblNames.SelectedValue);
                        ucom.Parameters.AddWithValue("@pc", 0);
                        ucom.ExecuteNonQuery();
                    }
                }

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    if (cblNames.SelectedItem == null)
    isn't that a null reference ?
    think about it,

    Code:
    if (cblNames.SelectedItem == null)
    then your code below will expand to this -

    Code:
    if(null.Selected == true)
    {
    ...
    }
    Spidey out!

  6. #6
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71
    ok thats right then i remove

    Code:
    if (cblNames.SelectedItem == null)
    but there is the same error on the same line

  7. #7
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by Aga^^ View Post
    ok thats right then i remove

    Code:
    if (cblNames.SelectedItem == null)
    but there is the same error on the same line
    Yes, because if it was true before, that means you were passing in a null reference from somewhere. So, make sure all your objects are being initialized properly.

    Try this -
    Code:
    if (cblNames.SelectedItem != null)
    so, that you know whats causing the trouble.
    Spidey out!

  8. #8
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71
    yess that works

    thanks spidey

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    SelectedItem is null if nothing is selected in the listbox, thus your error.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rock, Paper, Scissors
    By Slynet in forum C Programming
    Replies: 2
    Last Post: 02-13-2009, 07:14 PM
  2. static property
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-12-2008, 01:03 AM
  3. C++/CLI Property
    By psychopath in forum Windows Programming
    Replies: 5
    Last Post: 07-11-2006, 09:45 PM
  4. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM
  5. Property Sheets :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 05-09-2002, 03:04 PM