Commonly Used C# Snippets for SQL CRUD Operations

Commonly used code snippets while in C# & SQL while reading and writing data to database.


        public void GetDataSetWithParam()
        {
            try
            {
                using (SqlConnection con = new SqlConnection(str))
                {
                    using (SqlCommand cmd = new SqlCommand("sp_GetEmpWithId", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("Id", 1);
                        SqlDataAdapter adp = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        adp.Fill(ds);
                    }
                }
            }
            catch (Exception exp)
            {

            }
        }

        public void InsertWithParam_In_Out()
        {
            try
            {
                using(SqlConnection con=new SqlConnection(str))
                {
                    using (SqlCommand cmd = new SqlCommand("sp_InsertEmp", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("Name", "Jayesh");

                        SqlParameter outParam = new SqlParameter("ReturnId", SqlDbType.Int,1000);
                        outParam.Direction = ParameterDirection.Output;

                        cmd.Parameters.Add(outParam);
                        con.Open();

                        cmd.ExecuteNonQuery();

                        int returnValue = (int)cmd.Parameters["ReturnId"].Value;
                    }
                }
            }
            catch (Exception exp)
            {

            }
        }


        public void GetData_UsingReader()
        {
            using (SqlConnection con = new SqlConnection(str))
            {
                using (SqlCommand cmd = new SqlCommand("sp_GetAllEmps", con))
                {
                    SqlDataReader rdr = null;
                    con.Open();
                    rdr = cmd.ExecuteReader();

                    List empList = new List();
                    Employee emp = null;
                    while (rdr.Read())
                    {
                        emp = new Employee();
                        emp.Name = rdr["Name"].ToString();
                        emp.Id = rdr["Id"].ToString();
                        empList.Add(emp);
                    }
                }
            }
        }

Aligning a Span inside a Div

Being from a Asp.NET background, I never took much interest in designing HTML pages. Recently, I started learning HTML to get a deeper insight into the problems faced when converting a photo shop design to HTML code.
In the coming few weeks, I'll share some issues and their solutions faced in my HTML learning journey. The issues might be trivial but would be useful to some one who's just started learning HTML and CSS.

The present issue is how to align a span inside a div. Have a look at the below shown codepen. I'm trying to align the span with text Tech Illumination to the center of the parent div.

See the Pen WbWyZE by Jay (@jay3dec) on CodePen.



The solution to the above problem is line-height. The line-height property sets equal space, above and below the element. Suppose if I set line-height:50px; then 25px space will be set above and below the particular element. Here is the corrected demo.

See the Pen EaJRwr by Jay (@jay3dec) on CodePen.

What does __name__=='__main__' in Python mean ??

All python beginners, once in a while must have stumbled upon the below piece of code.


 __name__=='__main__' 

What does it actually mean and what is the use of the above piece of code. We'll see what the above code does and its relevance with the help of an example.

 Lets create a simple python program hello.py with a function called foo

#!/usr/bin/python
# hello.py

def foo():
    print 'foo is called'

foo()
If you try running the above program, you should have the following output on the console.

foo is called
Now, suppose we need to import this hello.py file into another python program in order to access the foo function.
Let's create another python program called world.py. In world.py we'll import our hello.py and try to call foo. Here is how world.py would look like:

#!/usr/bin/python
# world.py

import hello

def bar():
    hello.foo()

bar()

Now, if you execute world.py, you should have the following output on the terminal console;

foo is called
foo is called

We'll as you saw the message gets printed twice. The simple solution would be to remove the foo function call from hello.py. But in that case it won't print the message when we execute hello.py. Well, this is where

__name__=='__main__'

comes into the picture. When a python program starts to execute, it sets a few special variable, __name__ is one of them. It contains the value __main__ if it's the main program being executed. But it won't have the value as __main__ being set, if it's being imported and executed, since it's not the main program when imported.
Let's modify hello.py to include the checking for __name__

#!/usr/bin/python
# hello.py

def foo():
    print 'foo is called'

if __name__ == '__main__':
    foo()
Now, if you try running the world.py program it won't print the message twice. The only message that is printed is of the hello.foo function call.

Call asmx web service from jQuery Ajax

I had earlier posted a tutorial on how to call c# server side methods using JavaScript. It made use of Page Method provided by .NET framework.
In this tutorial, we'll see how to call an asmx service web method using jQuery Ajax. So lets start by creating a web application in Visual Studio. Download and include the latest jQuery in your html or aspx page.
<script src=" jquery-2.1.1.js="" scripts="" type="text/javascript"></script>

How to Check 3G/ Wifi Dongle/ Data Card Usage in Ubuntu Linux

3G WiFi dongle or data card come in handy, when it comes to using internet on the go. Simply plug in the dongle and you are ready to go online. 3G dongle has completely revolutionized the way we use internet today.

I have been using 3G Airtel dongle for quite a while now. The 3G dongle was quite easy to install and use. In windows 7 on plugging in, it automatically installed the required software and within minutes it was ready to be connected. The interface was quite user friendly with options to check the usage balance too.

JavaScript JSON Encode/Decode with Asp.Net Sample Code

JavaScript JSON Encode/Decode with Asp.Net Sample Code


In this article, we'll discuss how to Encode and Decode JavaScript JSON arrays when passing it from client to server side and vice versa using Asp.Net WebMethods.
We encode JavaScript object into JSON when we need to pass it from client side to server side. Decoding is done on the server side, so that the JavaScript object can be converted  into a C# class object. Consider for instance that we need to collect a few details about an employee and pass it to the server side to a web method.


Installing Python Matplotlib in Ubuntu 12.04

Installing Python Matplotlib in Ubuntu 12.04 directly was quite a mess and I ended up with some weird errors.
A cleaner and smoother way to install matplotlib was using Pip. I wonder why it never crossed my mind given that fact, I had already used Pip for installation in the Python Flask MySQL tutorial.

In order to install matplotlib in Ubuntu 12.04, run the following code :

sudo apt-get install python-pip
easy_install -U distribute
sudo apt-get install libfreetype6-dev libpng-dev
sudo pip install matplotlib
Keep watching this space for a tutorial on getting started with creating graphs using Python and Matplotlib.

JavaScript JSON Array Revisited

JavaScript JSON Array Revisited

In this article, we'll discuss all about JavaScript JSON Array. We'll see how to create a JSON object in JavaScript and also how to convert a JSON string to JavaScript object.

JSON stands for JavaScript Object Notation. It is a text format which we normally use to exchange information between a client and server. It's independent of the language you use and almost all languages support JSON. In this article, we'll see how to convert JavaScript to JSON.