none
DropDown List

    Pregunta

  • Hi,

    I have a dropdown list which gets /sets value through the property. as

     

     get { return ddTextBlock.SelectedItem.Text.ToString(); }

     set { ddTextBlock.SelectedItem.Text= value ; }

     }

    the dropdown can be null or valued depending on the circumstance
    and when I want to add the vales from this form and if there is no value in the dropdown then I get null reference exception
    also I have some old records when I try to retrive the null values I get the same exception.
    this is what I am doing on form to get values
    If (drEditSc["scConfirmTextBlock"]=="") null; }
    else

    { GetSourceCode.TextBlock =

     { GetSourceCode.TextBlock = drEditSc["scConfirmTextBlock"].ToString(); }

    *GetSourceCode is a User Control
    * TextBlock is name of the get /set property mentioned aboved.

    Object reference not set to an instance of an object.//ERROR
    viernes, 12 de marzo de 2010 22:46

Todas las respuestas

  • If SelectedItem is null, SelectedItem.Text cannot be accessed (it would be as writing null.Text). This is what is causing the exception to be thrown.

    The solution is pretty straightforward:

    public string MyProperty {
      get {
        if (ddTextBlock.SelectedItem != null) {
          return ddTextBlock.SelectedItem.Text;
        } else {
          return null;
        }
      }
      set {
        if (ddTextBlock.SelectedItem != null) {
          ddTextBlock.SelectedItem.Text = value;
        }
      }
    }
    As for the second question, the code is a bit messed up. Can you please re-post it, and possibly give a little context (what is drEditSc?)

    HTH
    --mc
    viernes, 12 de marzo de 2010 23:45
  • Thanks for the update,
    Now I am able to INSERT into the database with the NULL values with the help of get property.

    The second question was
    how do I read back the null values from the database.

    for e.g in the above case
    Now I am able to INSERT into the database (NULL values)
    Now I have another page where I am using the same user control and wants to display data on the form
    If NULL display blank dropdown 
    else
    display the selected value from the database

    I am giving the live example

    The Text Block Property

    public

     

    string TextBlock

    {

     

     

    get {

     

    if (ddTextBlock.SelectedItem != null)

    {

    return ddTextBlock.SelectedItem.Text; }

     

    else

    {

    return null; }

    }

     

    set

    {

     

    if (ddTextBlock.SelectedItem != null)

    ddTextBlock.SelectedItem.Text =

    value;

    }


    and reading it on the form as

    if

     

    (drEditSc["scConfirmTextBlock"]==DBNull.Value)

    { GetSourceCode.TextBlock =

    ""; }

     

    else

    { GetSourceCode.TextBlock = drEditSc[

    "scConfirmTextBlock"].ToString(); }

     


    ERROR://Object cannot be cast from DBNull to other types.

    sábado, 13 de marzo de 2010 0:13
  • If drEditSc is a SqlDataReader, the code seems perfectly ok. Where is the exception generated?

    --mc
    sábado, 13 de marzo de 2010 2:37
  • System .Format exception
    lunes, 15 de marzo de 2010 16:18