C# Code For Beginner (Part 2)

1. Delete Item in Database using Textbox

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 MySql.Data.MySqlClient;

namespace blog2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            string MyConnection2 = "server=localhost;user id=root;password=;database=blog";

            string Query = "delete from example where Item_Code='" + this.tbdel.Text + "';";

            MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);

            MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);

            MySqlDataReader MyReader2;

            MyConn2.Open();

            MyReader2 = MyCommand2.ExecuteReader();

            MessageBox.Show("Successfully Deleted");

            while (MyReader2.Read())
            {

            }

            MyConn2.Close();

        }
    }
}

output 





2. Delete Item in Database using Datagridview


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 MySql.Data.MySqlClient;

namespace blog2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            string MyConnection2 = "server=localhost;user id=root;password=;database=blog";
            MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);

            int id = dataGridView1.CurrentRow.Index;
            int dd = Convert.ToInt32(dataGridView1.Rows[id].Cells["Code"].Value.ToString());
            string Delete = "Delete from example where Item_Code=" + dd + "";
            MySqlCommand cmd = new MySqlCommand(Delete, MyConn2);
            MyConn2.Open();
            int x = cmd.ExecuteNonQuery();
            MessageBox.Show("Successfully Deleted");
            MyConn2.Close();

        }
    }
}

output






3. Remove Item in Textbox


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 MySql.Data.MySqlClient;

namespace blog2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btndel_Click(object sender, EventArgs e)
        {
         
            tbdel1.Clear();
            tbdel2.Text = "";

        }
    }
}

output






4. Remove Item in Datagridview

i.


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 MySql.Data.MySqlClient;

namespace blog2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btndel_Click(object sender, EventArgs e)
        {
           
            dataGridView1.Rows.Clear();
            dataGridView2.DataSource = "" ;

        }
    }
}

output






ii.


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 MySql.Data.MySqlClient;

namespace blog2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btndel_Click(object sender, EventArgs e)
        {
           
            int rowIndex = dataGridView1.CurrentCell.RowIndex;
            dataGridView1.Rows.RemoveAt(rowIndex);
        }
    }
}

output










Comments

Popular posts from this blog

C# Analog Clock

Basic Prolog (List)

SOLID Principles