Untitled

mail@pastecode.io avatar
unknown
csharp
a month ago
4.0 kB
2
Indexable
Never
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Xml.Linq;

namespace Sabucido_week4
{
    public partial class Form1 : Form
    {
        Connection conn = new Connection();
        SqlConnection kon;
        SqlCommand cmd;
        SqlDataReader rd;

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            kon = conn.getCon();
            kon.Open();
            
                SqlCommand cmd = new SqlCommand("INSERT INTO Contacts (ContactID, Name, PhoneNumber, Email) VALUES (@ContactID, @Name, @PhoneNumber, @Email)", kon);
                cmd.Parameters.AddWithValue("@ContactID", idBox.Text);
                cmd.Parameters.AddWithValue("@Name", nameBox.Text);
                cmd.Parameters.AddWithValue("@PhoneNumber", phoneBox.Text);
                cmd.Parameters.AddWithValue("@Email", mailBox.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Contact added successfully!");
                ClearFields();
                LoadContacts();
            kon.Close();
        }

        private void delButton_Click(object sender, EventArgs e)
        {
            kon = conn.getCon();
            kon.Open();

            SqlCommand cmd = new SqlCommand("DELETE FROM Contacts WHERE ContactID = @ContactID", kon);
                cmd.Parameters.AddWithValue("@ContactID", idBox.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Contact deleted successfully!");
                ClearFields();
            LoadContacts();
            kon.Close();
            
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            kon = conn.getCon();
            kon.Open();
            
                SqlCommand cmd = new SqlCommand("UPDATE Contacts SET Name = @Name, PhoneNumber = @PhoneNumber, Email = @Email WHERE ContactID = @ContactID", kon);
                cmd.Parameters.AddWithValue("@ContactID", idBox.Text);
                cmd.Parameters.AddWithValue("@Name", nameBox.Text);
                cmd.Parameters.AddWithValue("@PhoneNumber", phoneBox.Text);
                cmd.Parameters.AddWithValue("@Email", mailBox.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Contact updated successfully!");
                ClearFields();
                LoadContacts();

            kon.Close();
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            kon = conn.getCon();
            kon.Open();

            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Contacts WHERE ContactID = @ContactID", kon);
                da.SelectCommand.Parameters.AddWithValue("@ContactID", idBox.Text);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;

            kon.Close();
        }

        private void viewButton_Click(object sender, EventArgs e)
        {
            LoadContacts();
        }

        private void LoadContacts()
        {
            kon = conn.getCon();
            kon.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Contacts", kon);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;

            kon.Close();
        }

        private void ClearFields()
        {
            idBox.Text = "";
            nameBox.Text = "";
            phoneBox.Text = "";
            mailBox.Text = "";
        }
    }
}
Leave a Comment