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;
    }
 }
7
  • Where do you get the exception, where is Label1 declared? Commented Nov 7, 2014 at 12:03
  • It's declared in the designer.cs as - protected global::System.Web.UI.WebControls.Label Label1; the error is thrown when selecting anything from the dropdown in debug mode Commented Nov 7, 2014 at 12:05
  • can you post you .cs side code? Commented Nov 7, 2014 at 12:54
  • yep - have edited above to include full code - thx Commented Nov 7, 2014 at 13:05
  • Can you try this one <asp:ListItem Text="Select One" Value=""></asp:ListItem> <asp:ListItem Value="Yes" Text="Yes"></asp:ListItem> <asp:ListItem Value="No" Text="No"></asp:ListItem> Commented Nov 7, 2014 at 13:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I know that - I want it to be NULL in the SQL unless the user has selected Yes or No
Then Specify it was string, as in string Result = "NULL"; in your code and store Result in SQL as Varchar field datatype
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>`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.