locked
How do I prevent the ViewState of a dropdown throwing an error if no option is selected? RRS feed

  • Question

  • User1158330421 posted

    I tried put in try catch :

    string userMeasurement = ViewState["DropDown"].ToString();

    try

    {

    if (userMeasurement == null && userMeasurement == "")
    {
    showError("Please select a measurement type");
    return;
    }

    }

    catch(Exception)

    {

    showError(Please enter valid measurement;

    return;

    }

    The first line with ViewState keeps throwing NullReferenceException even when I tried to wrap the String userMeasurement line in the try 

    Tuesday, July 3, 2018 8:08 AM

All replies

  • User283571144 posted

    Hi kevinfarrell2k,

    The first line with ViewState keeps throwing NullReferenceException even when I tried to wrap the String userMeasurement line in the try 

    According to your description, I suggest you try to use as keywrod to aviod throwing the exception.

    From MSDN article.

    The as operator is like a cast operation. However, if the conversion isn't possible, asreturns null instead of raising an exception.

    More details, you could refer to below demo codes:

    ASPX:

            <div>
                <asp:DropDownList ID="DropDown" runat="server">
                    <asp:ListItem Value="aaa" Text="aaa"></asp:ListItem>
                    <asp:ListItem Value="bbb" Text="bbb"></asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
             </div>

    Code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace AspNetNormalIssue.Webform
    {
        public partial class DropDownListString : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                    string userMeasurement = ViewState["DropDown"] as string;
                    if (userMeasurement == null && userMeasurement == "")
                    {
                         return;
                    }
                }
                catch (Exception)
                {
                    return;
    
                }
            }
        }
    }

    Result:

    Best Regards,

    Brando

    Wednesday, July 4, 2018 3:21 AM
  • User1158330421 posted

    Thanks Brando, but I get an error because the userMeasurement appears in a switch statement after and if I use this solution then userMeasurement isn't recognised because it doesn't exist in the current context.

    Wednesday, July 4, 2018 8:16 AM
  • User-1171043462 posted

    You don't need any Try Catch. Just use Convert.ToString() instead of ToString() function

    Convert.ToString() will not throw error even if the object is NULL.

            string userMeasurement = Convert.ToString(ViewState["DropDown"]);
            if (!string.IsNullOrEmpty(userMeasurement))
            {
                showError("Please select a measurement type");
                return;
            }

    Wednesday, July 4, 2018 6:16 PM