1

I have a dropdown containing blank, Yes and No. If the user selects No I want Label1 to say "No further info required".

I'm getting a nullreferenceexception when I debug - The value in SQL db of the field is NULL when the user gets to using the dropdown, I want them to be able to select Yes or No and also if they have selected and stored "Yes" or "No" previously, I also want them to be able to go back in and select blank which will feed NULL back into the database.

What's the simplest way of handling this? I assume the error is thrown because of the NULL value in the DB?

Code:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
             SelectedValue='<%# Bind("BlahBlah") %>' 
             onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem>Yes</asp:ListItem>
            <asp:ListItem>No</asp:ListItem>
         </asp:DropDownList>`

full cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FrontEnd_v1
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (DropDownList1.SelectedValue == "No")
        {
            Label1.Text="No Further Info Required";
        }

        else
        {
            Label1.Text="";
        }



    }
}

}

designer.cs:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace FrontEnd_v1 {


public partial class WebForm1 {

    /// <summary>
    /// FormView1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.FormView FormView1;

    /// <summary>
    /// RM_Remediation control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.SqlDataSource SQLSource;


    /// <summary>
    /// Button1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.Button Button1;

    protected global::System.Web.UI.WebControls.DropDownList DropDownList1;

    protected global::System.Web.UI.WebControls.Label Label1;
    }
 }
user3294008
  • 61
  • 3
  • 9

2 Answers2

1

If it is showing NULL in the SQL, it means that no value was inputted at it's place. You might wanna re-embed values into your DB because a NULL is virtually empty field.

SanyTiger
  • 666
  • 1
  • 8
  • 23
1

try this code. Add Value in ListItem.

  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
         SelectedValue='<%# Bind("BlahBlah") %>' 
         onselectedindexchanged="DropDownList1_SelectedIndexChanged">
      <asp:ListItem></asp:ListItem>
      <asp:ListItem Value="Yes" Text="Yes"></asp:ListItem>
      <asp:ListItem Value="No" Text="No"></asp:ListItem>
    </asp:DropDownList>`
Genish Parvadia
  • 1,437
  • 3
  • 17
  • 30