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.

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>

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.