Showing posts with label Windows Programming using CSharp. Show all posts
Showing posts with label Windows Programming using CSharp. Show all posts

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.

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.
         

Thursday, 4 April 2013

Example: Multi Document Interface







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 WinForms7
{
    public partial class MidParent : Form
    {
        public MidParent()
        {
            InitializeComponent();
        }

        private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1();
            f.MdiParent = this;
            f.Show();
        }

        private void arrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.MdiParent = this;
            f.Show();
        }

        private void form3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 f = new Form3();
            f.MdiParent = this;
            f.Show();
        }

        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void horizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void verticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }
    }
}

Multi Document Interface

  • While desdignng an application it can contain any no. of forms in it, right now to run the desired form we are explicitly specifying the form class name under program class. But when we provide the app to client we only give him the assembies of the project(ie IL code). So client doesnot have a chance to edit the client program and specify the form class name he wants to run.
  • To overcome the above problem we are provided with an approach known as multi document interfaces where in this approach an application will be having on form as a startup form which is refereed to as mdiparent or mdicontainer, so that clients dont require to change the form name under program class at any time. Rest of the forms in app will be under the control of mdiparent form and refered to as mdichilds. To make a form as mdiparent set the property ismdicontainer as true. . Note:An application can have only one mdiparent and all other fprms must be childs of parent which should come and sit under parent. Eg: VS is an mdiparent and launches all other forms like "New Project", "Add New Item" as its childs.
  • To launch a form as child of parent create object of child from class, set its mdiparent property with parent forms reference and then class show() method.
Layout:
  • When we have more than one child forms opened at a time under parent, child forms will be arranged inside the parent by using a layout for arrangement which can be any of the four options like
               - Cascade: Child forms are arranged one on top of the other.
               - TileVertical:  Child forms are arranged one beside the other.
               - TileHorizontal: Child forms are arranged one on top of the other.
               - ArrangeIcons: All child forms are arranged with in the bottom of the parent.

Example:ComboBox, ListBox and CheckBox List control




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 WinForms7
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("AP");
            listBox1.Items.Add("Orrisa");
            listBox1.Items.Add("Maharashtra");
            listBox1.Items.Add("MP");
            listBox1.Items.Add("UP");
            string[]cities={"Bengaluru","Chennai","Delhi","Hyderabad","Kolkata","Mumbai"};
            checkedListBox1.Items.AddRange(cities);
        }

        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
                if (comboBox1.FindStringExact(comboBox1.Text) == -1)
                    comboBox1.Items.Add(comboBox1.Text);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(comboBox1.Text);
            MessageBox.Show(comboBox1.SelectedItem.ToString());
            MessageBox.Show(comboBox1.SelectedIndex.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (object obj in listBox1.SelectedItems)
                MessageBox.Show(obj.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string str = null;
            foreach (object obj in checkedListBox1.CheckedItems)
                str += obj.ToString() + ",";
            str=str.Substring(0,str.Length-2);
            int pos=str.LastIndexOf(",");
            if(pos!=-1)
            {
                str=str.Remove(pos,1);
                str=str.Insert(pos,"and");
            }
            MessageBox.Show(str);
        }
       
    }
}

Example of RadioButton and CheckBox Control






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 WinForms7
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

          private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
                MessageBox.Show("Radiobutton1 is selected");
            else if
                (radioButton2.Checked)
                MessageBox.Show("Radiobutton2 is selected");
            else if
                (radioButton3.Checked)
                MessageBox.Show("Radiobutton3 is selected");
          }

        private void button2_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                MessageBox.Show("Checkbox1 is selected");
            if (checkBox2.Checked)
                MessageBox.Show("Checkbox2 is selected");
            if (checkBox3.Checked)
                MessageBox.Show("Checkbox3 is selected");
        }
       
    }
}