Search This Blog

Sunday, July 3, 2011

DataBase Connection C# Simple Programming

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

namespace Prodectbaseproject
{
  
    public partial class Login : Form
    {
        DAL Obj = new DAL();

        public Login()
        {
            InitializeComponent();
        }

        private void btnsubmit_Click(object sender, EventArgs e)
        {
            string radio = "";
            if (radfemale.Checked == true )
            {
                radio = "F";
            }
            else if (radmale.Checked == true)
            {
                radio = "M";
            }
            string category = "";
            if (Chkgenral.Checked == true)
            {
                category = "Genral";
            }
            else if (chkother.Checked == true)
            {
                category = "Other";
            }
            string str = "UserDetails_sp 'I','','" + txtname.Text + "','" + txtmailid.Text + "','" + combocity.Text + "','" + category + "','" + radio + "','" + picimage.ImageLocation + "','"+Convert.ToDateTime(dateTimePicker1 .Value.ToShortDateString ()) +"'";
            Obj.insertfun(str);
        }

        private void btnphoto_Click(object sender, EventArgs e)
        {
            OpenFileDialog openimage = new OpenFileDialog();
            openimage.Filter = "JPG|*.jpg|GIF|*.gif|PNG|*.png|BMP|*.bmp";
            if (DialogResult.OK == openimage.ShowDialog())
            {
                txtphoto.Text = openimage.FileName;
                btnphoto.Enabled = true;
                picimage.ImageLocation = openimage.FileName;
                picimage.SizeMode = PictureBoxSizeMode.StretchImage;


            }
            else
            {
                MessageBox.Show("Select File");
            }

        }
    }
}



View Page

  private void btnshow_Click(object sender, EventArgs e)
        {
            string str = "select * from crtWaiter where name ='" +cmbwaitername .Text+ "'";
            DataTable dt = new DataTable();
            dt = Obj.getdata(str);
            if (dt.Rows.Count > 0)
            {
                pictureBox1.ImageLocation = dt.Rows[0]["wtr_image"].ToString();
                txtsalary.Text = dt.Rows[0]["salary"].ToString();
                txtmobileno.Text = dt.Rows[0]["mobile_no"].ToString();
                txtdate.Text = dt.Rows[0]["Date"].ToString();
            }
        }

        private void WaitersProfile_Load(object sender, EventArgs e)
        {
            string str1 = "select name,Id from crtWaiter";
            Obj.fillcombo(cmbwaitername, "name", "Id", str1);
           
        }

Edit,Delete Record

public partial class crtstaff : Form
    {
        DAL Obj = new DAL();
        public crtstaff()
        {
            InitializeComponent();
        }
        AutoCompleteStringCollection nameCollection = new AutoCompleteStringCollection();
        private void save1_Click(object sender, EventArgs e)
        {

            try
            {

                if (save1.Text == "Save")
                {
                    if (stfname.Text == "" ||stfpost .Text ==""|| stfadd .Text =="" ||stfmobile .Text =="" || stfsal .Text =="")
                    {
                        MessageBox.Show("Enter the Employee Record");

                    }
                    else
                    {
                        //string constring = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=bar_db;Integrated Security=True";
                        //SqlConnection con = new SqlConnection(constring);
                        string cmdstring = "insert into crtstaff (stfname,post,address,mobile_no,salary,Date) values( '" + stfname.Text + "','" + stfpost.Text + "', '" + stfadd.Text + "', '" + stfmobile.Text + "', '" + stfsal.Text + "','"+Convert .ToDateTime (dateTimePicker1 .Value .ToShortDateString())+"') ";
                        Obj.Inserfun(cmdstring);
                       // SqlCommand cmd = new SqlCommand(cmdstring, con);
                        //con.Open();
                       // cmd.ExecuteNonQuery();
                        MessageBox.Show("New Staff Member has  Been Added");
                        //con.Close();
                        this.Close();


                        stfpost.Text = "";
                        stfadd.Text = "";
                        stfmobile.Text = "";
                        stfsal.Text = "";

                        this.Hide();
                    }
                }
                else if (save1.Text == "Update")
                {

                    //string constring = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=bar_db;Integrated Security=True";
                    //SqlConnection con = new SqlConnection(constring);
                    string cmdstring = "update crtstaff  set  post = '" + stfpost.Text + "' , address = '" + stfadd.Text + "' , mobile_no = '" + stfmobile.Text + "' ,  salary = '" + stfsal.Text + "',Date='" + Convert.ToDateTime(dateTimePicker1.Value.ToShortDateString()) + "' where stfname ='" + stfname.Text + "'";

                    Obj.Updatefun(cmdstring);
                    //SqlCommand cmd = new SqlCommand(cmdstring, con);
                    //con.Open();
                    //cmd.ExecuteNonQuery();
                    MessageBox.Show("Staff Member has  Been Updated ");
                   // con.Close();
                   

                    stfpost.Text = "";
                    stfadd.Text = "";
                    stfmobile.Text = "";
                    stfsal.Text = "";
                    save1.Enabled = false;
                    edit2.Enabled = true;
                   this.Close();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void crtstaff_Load(object sender, EventArgs e)
        {
            string constring2 = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=bar_db;Integrated Security=True";
            SqlConnection con2 = new SqlConnection(constring2);
            string cmdstring2 = "select distinct stfname from crtstaff order by stfname";
            SqlCommand cmd2 = new SqlCommand(cmdstring2, con2);
            SqlDataReader dr2;
            con2.Open();
        
            dr2 = cmd2.ExecuteReader();
            if (dr2.HasRows == true)
            {
                while (dr2.Read())
                {
                    nameCollection.Add(dr2["stfname"].ToString());

                }
            }

            dr2.Close();

            stfname.AutoCompleteMode = AutoCompleteMode.Suggest;
            stfname.AutoCompleteSource = AutoCompleteSource.CustomSource;
            stfname.AutoCompleteCustomSource = nameCollection;
                      

        }

        private void stfname_TextChanged(object sender, EventArgs e)
        {
            string constring2 = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=bar_db;Integrated Security=True";
            SqlConnection con2 = new SqlConnection(constring2);
            string cmdstring2 = "select  stfname,post,address,mobile_no,salary from crtstaff where stfname = '" + stfname.Text + " ' ";
           SqlCommand cmd2 = new SqlCommand(cmdstring2, con2);
            SqlDataReader dr2;
            con2.Open();
           dr2 = cmd2.ExecuteReader();

            while (dr2.Read())
            {
                if (stfname.Text == dr2["stfname"].ToString())
                {

                    stfpost.Text = dr2["post"].ToString();
                    stfadd.Text = dr2["address"].ToString();
                    stfmobile.Text = dr2["mobile_no"].ToString();
                    stfsal.Text = dr2["salary"].ToString();


                    stfpost.Enabled = false;
                    stfadd.Enabled = false;
                    stfmobile.Enabled = false;
                    stfsal.Enabled = false;
                  

                }
                else
                {  
                    stfpost.Text = "";
                    stfadd.Text = "";
                    stfmobile.Text = "";
                    stfsal.Text = "";
                    save1.Text = "Save";

                    stfpost.Enabled = true;
                    stfadd.Enabled = true;
                    stfmobile.Enabled = true;
                    stfsal.Enabled = true;

                    save1.Enabled = true;
                    edit2.Enabled = false;
                }
            }
        }

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

        private void edit2_Click(object sender, EventArgs e)
        {
            try
            {

                stfname.Enabled = true;
                stfpost.Enabled = true;
                stfadd.Enabled = true;
                stfmobile.Enabled = true;
                stfsal.Enabled = true;

                save1.Enabled = true;
                edit2.Enabled = false;
                save1.Text = "Update";



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {

            //string constring = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=bar_db;Integrated Security=True";
           // SqlConnection con = new SqlConnection(constring);
            string cmdstring = "delete from crtstaff where stfname = '" + stfname.Text + "'  ";
            Obj.Deletefun(cmdstring);          
           // SqlCommand cmd = new SqlCommand(cmdstring, con);
            //con.Open();
           // cmd.ExecuteNonQuery();
            MessageBox.Show("Staff Member has  Been Deleted ");
          //  con.Close();
            this.Close();
        }
    }
}