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