-2
 protected void Page_Load(object sender, EventArgs e)
        {


         num = Convert.ToInt32(Request.QueryString["RegNo"].ToString());
            if (!IsPostBack)
            {
                BindTextBoxvalues();
            }    
        }

here it will display an error : Object reference not set to an instance of an object

divikdiya
  • 313
  • 3
  • 5
  • 14
  • 3
    Add a break point, debug it! and see which line throws the error. – Wheels73 Feb 01 '18 at 09:11
  • Somewhere in your code you are trying to get a property of a null value. Can you post the stacktrace so we can see on what line the error occurs? – Jerodev Feb 01 '18 at 09:12
  • 2
    Your code has multiple problems, from not using properties, not checking for null for query string, not checking if the data table has any records, not using proper naming for columns, not using Sql Parameters, not disposing sql connection, sqlDataAdapter. – mybirthname Feb 01 '18 at 09:22
  • yes i put a breakpoint and identify which part error occured. – divikdiya Feb 01 '18 at 09:23
  • num = Convert.ToInt32(Request.QueryString["RegNo"].ToString()); – divikdiya Feb 01 '18 at 09:23
  • 1
    Your query string is probably empty `.ToString()` throws the error – gvk Feb 01 '18 at 09:42
  • Your Request.QueryString["RegNo"] should contain only numbers. Other wise Convert.ToInt32 will return error – Kiran k g Feb 01 '18 at 09:47

2 Answers2

2

First you need to check that the querystring parameter is not null. Then you need to check that the value is actually an integer before trying to convert it. For this you can use the method Int32.TryParse. If the value is a convertible to an int, it will do the conversion for you at the same time.

For example:

if (Request.QueryString["RegNo"] != null) {
  int num = 0; 
  bool parseResult = Int32.TryParse(Request.QueryString["RegNo"].ToString(), out num);
  if (parseResult == true) {
    //valid number, so continue
    if (!IsPostBack)
    {
      BindTextBoxvalues();
    }    
  }
  else {
    //do something suitable here like display an error message or throw an exception, or continue without executing this particular piece of functionality, whatever is necessary for your application
  }
else {
    //do something suitable here like display an error message or throw an exception, or continue without executing this particular piece of functionality, whatever is necessary for your application
}

See https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx for details and more examples.

ADyson
  • 57,178
  • 14
  • 51
  • 63
1

You could use the following to avoid exception.

var regNo = Request.QueryString["RegNo"];
if(!string.IsNullOrEmpty(regNo) && IsDigitsOnly(regNo.ToString().Trim()))
{
 num = Convert.ToInt32(regNo.ToString());
}

private bool IsDigitsOnly(string str)
{
    foreach (char c in str)
    {
        if (!char.IsDigit(c))
        {
            return false;
        }
    }

    return true;
}
Kiran k g
  • 946
  • 11
  • 19
  • Worth noting that while this will work for this situation (since the RegNo in the DB is likely to be always positive) technically this doesn't strictly validate that the string is parseable to an int, since it will disallow negative integers (e.g. "-100"). So it's not re-usable as a general validation routine for checking integers. It's also probably less efficient that using the built-in TryParse method – ADyson Feb 01 '18 at 10:09
  • @Adyson `TryParse` wont prevent from null reference exception. If there is no query string value for `RegNo`, then `ToString()` on that will give null reference exception before `TryParse` – Bharadwaj Feb 01 '18 at 10:13
  • 1
    @KiranKG `regNo.ToString()` no need to call `ToString()` again as it is already a `string` – Bharadwaj Feb 01 '18 at 10:18
  • @Bharadwaj you're correct, I have amended my answer. Thanks. – ADyson Feb 01 '18 at 10:18
  • it works. i changed my coding . – divikdiya Feb 01 '18 at 10:50
  • This can simply be replaced by a call to int.TryParse. – Polyfun Feb 01 '18 at 12:15