executenonquery in c#

                          Using executenonquery insert,edit,delete and update examples in c# 

executenonquery in csharp



                                                 This tutorial demonstrate to explained using executenonquery insert, update, delete and update with examples and how to use ado.net  in sqlcommand method in c# .
                      ExecuteNonQuery is basically used for operations where there is nothing returned from the sql query or stored procedure .preffered use will be for insert,update and delete operation.
                     ExecuteNonQuery is one of the most popular to used method in sqlcommand object and is used to executing statements.And this tasks performed well in data manipulation tasks ..

Before this tutorial previous learn How to create login page in C#:>>How to create login form in C# 

create a database named as finacial.
create a table and named as login

create table login (id int primary key identity(1,1) not null, username varchar(50) not null, password varchar(50) not null);

create table   and named as recharge 

CREATE TABLE recharge(
id int primary key IDENTITY(1,1) NOT NULL,
mobilenetwork varchar(50) NULL,
mobilenumber varchar(50) NULL,
amount varchar(40) NULL,
date varchar(60) NULL,
status varchar(50) NULL);

And then create two empty file named as 

1.login.cs

2.rrmain.cs

executenonquery in csharp




1.login.cs

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.SqlClient;

namespace RRstudio_finacial_application
{
    public partial class Login : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=finacial;Integrated Security=True");
        public Login()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select Count(*) from login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'",con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1")
            {
                RR_Main rm = new RR_Main();
                rm.Show();
                this.Hide();
            }
            else { MessageBox.Show("please check user username and password"); }
            con.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
        }
    }
}


executenonquery in csharp




 2.rrmain.cs


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

namespace RRstudio_finacial_application
{
    public partial class RR_Main : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=finacial;Integrated Security=True");
        public RR_Main()
        {
            InitializeComponent();
            display();
            display2();
        }
  private void btnNew(object sender, EventArgs e)
        {
            textBox2.Text = "";
            textBox2.Clear();
            textBox3.Text = "";
            textBox3.Clear();
            comboBox1.SelectedIndex = -1;
            comboBox8.SelectedIndex = -1;
            textBox16.Clear();
       
        }

        private void btnsave(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cm = new SqlCommand("insert into recharge ( mobilenetwork, mobilenumber, amount , date , status) values ('" + comboBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + dateTimePicker2.Text + "','"+comboBox8.Text+"')", con);
            cm.ExecuteNonQuery();
            MessageBox.Show("Insert Successfully");

            textBox2.Clear();
            textBox16.Clear();
            textBox3.Clear();
            comboBox1.SelectedIndex = -1;
            comboBox8.SelectedIndex = -1;

            con.Close();
            display();
       
        }
        void display() { 
        con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from recharge",con);
           System.Data.DataTable dt = new System.Data.DataTable();
            sda.Fill(dt);
            dataGridView1.Rows.Clear();
            foreach (DataRow items in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = (n + 1).ToString();
                dataGridView1.Rows[n].Cells[1].Value = items[0].ToString();
                dataGridView1.Rows[n].Cells[2].Value = items[1].ToString();
                dataGridView1.Rows[n].Cells[3].Value = items[2].ToString();
                dataGridView1.Rows[n].Cells[4].Value = items[3].ToString();
                dataGridView1.Rows[n].Cells[5].Value = items[4].ToString();
                dataGridView1.Rows[n].Cells[6].Value = items[5].ToString();
            }
            con.Close();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.TextLength <= 10)
            {

            }
            else { MessageBox.Show("mobile number will be upto 10 number"); }

        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            textBox16.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            comboBox1.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            textBox2.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            textBox3.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            comboBox8.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();

        }

        private void btnupdate(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cm = new SqlCommand("Update recharge set mobilenetwork='" + comboBox1.Text + "',mobilenumber='" + textBox2.Text + "',amount='" + textBox3.Text + "',status='"+comboBox8.Text+"' where id='" +textBox16.Text + "'", con);
            cm.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Update successfully");

            display();

        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from recharge where date like '%" + textBox15.Text + "%' or mobilenumber like '" + textBox15.Text + "%'", con);
            System.Data.DataTable dt = new System.Data.DataTable();
            sda.Fill(dt);
            dataGridView1.Rows.Clear();
            foreach (DataRow items in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = (n + 1).ToString();
                dataGridView1.Rows[n].Cells[1].Value = items[0].ToString();
                dataGridView1.Rows[n].Cells[2].Value = items[1].ToString();
                dataGridView1.Rows[n].Cells[3].Value = items[2].ToString();
                dataGridView1.Rows[n].Cells[4].Value = items[3].ToString();
                dataGridView1.Rows[n].Cells[5].Value = items[4].ToString();
                dataGridView1.Rows[n].Cells[6].Value = items[5].ToString();


            }
            con.Close();
           
        }

        private void btndelete(object sender, EventArgs e)
        {
             con.Open();
           SqlCommand cm = new SqlCommand("delete from recharge where id='"+textBox16.Text+"'", con);
           cm.ExecuteNonQuery();
           con.Close();
           MessageBox.Show("Delete successfully");
           textBox16.Clear();
           textBox2.Clear();
           textBox3.Clear();
           textBox15.Clear();
           comboBox1.SelectedIndex = -1;
           comboBox8.SelectedIndex = -1;
           display();
       
        }

private void RR_Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Change_Password cp = new Change_Password();
            cp.Show();
        }

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

        

    }
}

                                       
 In this tutorial,i create code to login type.Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior.

The DataGridView control makes it easy to define the basic appearance of cells and the display formatting of cell values. The cell is the fundamental unit of interaction for the DataGridView. 

In my datagridview is clickable and update the data using datagridview_mousevent () and what it has in my screenshot i create the code like the way and when your data is enter automatically the data is display in Gridview table just copy and paste my code and use it.

In this step we will learn C# executenonquery insert,edit,delete and update
 it will helps you with your need. The next time I will put some more interesting commands, and continue project thank you this is helpful for you will Like That's all, You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
Previous Post Next Post