Thursday, 9 May 2013

Query String Parameters

     Query String Parameters are used to pass values from one page to another page. Query String parameters are created by using the ? symbol at the end of the url while navigating to a page. Next to ? symbol, specify the parameter nae followed by = operator followed by the value to pass using that parameter. When you have multiple parameters then separate them with & symbol. 
          
                      Server.Transfer("~/page2.aspx?pname=value&pname=value")

To read Query String parameters, use query string property of Request object.

                      Varname=Request.QueryString["pname"]

The following example creates two pages Querystring1.aspx and Querystring2.aspx and withen the 1st page within a gridveiw the details of departments are displayed with select option and when user select a department then the selected deptno will be passed to 2nd page through querystring parameter and within the second page the list of employeees working in that deptnowill be displayed in a gridveiw.

1.  Add a page to the website with the name querystring1.aspx, take a gridview on it, set the id of that gridveiw to Gvdept, set its AutoGenerateSelectButton property to true

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApp
{
    public partial class Querystring1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=localhost; database=db1;uid=cheryl;trusted_connection=true");
            SqlCommand cmd = new SqlCommand("Select * from dept", cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            gvdept.DataSource = dr;
            gvdept.DataBind();
            gvdept.DataKeyNames = new string[] { "Deptno" };
            cn.Close();
            dr.Close();
        }
        protected void gvdept_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            gvdept.SelectedIndex = e.NewSelectedIndex;
            string Dno = gvdept.SelectedValue.ToString();
            Response.Redirect("~/Querystring2.Aspx?Dno=" + Dno);
        }
    }
} 
2.  Add another page to the website with the name querystring2.aspx. Place a gridveiw on that page, set its id as GvEmp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApp
{
    public partial class Querystring2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string Dno = Request.QueryString["Dno"].ToString();
            string Sql = "Select * from emp where Deptno=" + Dno;
            SqlConnection cn = new SqlConnection("server=localhost; database=db1;uid=cheryl;trusted_connection=true");
            SqlCommand cmd = new SqlCommand(Sql, cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            gvemp.DataSource = dr;
            gvemp.DataBind();
            cn.Close();
            dr.Close();

        }
    }
}

Wednesday, 8 May 2013

LINQ to SQL (Example)

Example : Add a new form in project and design it as following.



Output :




Coding:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinqProject1
{
   public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        CS11DBDataContext dc;
        List cust;
        int rno = 0;
        private void Form2_Load(object sender, EventArgs e)
        {
            dc = new CS11DBDataContext();
            cust = dc.GetTable().ToList();
            ShowData();
        }
        private void ShowData()
        {
            txtCustID.Text = cust[rno].CustId.ToString();
            txtCname.Text = cust[rno].Cname.ToString();
            txtCity.Text = cust[rno].City.ToString();
            txtBalance.Text = cust[rno].Balance.ToString();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (rno0)
            {
                rno -= 1;
                ShowData();
            }
            else
            {
                MessageBox.Show("First record of the table", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        } 
    } 
}

LINQ to SQL

Microsoft has provided LINQ as a lightweight facade that provides a strongly typed interface to the underlying data stores. LINQ provides the means for developers to stay within the coding environment they are used to and access the underlying data as objects that work with the IDE, intellisense, and even debugging. With LINQ the queries you create now become first-class citizens within the .Net framework alongside everything else you are used to. When you work with queries for the data store you are working with, you will quickly realize that they now work and behave as if they are types in the system.

LINQ to SQL in particular is a means to have a strongly typed interface against a SQL Server database. You will find the approach that LINQ to SQL provides is by far the easiest approach to the querying SQL Server available at the momoent. It is important to remember that LINQ to SQL is not only about querying data, but you also are able to perform Insert/Update/Delete statements that you need to perform which are known as CRUD operations (Create/Read/Update/Delete). Visual Studio comes into strong play with LINQ to SQL in that you will find an extensive user interface that allows you to design the LINQ to SQL classes you will work with.

Example : 

Place a DataGridView control on the first Form of project, change the name of DataGridView as gdView and write the following code:

Output:

 

Coding :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;

namespace LinqProject1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CS11DBDataContext dc = new CS11DBDataContext();
            Table tab = dc.GetTable();
            dgview.DataSource = tab;
        }  
      
    }
}

Tuesday, 7 May 2013

Collections

     Arrays are simple data structures used to store data items of a specific type Although commonly used, arrays have limited capabilities. For instance you must specify an array size, and if at execution time you wish to modify it you must do so by creating a new array or by using array classes resize method, which creates a new array and copies the existing elements into the new array.
     Collections are a set of prepackaged data structures that offer greater capability than traditional arrays. They are reusable, reliable, powerful and efficient and have been carefully designed and tested to ensure quality and performance. Collections are similar to arrays but provide additional functionalities, such as dynamic resizing - They automatically increase their size at execution time to accommodate additional elements, inserting of new elements, removing of existing elements etc.
     Initially .net introduces so many collection classes under the namespace System.Collections like Stack, Queue, LinkedList, SortedList, ArrayList etc.

Monday, 6 May 2013

Query String Parameters

Query String parameters to pass values from one page to another page. Query string parameters are created by using ? symbol at the end of the url while navigating to a page. Next to the ? symbol, specify the parameter name followed by= operator  followed by the value to pass using that parameter. When, you have multiple parameters then seperate them with & symbol.

Server.Trnasfer("~/page2,aspx? pname=value& pname=value");

To read query string parameters, use QueryString property of Request object.

Varname=Request.QueryString["pname"];

Saturday, 4 May 2013

Crystal Reports

         Crystal Reports is a business intelligence application used to design and generate reports from a wide range of data sources. Several other applications, such as Microsoft Visual Studio bundle and OEM version of Crystal Reports as a general purpose reporting tool. Crystal Reports become the de facto standard report writer when Microsoft released it with Visual Studio.
       The product was originally created by crystal services Inc. as Quik reports when they could  not find a suitable commercial report writer for their accounting software. After producing versions 1.0 through 3.0, the company was acquired in 1994 by Seagate Technology. Crystal Services was combined with Holostic Systems to form the information group of Seagate software, which later rebranded as crystal decisions, and produced versions 4.0 through 9.0. Crystal decisions was acquired in December 2003 by business objects, which has so far produced versions 10, 11 and current version 12.
         

Wednesday, 1 May 2013

Session State Management

Session is a block of memory on server allocated separately for every user of the website and is used to store the data that is private to a particular user. For the applications like online shopping session state is required for storing items selected by the user in different pages and finally gets the list of session and prepares the bill

Session has the following advantages over Cookies
  • Session data is stored on server and then there will be security for the data. But cookies are stored on client system in plain text file and hence there is no security for the data.
  • Session can store any type of data whereas a cookie can store only standard type of data like int,float,double,string,date.
  • Session has no limit and you can store any type of data in the session whereas a cookie can store a maximum of only 4KB data and overall 80KB if the browser allows a maximum of 20 cookies per website.
  • Session state cant be disabled by an end user and hence the code within using session will work whereas cookies can b disables with end user in browser and hence there is no guarantee that your code that uses cookies will work.