-1

The following code works fine on the localhost but after publishing the functionality does not work neither does and it returns the following error. Any ideas?

"Load bug - System.ArgumentNullException: Value cannot be null. Parameter name: String
    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)  
    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)  
    at Main.Page_Load(Object sender, EventArgs e) in C:\Users\xyz\source\repos\zyc\bgfn\Main.aspx.cs:line 38"
if (!IsPostBack)
{
    try
    {
        if (ConfigurationManager.AppSettings.Keys.Count < 3)
        {
            ConfigurationManager.AppSettings["saved"] = DateTime.Now.ToShortTimeString();
            ConfigurationManager.AppSettings["houseLeft"] = "60";
            ConfigurationManager.AppSettings["carLeft"] = "30";
        }

        // PROBLEM IS THAT REPUBLISHING WILL RESET THE CONFIG AND HENCE ALL THE OFFERS - MOVE COUNTS TO DB

        int houseLeft = int.Parse(ConfigurationManager.AppSettings["houseLeft"]);

        int carLeft = int.Parse(ConfigurationManager.AppSettings["carLeft"]);

        ConfigurationManager.AppSettings["houseLeft"] = houseLeft.ToString();
        ConfigurationManager.AppSettings["carLeft"] = carLeft.ToString();

        redeemOne.Text = houseLeft + " left";
        redeemTwo.Text = carLeft + " left";
    }
    catch (Exception exc)
    {
        lblDebug.Text = "Load bug - " + exc.ToString();
        redeemOne.Text = "0" + " left";
        redeemTwo.Text = "0" + " left";
    }
}
GSerg
  • 71,102
  • 17
  • 141
  • 299
mac001
  • 35
  • 7
  • 2
    It would appear the comment in your own code answers your question? – GSerg Apr 20 '18 at 16:41
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – GSerg Apr 20 '18 at 16:43

2 Answers2

0

looks like you have ConfigurationManager.AppSettings["houseLeft"] and/or ConfigurationManager.AppSettings["carLeft"] as null. You may want to either check for null or put a try catch to handle the argument null exception

XP.
  • 52
  • 7
  • try/catch and handle exception should not be used to handle errors in code which could be safely determined. Exception handling is suggested method for handling errors while reading from a network stream eg. – dsdel Apr 20 '18 at 16:45
0

Instead of checking the key count, tried checking the single keys? Anyone can add/change appsettings to the app.config... : )

 if (ConfigurationManager.AppSettings["saved"] == null)
      ConfigurationManager.AppSettings["saved"] = DateTime.Now.ToShortTimeString();
 if (ConfigurationManager.AppSettings["houseLeft"] == null)
      ConfigurationManager.AppSettings["houseLeft"] = "60";
 if (ConfigurationManager.AppSettings["carLeft"] == null)
     ConfigurationManager.AppSettings["carLeft"] = "30";

Further I would safely try to convert to avoid exceptions - equal what the setting in app.config is:

if(!int.TryParse(ConfigurationManager.AppSettings["houseLeft"], out int houseLeft))
{
   //handle error...eg. setting a default value
  ConfigurationManager.AppSettings["houseLeft"] = "60";
}
dsdel
  • 982
  • 1
  • 6
  • 10