Monthly Archives: March 2014

Minimizing the Error (Exception) in Web and Software development

Sometimes we create a code to execute for us. Let’s say, we create a calculator app. In which we have many classes; which include data and methods. Each class has its own specified set of data and methods on which it works.

So, now that you’re having a class of Calculator, and it has some data as value1 and numerous methods. For example you’re having something like a Mathematics class and all of its methods. I won’t use any of the methods that is defined inside a language, because this tutorial is not only for ASP.NET, but is also applicable, to all other programming languages too. Such as Java, C++ and some other Object-oriented languages. I am not aware of PHP, if it applies, do let me know.

@{
  var calc = new Calculator(); // create an calculator class object
  /* variable declaration */
  var val1 = 30;
  var val2 = 15;
  /* expressions and all other activity here... */
  var result = 0;
  result = (val2 * 2) - val1; // we know its gonna be 0
}

Now, lets continue. We have a simpe program, to calculate the result from these 2 provided variable. Please note that this code was for those, who are using ASP.NET, for those you might be using Java the above code would be changed to the following:

public class fileName {
  public void main () {
    Calculator calc = new Calculator(); // create an calculator class object
    /* variable declaration */
    int val1 = 30;
    int val2 = 15;
    /* expressions and all other activity here... */
    int result = 0;
    result = (val2 * 2) - val1; // we know its gonna be 0
  }
}

Ummm, why hate C++? Here is the code for that too:

int main () {
  Calculator calc = new Calculator(); // create an calculator class object
  /* variable declaration */
  int val1 = 30;
  int val2 = 15;
  /* expressions and all other activity here... */
  int result = ;
  result = (val2 * 2) - val1; // we know its gonna be 0
  return 0;
  /* can use void to prevent the return line */
}

Now, you might have come to know about what was done here. There was a simple subtraction, now the user might want to get another result using a division method. Such as:

var some = 2;
var res = some/result;

Boing! Here you get an error, which is known as Exception in the language of Programming. These are the errors that the are generated on the go. In this case it is:

 System.DivideByZeroException

There are many other Exceptions like this, which are annoying. In Web development, they are shown to users which are annoying and are not that much user-friendly. Secondly, in the Software development, these exceptions can break the operation and close the entire software. Too much annoying for the user, that he might uninstall your app.

To control these, you use a block that is known as try catch block. In the try section you write the code to be tried by the Compiler or the machine and in the Catch block you tell compiler what to do if an exception occurs. For example

try {
  /* all your code here to be tried... */
} catch (Exception e) { /* catch all exceptions.. */
  Response.Write("Following error generated: " + e.Message);
}

This way, you can check if any exception occurs and you can do anything related to it. You can show him an error popup, that might be less annoying or even sometimes helpfull too. You can just say: ‘Sorry, you cannot divide an integer by a zero’, instead of ‘Exception detailed CSxxxx: System.DivideByZeroException, blah and blah and blah’.

Try some other codes and tell me where I might be missing some information.

Windows app development

Well, since the upgrade of Windows 8, developers can now develop apps in HTML and CSS. Windows 8.1 has some other built-in bug fixes too. But this feature came up by the upgrade of OS from Win 7 to Win 8.

Now, we can create a simple and easy HTML software for Windows API. We can stylize it using CSS, and we can work on the events using JavaScript, man please no more JavaScript talk, I am sick of it. jQuery would be used here more oftenly, but still we would need to stick to pure (or native) JavaScript in some places where we would be handling the OS based messages.

So, let us get our selves warmed up for Windows Programming too. It’s just been made easy for us!

For more go to: http://msdn.microsoft.com/en-us/windows/apps/br229512

Dynamic Content on a Website

Sometimes you have to show a lot of content to the user, but don’t want to create multiple pages for each of the object. For example, if you were a shopkeeper or a baker or cook. You might not want to create a seperate page for cake.html, cookie.html, chicken-soup.html. That is pretty sick right?

That’s where you use the Query Strings or the Url Data. You just append a data to your URL, and on the server-side, you use that data from the URL, and change the content on the web page. That would be just like, changing the switch from ON to OFF.

Let’s continue our example. If you were the developer of your website for your bakery. You might have to write the code in each of the page. And at the same time, you might to make a new page if your bakery gets a new item. Then your file system would have been like:

  1. Default.html (or index.html)
  2. About.html
  3. Contact.html
  4. Order.html
  5. Cake.html
  6. Cookie.html
  7. Apple-pie.html
  8. Juice.html
  9. …etc

But, you can reduce this to just one page. Yes! Lets change the File System to this:

  1. Default.html (or index.html)
  2. About.html
  3. Contact.html
  4. Order.html
  5. Item.html
  6. …etc

If you’re using ASP.NET Web Pages, that we’re using since the beginning. Then you can try this code out, you will find it really simple, handy and amazing.

The example URL would be like: http://www.example.com/item/item-id-or-item-name

@{ 
   var item = UrlData[0]; /* get the urldata */
   var content = "";
   if(item == "somevalue") {
     content = "Cakes are good!";
   } else if (item == "someothervalue") {
     content = "Cookies are fresh!";
   }
}

<div>@content</div>

Now, I will explain the code, the code takes the URL data of the page, you must keep a note that the content placed infront of the page name can be accessed. In the example we had:

http://www.example.com/page-name/url-data-content-here-infront-of-page

Then I used the ASP.NET to get the first part, note the [0] first parameter after the page name. And then used an if else block to show the dynamic content depending on the item-name.

Lets do this code with out own example:

http://www.example.com/item/cake/purchase

@{
   var item = UrlData[0];
   var action = UrlData[1]; /* notice the paramter */
   var response = "";
   var content = "";
   if(item == "cake") {
     content = "Cakes are good for birthday party!";
   }
}

<div>
   <p>@content</p>
   @if(action == "purchase") {
     <a href="buy-item/@item">CHECK OUT</a>
   } else {
     <a href="item/@item/purchaase">PURCHASE THIS</a>
   }
</div>

This is a sample, you can see that the code would execute and would check for the URL, and inside the URL it would change the content of the Website. You can have as many URL parameters as much you want and they are better to read too. You can add actions, such as feedback, purchase, contact email sent etc.