Wednesday 9 May 2012

How to Blink the Text from database in windows C# || How to Bind the Data from Database to a Label in Windows C#.Net

To implement this concept you need to follow the below steps :


Step1 :
First you need to design a table in Sql Database to retrieve the records from database.

Step2:
On the File menu, click New Project. Select Windows Forms Application as your project type.
Design the Form using controls from Toolbox.

Step3:
Now open the Form.cs page and write the following source code.

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private const int _blinkFrequency = 250;
        private const int _maxNumberOfBlinks = 1000;
        private int _blinkCount = 0;
        SqlConnection con = new SqlConnection("Data Source=sridhar;Initial Catalog=Employee;User ID=sa;Password=123");

        public Form1()
        {
            InitializeComponent();
            Bind();
            timer1.Interval = _blinkFrequency; //optional: set in design code
            label1.Text = "";
            timer1.Start();
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            this.label1.Visible = !this.label1.Visible;
            _blinkCount++;
            if (_blinkCount == _maxNumberOfBlinks * 2)
            {
                timer1.Stop();
                label1.Visible = true;
            }
        }

        private void Bind()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select * from Emp_Details", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Emp_Details");
            label1.DataBindings.Add("Text", ds, "Emp_Details.EmpName");

        }
    }
}


Step4:
Now build the Solution and Debug it for the output.

OutPut :


No comments:

Post a Comment