BASIC WINDOW FORM C# INSERT UPDATE DELETE

FORM LOGIN ========================================================================================
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
namespace KlinikXYZ
{
  
    public partial class FLogin : Form
    {
      

        public FLogin()
        {
            InitializeComponent();
        }

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

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=IHSAN-PC;Initial Catalog=klinikxyz;User Id=sa;Password=P@ssw0rd";
            //con.ConnectionString="data source=IHSAN-PC\SQLEXPRESS;integrated security=true"; /*sql express case */
            con.Open();

            string txtuname = textBox1.Text;
            string txtpass = textBox2.Text;

          
            string query = "select*from usertbl where username=@uname and password=@pwd";
            SqlCommand cmd = new SqlCommand(query,con);
            cmd.Parameters.Add(new SqlParameter("@uname",txtuname));
            cmd.Parameters.Add(new SqlParameter("@pwd", txtpass));

            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows == true)
            {
              
                FUtama f1 = new FUtama();
                f1.Show();
              
              
            }
            else
            { MessageBox.Show("user name or password invalid!");}

        }
    }
}



FORM1 INSERT UPDATE DELETE=================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace KlinikXYZ

  
    public partial class Form1 : Form
    {


        //public static SqlConnection cn = new SqlConnection(@"data source=.\MSSQLServer2008;initial catalog=klinikxyz;user id=sa;password=Password123;integrated security=true");
        public static SqlConnection cn = new SqlConnection(@"Server=IHSAN-PC;Database=klinikxyz;User ID=sa;Password=P@ssw0rd;Trusted_Connection=False");

     

        public Form1()
        {
         
            InitializeComponent();
          
        }

     
        private void Form1_Load(object sender, EventArgs e)
        {
            if(cn.State != ConnectionState.Open)
            {
                cn.Open();
              
            }
            viewData();
        }

      
        void viewData()
        {
            SqlCommand cmd = new SqlCommand("select * from dokter", cn);
            DataTable ds = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            da.Fill(ds);
            dataGridView1.DataSource = ds;
            dataGridView1.Refresh();

        
        }

        private void button2_Click(object sender, EventArgs e)
        {
         
            SqlCommand cmd = new SqlCommand("insert into dokter values('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox2.Text + "')", cn);
            cmd.ExecuteNonQuery();
            viewData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("update dokter set namadokter='" + textBox3.Text + "',bidangspesialisasi='" + textBox2.Text + "' where iddokter='" + textBox1.Text + "'", cn);
            cmd.ExecuteNonQuery();
            viewData();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("delete from dokter where iddokter='" + textBox1.Text + "'", cn);
            cmd.ExecuteNonQuery();
            viewData();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked) {
                search("iddokter", textBox4.Text);
            }
            else if (radioButton2.Checked)
            {
                search("namadokter", textBox4.Text);
            }
            else {
                search("bidangspesialisasi", textBox4.Text);
            }
        }
        void search(string label,string val)
        {
            SqlCommand cmd = new SqlCommand("select * from dokter where "+ label +" like '%" + val + "%'",cn);
            DataTable ds = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            da.Fill(ds);
            dataGridView1.DataSource = ds;
            dataGridView1.Refresh();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
          
            DataGridViewRow row = new DataGridViewRow();
            row = dataGridView1.Rows[e.RowIndex];
            textBox1.Text = row.Cells[0].Value.ToString();
            textBox3.Text = row.Cells[1].Value.ToString();
            textBox2.Text = row.Cells[2].Value.ToString();
            textBox1.ReadOnly = true;
            button2.Enabled = false;
            button3.Enabled = true;
            button4.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Focus();
            textBox1.Text = "";
            textBox2.Text = string.Empty;
            textBox3.Text = null;
            textBox1.ReadOnly = false;
            button2.Enabled = true;
            button3.Enabled = false;
            button4.Enabled = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            viewData();

                    
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {
            //Form2 fm2 = new Form2();
            //fm2.param1 = textBox3.Text;
            //fm2.param2 = textBox2.Text;
            //fm2.setTextbox1(textBox1.Text);

            //fm2.Show();

            Form2 fm2 = (Form2)this.Owner;
            fm2.param1 = textBox3.Text;
            fm2.param2 = textBox2.Text;
            fm2.setTextbox1(textBox1.Text);

            fm2.Show();


            this.Close();
        }

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

        }
         
    }
}







CLASS DATA ========================================================================

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

namespace KlinikXYZ
{

    public class Data
    {
        public Data()
        {

        }
        public static SqlConnection cn = new SqlConnection(@"data source=IHSAN-PC;initial catalog=klinikxyz;User Id=sa;Password=P@ssw0rd;integrated security=true");
                                                                                
        public static void koneksi()
        {
            {
                if (cn.State != ConnectionState.Open)
                {
                    cn.Open();
                }
            }
        }

    }
}


FORM TRANSAKSI=============================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Linq;

namespace KlinikXYZ
{
    public partial class Form2 : Form
    {

     

        //public static SqlConnection cn = new SqlConnection(@"data source=.\MSSQLServer2008;initial catalog=klinikxyz;user id=sa;password=Password123;integrated security=true");
        //public static SqlConnection cn = new SqlConnection(@"Server=172.30.100.153\MSSQLServer2008;Database=klinikxyz;User ID=sa;Password=Password123;Trusted_Connection=False");
        public static SqlConnection cn = new SqlConnection(@"Server=IHSAN-PC;Database=klinikxyz;User ID=sa;Password=P@ssw0rd;Trusted_Connection=False");

        public string pas1;
        public string pas2;

        #region Source

        public KlinikXYZEntities3 Entity { get; set; }

        #endregion


        public void settexbox7(string pas)
        {
            textBox7.Text = pas;
            textBox6.Text = pas1;
            textBox5.Text = pas2;
          
        }


        public string param1;
        public string param2;

        public void setTextbox1(String param)
        {
            textBox1.Text = param;
            textBox2.Text = param1;
            textBox3.Text=param2;
        }

      
        public Form2()
        {
            InitializeComponent();

            //this.Entity = new KlinikXYZEntities3();

            //var a = Entity.Dokters.Where(p => p.IdDokter == "1");
            //if(a.Count() != 0)
            //    MessageBox.Show(a.FirstOrDefault().NamaDokter);
        }

      

        private void button2_Click(object sender, EventArgs e)
        {
            //lookupPasien lp =new lookupPasien();
            //lp.Show();

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.Owner = this;
            f1.ShowDialog();
            button1.Enabled = false;

        }

     

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

        }

        void viewData()
        {
            SqlCommand cmd = new SqlCommand("select * from transaksi", cn);
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable ds = new DataTable();
            da.SelectCommand = cmd;
            da.Fill(ds);
            dataGridView1.DataSource = ds;
            dataGridView1.Refresh();



        }


        private void button6_Click(object sender, EventArgs e)
        {


            if (!isvalidate())
            {
                MessageBox.Show("Data tidak boleh kosong");
            }
            else
            {
                SqlCommand cmd = new SqlCommand("insert into transaksi(IdTransaksi,IdDokter,NoRegistrasiPasien,IdObat) values('" + textBox4.Text + "','" + textBox1.Text + "','" + textBox7.Text + "','" + textBox11.Text + "')", cn);
                //SqlCommand cmd = new SqlCommand("insert into transaksix(IdTransaksix) values('" + textBox4.Text + "')", cn);
                cmd.ExecuteNonQuery();
                viewData();
                MessageBox.Show("DATA TRANSAKSI TERSIMPAN.", "Information!");
            }
          
        }

        private void Form2_Load(object sender, EventArgs e)
        {
         
            if (cn.State != ConnectionState.Open)
            {
                cn.Open();

            }
            viewData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }
      
      
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            DataGridViewRow row = new DataGridViewRow();
            row = dataGridView1.Rows[e.RowIndex];
            //textBox1.Text = row.Cells[0].Value.ToString();
            //textBox3.Text = row.Cells[1].Value.ToString();
            //textBox2.Text = row.Cells[2].Value.ToString();

        }

        private bool isvalidate()
        {
      
        if (string.IsNullOrEmpty(textBox1.Text))
            return false;

        if (string.IsNullOrEmpty(textBox4.Text))
            return false;

        if (string.IsNullOrEmpty(textBox7.Text))
            return false;

       if (string.IsNullOrEmpty(textBox11.Text))
            return false;

            return true;

        }

        private void button3_Click_1(object sender, EventArgs e)
        {

        }
         
        }
    }

0 comments:

Post a Comment