Write a Phone Book program using C# ADO .Net - Code

Latest

programs

Thursday, October 6, 2011

Write a Phone Book program using C# ADO .Net


using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace TestPhoneBook
{
    public partial class Form1 : Form
    {
        public Form1() {   InitializeComponent();    }
        OleDbConnection con;
        OleDbCommand cmd;
        DataSet ds;
        public OleDbDataAdapter dataAdapter;
        DataTable dataTable;
        int currRec;
        int totalRec;
        private void Constructing()
        {
            string connectionString = "provider=Microsoft.Jet.OLEDB.4.0; data source = c:\\db1.mdb";
            con = new OleDbConnection(connectionString);
            con.Open();
          dataAdapter = new OleDbDataAdapter("Select * FROM Pbook order by Cname", con);
            ds = new DataSet();
            dataAdapter.Fill(ds, "Prog");
            dataTable = ds.Tables["Prog"];
            currRec = 0;
            totalRec = dataTable.Rows.Count - 1;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Constructing();
            FillRecords(currRec);
        }
        private void InsertButton_Click(object sender, EventArgs e)
        {
            string name = NameTxt.Text;
            string number = PhoneTxt.Text;
            string cmdString = string.Format("Insert into Pbook values ('{0}','{1}')", name, number);
            cmd = new OleDbCommand(cmdString, con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Inserted SUcessfully");
        }
        private void UpdateButton_Click(object sender, EventArgs e)
        {
            string name = NameTxt.Text;
            string number = PhoneTxt.Text;
            string cmdString = string.Format("Update Pbook set CNum = '{0}' where Cname like '{1}'", number,name);
            cmd = new OleDbCommand(cmdString, con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Updated SUcessfully");
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {   string name = NameTxt.Text;
            string number = PhoneTxt.Text;
            string cmdString = string.Format("Delete from Pbook where Cname like '{0}'", name);
            cmd = new OleDbCommand(cmdString, con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Updated SUcessfully");
        }
        private void ExitButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Good Bye");
            Application.Exit();
        }
        private void FirstButton_Click(object sender, EventArgs e)
        {   currRec = 0;
            FillRecords(currRec);
        }
        private void NextButton_Click(object sender, EventArgs e)
        {   currRec++;
            if (currRec > totalRec)
                currRec = 0;
            FillRecords(currRec);
        }
        private void PreviousButton_Click(object sender, EventArgs e)
        {   currRec--;
            if (currRec < 0)
                currRec = totalRec;
            FillRecords(currRec);
        }
        private void LastButton_Click(object sender, EventArgs e)
        {  FillRecords(totalRec);
        }
        private void FillRecords(int current)
        {   NameTxt.Text = dataTable.Rows[current]["Cname"].ToString();
            PhoneTxt.Text = dataTable.Rows[current]["CNum"].ToString();
        }
        private void RefreshButton_Click(object sender, EventArgs e)
        {   NameTxt.Text = "";
            PhoneTxt.Text = "";
            con.Close();
            Constructing();
        }
    }
}


No comments:

Post a Comment