Wednesday 11 November 2015

Insert update delete in DataGridView in C#

Insert update delete in DataGridView in C#




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;

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

        DataTable dt = new DataTable();
        int i, j;
        int rowcount, sum;

        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("S.No", typeof(int));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Card No", typeof(int));
            dt.Columns.Add("Client Name", typeof(string));
            dt.Columns.Add("Product Name", typeof(string));
            dt.Columns.Add("Empty Bottle", typeof(int));
            dt.Columns.Add("Balance Account", typeof(int));
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            dt.Rows.Add(txtSno.Text, dateTimePicker1.Value, txtCno.Text, txtCname.Text, txtPname.Text, txtEbottle.Text, txtBaccount.Text);
            dataGridView1.DataSource = dt;

            txtSno.Text = null;
            txtCno.Text = null;
            txtCname.Text = null;
            txtEbottle.Text = null;
            txtPname.Text = null;
            txtBaccount.Text = null;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                dt.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

        private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[i];
            txtSno.Text = row.Cells[0].Value.ToString();
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            txtCno.Text = row.Cells[2].Value.ToString();
            txtCname.Text = row.Cells[3].Value.ToString();
            txtPname.Text = row.Cells[4].Value.ToString();
            txtEbottle.Text = row.Cells[5].Value.ToString();
            txtBaccount.Text = row.Cells[6].Value.ToString();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[i];
            row.Cells[0].Value = txtSno.Text;
            row.Cells[1].Value = dateTimePicker1.Text;
            row.Cells[2].Value = txtCno.Text;
            row.Cells[3].Value = txtCname.Text;
            row.Cells[4].Value = txtPname.Text;
            row.Cells[5].Value = txtEbottle.Text;
            row.Cells[6].Value = txtBaccount.Text;
        }
    }
}

Monday 9 November 2015

Insert, Update And Delete in ASP.NET



Data Base Design




create database Form

use Form

create table Employee
(
[ID] int identity primary key,
[Name] varchar (25),
[Email] varchar (25),
[Password] varchar (25),
);

select * from Employee


Insert, Update And Delete in ASP.NET






<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication6.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="txtUpdate" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
        <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
        <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
   
    </div>
    </form>
</body>
</html>



Insert, Update And Delete Coding in ASP.NET


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;

namespace WebApplication6
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        string path = ConfigurationManager.ConnectionStrings["FormConnectionString"].ConnectionString;
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(path);
            SqlCommand cmd = new SqlCommand("insert into Employee values('" + txtName.Text + "','" + txtEmail.Text + "', '" + txtPass.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();

            txtName.Text = "";
            txtEmail.Text = "";
            txtPass.Text = "";
        }

        string Update = ConfigurationManager.ConnectionStrings["FormConnectionString"].ConnectionString;
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Update);
            SqlCommand cmd = new SqlCommand("update Employee set Name='"+txtName.Text+"', Email='"+txtEmail.Text+"', Password='"+txtPass.Text+"' where id = '"+txtUpdate.Text+"' ", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            txtUpdate.Text = "";
            txtName.Text = "";
            txtEmail.Text = "";
            txtPass.Text = "";

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Update);
            SqlCommand cmd = new SqlCommand("delete from Employee where id = '" + txtUpdate.Text + "' ", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            txtUpdate.Text = "";
            txtName.Text = "";
            txtEmail.Text = "";
            txtPass.Text = "";
        }

       
    }
}


Wednesday 4 November 2015

Create Insert AND Update Form


DataBase Create




Create Insert AND Update Form





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;

namespace databaseconnection
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {

         string path = "Data Source=.;Initial Catalog=connection;Persist Security Info=True;User ID=sa;Password=123";
         SqlConnection con = new SqlConnection(path);

         SqlCommand cmd = new SqlCommand("insert into Register values ('" + txtName.Text + "', '" + txtEmail.Text + "')", con);
         con.Open();
         cmd.ExecuteNonQuery();

         txtName.Text = "";
         txtEmail.Text = "";
            

            
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string path = "Data Source=.;Initial Catalog=connection;Persist Security Info=True;User ID=sa;Password=123";
            SqlConnection con = new SqlConnection(path);

            SqlCommand cmd = new SqlCommand("UPDATE Register SET Name='" + txtName.Text + "',Email ='" + txtEmail.Text + "' where ID='"+txtUpdate.Text+"'", con);
            con.Open();
            cmd.ExecuteNonQuery();

            txtName.Text = "";
            txtEmail.Text = "";
            
        }
    }

}