SQL Server: Error Handling

Error handling in SQL Server is similar to C#’s exception handling.  Syntax is below:

BEGIN TRY
    -- this is where you put your statements
END TRY
BEGIN CATCH
    -- this is where you handle the error
END CATCH

 

 

And of course you can get more information about the error using the following:

  • ERROR_PROCEDURE() – returns name of stored procedure or trigger where the error occurred
  • ERROR_LINE() – returns the line number in the procedure or trigger where the error occurred
  • ERROR_NUMBER() – returns the error number
  • ERROR_MESSAGE() – returns the error message

 

The following might also be useful in handling your errors:

  • @@TRANCOUNT – holds the # of transactions ongoing; useful if you are using transactions and want to know if you have pending transactions after catching an error (IF @@TRANCOUNT > 0) so you can maybe rollback
  • @@PROCID – returns the object ID of the current stored procedure, UDF, or trigger
  • OBJECT_NAME() – returns the name of the database object referred to by the object ID; passing @@PROCID will return the name of the current stored procedure, UDF, or trigger (OBJECT_NAME(@@PROCID)
  • USER_NAME() – returns database user name (e.g. dbo); for more info go here
  • SYSTEM_USER – returns the current Windows or SQL Server login name depending on how the user is logged in; for more info go here
Advertisements

HTML, CSS, Javascript, and jQuery: Putting them all together (Part 3)

jQuery is one of the Javascript frameworks out there, and it’s the most popular.  Basically it lets you write less Javascript code and do complicated things using simple calls to the framework. 

To use jQuery in your HTML page, first you need to download the library from it’s website jQuery.com.  Just like adding an external Javascript file, you can point to the jQuery library you just downloaded using the <script> tag’s src attribute. 

Another way to add jQuery to your HTML page without downloading and hosting it yourself, is to include it from a CDN (Content Delivery Network)  like Google and Microsoft.  You can save download time this way if users have visited others sites that point their jQuery to one of these CDNs.

jQuery’s syntax revolves around the idea of selecting or querying HTML elements and performing action on the elements.  Below is the basic syntax:

$(selector).action() 

 

The $ indicates it’s a jQuery.  So if you see $ in an HTML page source, you know it’s using jQuery.  The selector is pretty much like the CSS selector.  The action defines what action needs to be done on the selected element.  Below is an example jQuery code:

<head>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js">
</script>
<script>
$(document).ready(function(){
$("p").click(function(){
alert("I'm a jQuery code!!!");
});
});
</script>
</head>

In the example above, I pointed my jQuery library to Microsoft’s CDN in the first <script> tag.  My jQuery code is located on the second <script> tag.  What it does is display a message when the <p> tag is clicked, same as in my previous post but this time using jQuery.  It still uses Javascript coding but it adds it’s jQuery syntax on top of it.  You will notice that the code is wrapped inside the jQuery document ready event.  This is the usual practice when doing jQuery to ensure that the document is fully loaded.

So now you know how HTML, CSS, Javascript, and jQuery ties together, it’s time to move forward to more meaty stuff, get more familiar and learn more about what these technologies can do.  This ends my 3-part post on this topic.  Below are the links to my other parts of this post:

HTML, CSS, Javascript, and jQuery: Putting them all together (Part 2)

On my previous post, I talked about HTML and CSS.  Now I’m going to talk about Javascript

Javascript is a popular scripting language that makes an HTML page dynamic.  To add Javascript code, you use the <script> tag and you can put them in the <head> or in the <body> section of the HTML page.  It is recommended to put all of them in the <head> section.  Below is an example of a Javascript function being called on a click event of the <p> tag.

<head>
<script type="text/javascript">
function myFunction()
{
alert("Hello, Universe!!!");
}
</script>
</head>
<body>
<p style="font-weight: bold; color: #0000FF" onclick="myFunction()">
Hello, World!!!
</p>
</body>

 

Remember that Javascript code are executed as the HTML page is being parsed.  The example below will display the message when the HTML page is being loaded without having to wait for any event.  Notice that the Javascript code is not contained in a function declaration like the example above.

<head>
<script type="text/javascript">
alert("Javascript code not belonging to any function");
</script>
</head>

 

If you have lots of Javascript code, it is preferred to put them in an external file with extension .js and use the <script> tag’s src attribute to point to this file.  That way you can also use this file in the other HTML pages.

<head>
<script src="MyJavaScript.js"></script>
</head>
<body>
<p style="font-weight: bold; color: #0000FF"
onclick="myAnotherFunction()">
Hello, World!!!
</p>
</body>

 

The file MyJavascript.js contains the following Javascript code:

function myAnotherFunction()
{
alert("Hello, Big Bang!!!");
}

 

Next post, I’ll cover the jQuery.

HTML, CSS, Javascript, and jQuery: Putting them all together (Part 1)

HTML describes how the web page will look like.  Below is an example of an HTML page (created using Visual Studio 2012 by the way).  You have the <!DOCTYPE>, <html>, <head>, and <body> tags as always.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example HTML Page</title>
</head>
<body>
<p> Hello, World!!! </p>
</body>
</html>

 

CSS was created to solve the problem of formatting an HTML page.  Everything that has got to do with style and layout should be done through CSS.  So how do we put CSS in an HTML page?  There are 3 ways:

  • Inline style.  This is the least preferred way to add CSS because you will be mixing content with presentation.  Below is an example of an inline style.  You have the style attribute added to an HTML tag which can contain any CSS property inside the “”.
<p style="font-weight: bold; color: #0000FF">
Hello, World!!!
</p>

 

  • Internal style sheet.  You use this if you want to apply the style throughout the page.  The example below will style all <p> tags that are defined in the HTML page.  You use the <style> tag under the <head> section of the HTML page to contain your CSS declarations.
<head>
<style type="text/css">
p {
font-family: Arial, Helvetica, sans-serif;
font-style: italic;
color: #00FF00;
}
</style>
</head>

 

  • External style sheet.  This is the preferred method.  Not only is the presentation separate from the content, but it can be reused in other HTML pages.  Below is how you add an external style sheet to an HTML page.  You use the <link> tag under the <head> section of the HTML page.
<head>
<link href="MyStyleSheet.css" rel="stylesheet" type="text/css" />
</head>

 

The file MyStyleSheet.css contains the following CSS declarations:

p {border-style: solid; background-color: #00FF00;}

 

What if we have a CSS property declared in more than one place?  If that happens, overriding will take place, with the inline style being the highest priority overriding the ones declared in both the internal or external style sheet, with internal style sheet second, and external style sheet third.  Also, if you place the <link> tag after the <style> tag, the external style sheet will override the internal style sheet.

What is the syntax for declaring CSS property then?  Easy.  You just have to specify a selector and one or more declarations.  Selector would be, in our example above, p, an HTML tag.  It can also be the tag’s attribute id or class, or many others as well which I will be discussing on another post geared towards CSS.

Declarations are the name: value; pairs inside the curly braces {}.  In our example above, declarations would be the {border-style: solid; background-color: #00FF00;}.  You can see the CSS property border-style is set to solid value.

W3Schools has a nice visual way of explaining the CSS syntax:

 

So that’s it for HTML and CSS.  On my next post, I will be talking about the Javascript and jQuery part.

C#: How many versions of C# do we have right now?

The latest version of C# that came with Visual Studio 2012 and .NET Framework 4.5 is 5.0.  For those who are still working on .NET Framework 2.0, the version of C# you are using is 2.0.  Between these, you have 3.0 and 4.0.  So naturally you might ask what are the differences between these versions?  Don’t fear, help is here ;).  Fortunately, there are some information on the Internet that can provide you with these differences and in more detail than I can possibly explain here in my post.  Below are the links you can go to.  Enjoy!