Saturday 27 May 2017

Insert Update Delete Using Gridview

Insert Update Delete Using Grid-view

Step:-1 ) Using SQL Server Create Table


Create Table UserTest(
id int primary key identity(1,1),
Name nvarchar(200),
EmailId nvarchar(200),
DOB varchar(50),
City nvarchar(200),
Company nvarchar(200),
Salary int,
);

Step :- 2) Using Visual Studio Web Application


TestWebForm.aspx
-----------------------------------

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function validateForm() {
            var txtName = document.getElementById('txtName');
            var txtEmailId = document.getElementById('txtEmailId');
            var txtDOB = document.getElementById('txtDOB');
            var txtCity = document.getElementById('txtCity');
            var txtCompany = document.getElementById('txtCompany');
            var txtSalary = document.getElementById('txtSalary');

            if (txtName.value == '') { alert('Enter Name'); txtName.focus(); return false; }
            if (txtEmailId.value == '') { alert('Enter Email-Id'); txtEmailId.focus(); return false; }
            if (txtDOB.value == '') { alert('Enter Date of Birth'); txtDOB.focus(); return false; }
            if (txtCity.value == '') { alert('Enter City Name'); txtCity.focus(); return false; }
            if (txtCompany.value == '') { alert('Enter Company Name'); txtCompany.focus(); return false; }
            if (txtSalary.value == '') { alert('Enter Sallary'); txtSalary.focus(); return false; }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>ID</td>
                    <td>
                        <input type="text" id="txtID"  readonly="true" class="inputTxt" runat="server" /></td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td>
                        <input type="text" id="txtName" class="inputTxt" runat="server" /></td>
                </tr>
                <tr>
                    <td>EmailID</td>
                    <td>
                        <input type="text" id="txtEmailId" class="inputTxt" runat="server" /></td>
                </tr>
                <tr>
                    <td>Date of Birth</td>
                    <td>
                        <input type="text" id="txtDOB"  class="inputTxt" runat="server" /></td>
                </tr>
                <tr>
                    <td>City</td>
                    <td>
                        <input type="text" id="txtCity" class="inputTxt" runat="server" /></td>
                </tr>
                <tr>
                    <td>Company</td>
                    <td>
                        <input type="text" id="txtCompany" class="inputTxt" runat="server" /></td>
                </tr>
                <tr>
                    <td>Salary</td>
                    <td>
                        <input type="text" id="txtSalary" class="inputTxt" runat="server" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Button runat="server" ID="btnSave" CssClass="btn" Text="Save" OnClientClick="return validateForm()" OnClick="btnSave_Click" />
                        <asp:Button runat="server" ID="btnUpdate" CssClass="btn" Text="Update" Visible="false" OnClientClick="return validateForm()" OnClick="btnUpdate_Click" />
                        <asp:Button runat="server" ID="btnCancel" CssClass="btn" Text="Cancel" OnClick="btnCancel_Click" /></td>
                </tr>
            </table>

            <br />
            <br />
            <asp:GridView ID="grvTest" runat="server" DataKeyNames="id" OnRowDeleting="grvTest_RowDeleting" OnSelectedIndexChanged="grvTest_SelectedIndexChanged">
                <Columns>
                    <asp:CommandField HeaderText="Update" ShowSelectButton="true" />
                    <asp:CommandField HeaderText="Delete" ShowDeleteButton="true" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>





TestWebForm.aspx.cs
--------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWebApp
{
    public partial class TestWebForm : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TestApp;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                BindGridView();
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                string str = "INSERT INTO UserTest(Name,EmailId,DOB,City,Company,Salary)";
                str += " VALUES('" + txtName.Value + "','" + txtEmailId.Value + "','" + Convert.ToDateTime(txtDOB.Value) + "','" + txtCity.Value + "','" + txtCompany.Value + "','" + txtSalary.Value + "');";
                SqlCommand cmd = new SqlCommand(str, con);
                int i = cmd.ExecuteNonQuery();
                con.Close();
                resetForm();
                BindGridView();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
               
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            resetForm();
            Response.Redirect("TestWebForm.aspx");
        }

        public void resetForm() {
            txtCity.Value = "";
            txtCompany.Value = "";
            txtDOB.Value = "";
            txtEmailId.Value = "";
            txtID.Value = "";
            txtName.Value = "";
            txtSalary.Value = "";
        }

        public void BindGridView()
        {
            try
            {
                DataSet ds = new DataSet();
                con.Open();
                string str = "SELECT * FROM UserTest;";
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter sda = new SqlDataAdapter(str, con);
                sda.Fill(ds);
                con.Close();
                grvTest.DataSource = ds;
                grvTest.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
               
            }
        }

        protected void grvTest_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                con.Open();
                int id = Convert.ToInt32(grvTest.DataKeys[e.RowIndex].Value);
                string str = "DELETE FROM UserTest WHERE id='" + id + "'; ";
                SqlCommand cmd = new SqlCommand(str, con);
                int i = cmd.ExecuteNonQuery();
                con.Close();
                BindGridView();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                
            }
        }

        protected void grvTest_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow newRow = grvTest.SelectedRow;
            txtID.Value = newRow.Cells[2].Text;
            txtName.Value = newRow.Cells[3].Text;
            txtEmailId.Value = newRow.Cells[4].Text;
            txtDOB.Value = newRow.Cells[5].Text;
            txtCity.Value = newRow.Cells[6].Text;
            txtCompany.Value = newRow.Cells[7].Text;
            txtSalary.Value = newRow.Cells[8].Text;
            btnSave.Visible = false;
            btnUpdate.Visible = true;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                string str = "UPDATE UserTest SET Name='" + txtName.Value + "',EmailId='" + txtEmailId.Value + "',DOB='" + txtDOB.Value + "',City='" + txtCity.Value + "',Company='" + txtCompany.Value + "',Salary='" + txtSalary.Value + "' WHERE id='" + txtID.Value + "'; ";
                SqlCommand cmd = new SqlCommand(str, con);
                int i = cmd.ExecuteNonQuery();
                con.Close();
                BindGridView();
                resetForm();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                
            }
        }

    }
}




Sunday 7 May 2017

Curd Operation - Stored Procedure

Curd Operation - Stored Procedure




Use IDE :- Microsoft SQL Server - 2008 R2


=================================================================
Create Database
=================================================================

CREATE DATABASE CurdDB;

USE CurdDB;


=================================================================
Create Table
=================================================================



CREATE TABLE [dbo].[Employee](
[EmpId] [bigint] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NOT NULL,
[EmpNo] [varchar](50) NOT NULL,
[Department] [varchar](50) NOT NULL,
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
(
[EmpId] ASC
)
) ON [PRIMARY]



=================================================================
Select Table
=================================================================

Select * from Employee;





=================================================================
Create Procedure for Get Employees Details
=================================================================


USE [CurdDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetEmployeeDetails]
AS
BEGIN
SELECT 
EmpId ,
FirstName ,
LastName ,
EmpNo ,
Department
FROM Employee
END









=================================================================
Create Procedure for INSERT AND UPDATE Record
=================================================================

USE [CurdDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AddEmployee]
@EmpId BIGINT ,
@FirstName VARCHAR(50) ,
@LastName VARCHAR(150),
@EmpNo VARCHAR(50) ,
@Department VARCHAR(50)
AS

IF(@EmpId= 0)
BEGIN
INSERT INTO Employee
(
FirstName ,
LastName ,
EmpNo ,
Department
)
values
(
@FirstName ,
@LastName ,
@EmpNo ,
@Department
)
END
ELSE
BEGIN
UPDATE Employee
SET FirstName = @FirstName ,
LastName = @LastName ,
EmpNo = @EmpNo ,
Department = @Department
WHERE EmpId = @EmpId
END





=================================================================
Create Procedure for  Delete Record
=================================================================


USE [CurdDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteEmployee]
@EmpId BIGINT
AS
BEGIN
DELETE FROM Employee
WHERE EmpId = @EmpId
END


================================================================
Note :- Use these stored procedure for curd operation in application.
================================================================

Factorial of a Number

Recently Viewed