Friday, 14 October 2016

insert update delete find and fill textboxes c# 2010

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;//added for sqlserver database

namespace project_RETAIL
{
    public partial class Supplier : Form
    {
        public Supplier()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {//insert code
            string date = textBox1.Text.ToString();
            string supid = textBox2.Text.ToString();
            string supname = textBox3.Text.ToString();
            string supadd = textBox4.Text.ToString();
            string supcompany = textBox5.Text.ToString();
            string contactno = textBox6.Text.ToString();
            string connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=e:\project-retail\dbRetail.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con = new SqlConnection(connectionstring);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into tblsupplier values(@date, @supid,@supname,@supadd,@supcompany,@supcontactno)", con);
            cmd.Parameters.AddWithValue("@date", date);
            cmd.Parameters.AddWithValue("@supid", supid);
            cmd.Parameters.AddWithValue("@supname", supname);
            cmd.Parameters.AddWithValue("@supadd", supadd);
            cmd.Parameters.AddWithValue("@supcompany", supcompany);
            cmd.Parameters.AddWithValue("@contactno",contactno);
            con.Close();
            MessageBox.Show("saved successfully");
        }

        private void button4_Click(object sender, EventArgs e)
        {//find and fill textboxes
            string date=textBox1.Text.ToString();
            string supid=textBox2.Text.ToString();
            string supname=textBox3.Text.ToString();
            string supadd=textBox4.Text.ToString();
            string supcompany=textBox5.Text.ToString();
            string contactno=textBox6.Text.ToString();
            string connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=e:\project-retail\dbRetail.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con = new SqlConnection(connectionstring);
            SqlCommand cmd=new SqlCommand("select * from tblsupplier where supId='"+supid+"'",con);
                con.Open();
            SqlDataReader dr1=cmd.ExecuteReader();
            if(dr1.Read())
           
              {

              textBox1.Text=dr1.GetValue(0).ToString();
        textBox2.Text=dr1.GetValue(1).ToString();
        textBox3.Text=dr1.GetValue(2).ToString();
        textBox4.Text=dr1.GetValue(3).ToString();
        textBox5.Text=dr1.GetValue(4).ToString();
        textBox6.Text=dr1.GetValue(5).ToString();

               }
            con.Close();
         
        }

        private void button3_Click(object sender, EventArgs e)
        {//delete code
            string date=textBox1.Text.ToString();
            string supid=textBox2.Text.ToString();
            string supname=textBox3.Text.ToString();
            string supadd=textBox4.Text.ToString();
            string supcompany=textBox5.Text.ToString();
            string contactno=textBox6.Text.ToString();
            string connectionstring=@"Data Source=.\SQLEXPRESS;AttachDbFilename=e:\project-retail\dbRetail.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con=new SqlConnection(connectionstring);
            con.Open();
            SqlCommand cmd=new SqlCommand("delete from tblsupplier where supId='"+supid+"'",con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Deleted successfully");

        }

        private void button2_Click(object sender, EventArgs e)
        {//update code
            string date = textBox1.Text.ToString();
            string supid = textBox2.Text.ToString();
            string supname = textBox3.Text.ToString();
            string supadd = textBox4.Text.ToString();
            string supcompany = textBox5.Text.ToString();
            string contactno = textBox6.Text.ToString();
            string connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=e:\project-retail\dbRetail.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con = new SqlConnection(connectionstring);
            con.Open();
            SqlCommand cmd = new SqlCommand("update tblsupplier set supDate='" + date + "',supId='" + supid + "',supName='" + supname + "',supAddress='" + supadd + "',supCompany='" + supcompany + "',supContact='" + contactno + "'where supid='" + supid + "'", con);
            cmd.Parameters.AddWithValue("@supDate", date);
            cmd.Parameters.AddWithValue("@supId", supid);
            cmd.Parameters.AddWithValue("@supName", supname);
            cmd.Parameters.AddWithValue("@supAddress", supadd);
            cmd.Parameters.AddWithValue("@supCompany", supcompany);
            cmd.Parameters.AddWithValue("@Contactno", contactno);
            con.Close();
            MessageBox.Show("updates successfully");
        }

       

        }




        }