Monday 30 April 2012

Pie Chart Control

How to Bind Data from DataBase to Pie Chart control in Windows using C#

In this article i am  using a class which has database connection to refer it click here
Write a store procedure for binding and name it as "Student_Marks "
Form.CS Code :
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.Collections;
namespace Pie_Charts_Sample
{
    public partial class Form1 : Form
    {
        SqlHelper sql = new SqlHelper();
        public Form1()
        {
            InitializeComponent();
            ShowPieChart();
         
        }
        private void ShowPieChart()
        {
            Hashtable ht = new Hashtable();
            DataSet ds = sql.ExecuteProcudere("Student_Marks ", ht);
            DataTable dt = ds.Tables[0];
            PieChart.DataSource = dt;
            PieChart.Series["Series1"].XValueMember = "Name";
            PieChart.Series["Series1"].YValueMembers = "Percentage";
            PieChart.DataBind();
        }
    }
}
OutPut : 

Friday 27 April 2012

Inserting and Retreiving Data from Database using 3-tier Architecture in Windows form || Inserting and Retriving Images into database in Window Forms

Step 1: 

Open Visual Studio->New project->Window form Application
Create a table in database for storing images ,Set increment property for  Id


Create Store procedure for insert values in to database and name it as "testinsert" 
Create Store procedure for Retrieving values from database and name it as "testshow"

Step 2:

 For 3-tier Architecture first open visual studio add new project->class library for DataAccess layer name as Dal in that take one class as Dalcls similarly add new project to the solution for Bussiness layer  name as Bal in that take one class as Balcls.

Step 3: 

In this article am taking one sqlhelper class for database connection .Refer sqlhelper class by Click here

Design the form as below: 

The Form1[Designer].cs looks like this


 

And write the following code in Dal class:
Dalcls.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace DAL
{
    public class dalcls
    {
        SqlHelper sh = new SqlHelper();
        public int insertimage(string name ,byte[] photo)
        {
            Hashtable ht = new Hashtable();
            ht.Add("@name", name);
            ht.Add("@image", photo);


            int result = sh.ExecuteQuery("testinsert", ht);
            return result;
        }
        public DataSet showimage(string name)
        {
            Hashtable ht = new Hashtable();
            ht.Add("@name", name);
            DataSet ds = sh.ExecuteProcudere("testshow", ht);

            return ds;
        }

    }
}


write the following code in Bal class:
Balcls.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data;

namespace BAL
{
   public class balcls
    {
       dalcls dal = new dalcls();
       public int insertimage(string name, byte[] photo)
       {
           return dal.insertimage(name,photo);
       }
       public DataSet showimage(string name)
       {
           return dal.showimage(name);
       }
    }
}



write the following code in Form1.cs
Form1.cs

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.IO;
using System.Collections;
using BAL;

namespace Insertimage3tier
{
    public partial class Form1 : Form
    {
        balcls bal = new balcls();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            MemoryStream ms1 = new MemoryStream();
            pictureBox1.Image.Save(ms1, pictureBox1.Image.RawFormat);

            byte[] data = ms1.GetBuffer();
            Hashtable ht = new Hashtable();
            ArrayList arry = new ArrayList();
            arry.Add(pictureBox1 );
            arry.Add(txtname.Text);
            int result = bal.insertimage(Convert.ToString (txtname.Text),data);
            if (result > 0)
            {
                MessageBox.Show("image inserted");
            }

        }

        private void btnupload_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Image Files|*.gif;*.jpg;*.png;*.bmp";

            openFileDialog1.ShowDialog();

            pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
        }

        private void btnRetrieve_Click(object sender, EventArgs e)
        {
            DataSet ds = bal.showimage(txtname.Text);
            int c=ds.Tables [0].Rows .Count;
            if (c > 0)
            {
                Byte[] b = new Byte[0];
                b = (Byte[])(ds.Tables[0].Rows[c - 1]["Image"]);
                MemoryStream ms1 = new MemoryStream(b, true);
                pictureBox1.Image = Image.FromStream(ms1);
            }

        }
    }
}

Output:



 

 

Sunday 22 April 2012

DataGridView in Windows Form

How to Insert, Edit, Update and Delete Data in DataGridView in Windows Form.


Form.cs Code :
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.Configuration;
using System.Data.SqlClient;
namespace savedatatomdf
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sqlcon"].ConnectionString);
        public Form1()
        {
            InitializeComponent();
            Bind();
        }
        private void Clear()
        {
            txtName.Text = string.Empty;
            txtLocation.Text = string.Empty;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert Into Test_Data(Name,Location) Values (@Name,@Location)", con);
            cmd.Parameters.AddWithValue("Name", txtName.Text);
            cmd.Parameters.AddWithValue("Location", txtLocation.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Inserted sucessfully");
            Bind();
            Clear();
        }
        private void Bind()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from Test_Data", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            SqlCommand delcmd = new SqlCommand();
            if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
            {
                delcmd.CommandText = "DELETE FROM Test_Data WHERE ID=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
                con.Open();
                delcmd.Connection = con;
                delcmd.ExecuteNonQuery();
                con.Close();
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                MessageBox.Show("Row Deleted");
            }
            Bind();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Update Test_Data set Name=@Name,Location=@Location Where(Name=@Name)", con);
            cmd.Parameters.AddWithValue("@Name", txtName.Text);
            cmd.Parameters.AddWithValue("@Location", txtLocation.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("updated......");
            con.Close();
            Bind();
            Clear();
        }
        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            txtName.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            txtLocation.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
        }
    }
}
Output :

How to Bind Data to ComboBox from Database in Windows Form || How to Set Default Select Item in ComboBox.

Binding ComboBox in Windows


Form.cs Code :
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.SqlClient;
namespace combox_binding
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=sridhar;Initial Catalog=employee;User ID=sa;Password=123");
        public Form1()
        {
            InitializeComponent();
            bind();
        }
        private void bind()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select empID,empname from emp", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataRow dr;
            dr = dt.NewRow();
            dr.ItemArray = new object[] { 0, "---Select an item---" };
            dt.Rows.InsertAt(dr, 0);
            comboBox1.DisplayMember = "empname";
            comboBox1.ValueMember = "empID";
            comboBox1.DataSource = dt;
            con.Close();
        }
    }
}
OutPut : 

Thursday 19 April 2012

How to use Marquee in Window forms using Timer control

To apply a marquee on the text with in time period in window forms using Timer control follow the steps:

1.  Open the Visual Studio 2010-->File -->New-->Project-->Select Windows Form Application         -->ClickOK
2. Design the form from  Tool Box first take one Label and one Timer Control.
3. Change the Timer and Label Names.
4. Double click on Timer control then Write the Below Code in Form1.CS  File

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.SqlClient;

namespace Marqueelabelin_Winform
{
    public partial class Form1 : Form
    {
        private int xPos = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Width == xPos)
            {
                //repeat marquee
                this.label1 .Location = new System.Drawing.Point(0, 40);
                xPos = 0;
            }
            else
            {
                this.label1.Location = new System.Drawing.Point(xPos, 40);
                xPos++;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
             
                 lblText.Text = "ALLEN";
                 timer1.Start();
          

        }
    }
}


 

How to Blink the Text in Windows Form Applications using with Timer control in C#.net ||Basic Timer control Example with window forms in C#.net

To blink the text with in time period in window forms using Timer control follow the steps:

1.  Open the Visual Studio 2010-->File -->New-->Project-->Select Windows Form Application         -->ClickOK
2. Design the form from  Tool Box first take one Label and one Timer Control.
3. Change the Timer and Label Names.
4. Double click on Timer control then Write the Below Code in Form1.CS  File
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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
      
        private const int _blinkFrequency = 250;
        private const int _maxNumberOfBlinks = 1000;
        private int _blinkCount = 0;
       
       public Form1()
        {
            InitializeComponent();
            timer1.Interval = _blinkFrequency; //optional: set in design code
            label1.Text = "Welcome ";
           
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
           
            this.label1.Visible = !this.label1.Visible;
            _blinkCount++;
         
            if (_blinkCount == _maxNumberOfBlinks * 2)
            {
                timer1.Stop();
                label1.Visible = true;
              
            }
        }
       
    }
}
 5. Run your application then you will get output like below: