Asp.net Insert Update Delete CRUD operations using C#

Asp.net Insert Update Delete CRUD operations using C#

In this Post We Will Explain About is Asp.net Insert Update Delete CRUD operations using C# With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Insert, Update and Delete a Record in ASP.NET Example

In this post we will show you Best way to implement Insert Select Update and Delete in ASP.NET, hear for Insert Update Edit Delete record in SqlDataSource with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Tecnology use:

1) First of all .Netframework 4
2)and then ASP.NET with C#.NET platform
3) simple Microsoft SQL Server version of the 2008 R2

In this web application you will to step by step learn or create

1) Insert a record in database
2) Update a record in database
3) Delete a record in database
4) Lastly List of the DataBind with GridView

In order to Implements operations Insert, Update, Edit and Delete using asp.net step by step operations, the table GridView will be displayed populated form database access in SQL Server database all the table using lightweight SqlDataSource control.

CREATE TABLE IF NOT EXISTS `user` (  
 `UID` int(100) NOT NULL AUTO_INCREMENT,  
 `Name` varchar(100) NOT NULL,  
 `Address` varchar(500) NOT NULL,  
 `Email` varchar(100) NOT NULL,  
 `userPhone` varchar(25) NOT NULL,  
 PRIMARY KEY (`UID`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;  

First of all the Open your asp.net instance of version Like as a asp.net Visual Studio 2012, and make a new step by step ASP.NET project Web application. here Name the project of the asp.net like as a “MYSQLCRUDApplication “, as display in the following examples:

Also Read :   Google Charts or graph using Google Chart API with PHP MySQLi

In the source code behind file (User.aspx.cs) write the source code as in the simple following.

User.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"  
CodeBehind="User.aspx.cs" Inherits="USERCRUDApplication.User" %>  
  
<asp:Content ID="Content1" ContentPlaceHolderID="titleContent" runat="server">  
    Simple Insert Select Update and Delete in ASP.NET using MySQL Database   
</asp:Content>  
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">  
</asp:Content>  
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">  
    <table>  
        <tr>  
            <td class="td">Name:</td>  
            <td>  
                <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>  
            <td>  
                <asp:Label ID="lblUID" runat="server" Visible="false"></asp:Label> </td>  
        </tr>  
        <tr>  
            <td class="td">Address:</td>  
            <td>  
                <asp:TextBox ID="txtUserAddress" runat="server"></asp:TextBox></td>  
            <td> </td>  
        </tr>  
        <tr>  
            <td class="td">userPhone:</td>  
            <td>  
                <asp:TextBox ID="txtUserPhone" runat="server"></asp:TextBox></td>  
            <td> </td>  
        </tr>  
        <tr>  
            <td class="td">User Email ID:</td>  
            <td>  
                <asp:TextBox ID="txtUserEmail" runat="server"></asp:TextBox></td>  
            <td> </td>  
        </tr>  
        <tr>  
            <td></td>  
            <td>  
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />  
                <asp:Button ID="btnUpdate" runat="server" Text="Update" Visible="false"  
OnClick="btnUpdate_Click" />  
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" /></td>  
            <td></td>  
        </tr>  
    </table>  
  
    <div style="padding: 10px; margin: 0px; width: 100%;">  
        <p>  
            Total User:<asp:Label ID="lbltotalcount" runat="server" Font-Bold="true"></asp:Label>  
        </p>  
        <asp:GridView ID="GridViewUser" runat="server" DataKeyNames="UID"   
            OnSelectedIndexChanged="GridViewUser_SelectedIndexChanged"  
OnRowDeleting="GridViewUser_RowRemove">  
            <Columns>  
                <asp:CommandField HeaderText="Update" ShowSelectButton="True" />  
                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />  
            </Columns>  
        </asp:GridView>  
    </div>  
</asp:Content>  

In the simple Web.config file make thedb connection string as in the source code following.

Also Read :   PHP Simple Image Upload and Display Source Code

Web.config

<connectionStrings>  
    <add name="ConnectionString"  
connectionString="Server=localhost;userid=root;password=;Database=LiveDB"  
providerName="MySql.Data.MySqlClient"/>  
 </connectionStrings> 

User.aspx.cs

Now, in the source code behind file use the following source code.

//include namespace
using System;  
using System.Collections.Generic;  
using System.Configuration;  
using System.Data;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using MySql.Data.MySqlClient;  
  
  
namespace USERCRUDApplication  
{  
    public partial class User : System.Web.UI.Page  
    {  
        #region MySqlConnection Connection and Page Lode  
        MySqlConnection db_conn = new  
MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);          
        protected void Page_Load(object sender, EventArgs e)  
        {  
            Try  
            {  
                if (!Page.IsPostBack)  
                {  
                    BindGridView();  
                      
                }  
            }  
            catch (Exception ex)  
            {  
                DisplayMessage(ex.Message);  
            }  
        }  
        #endregion  
        #region show message  
        void DisplayMessage(string msg)  
        {  
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script  
language='javascript'>alert('" + msg + "');</script>");  
        }   
        void clear()  
        {  
            txtUserName.Text = string.Empty; txtUserAddress.Text = string.Empty; txtUserPhone.Text = string.Empty;  
txtUserEmail.Text = string.Empty;  
            txtUserName.Focus();  
        }  
        #endregion  
        #region bind data to GridViewUser  
        private void BindGridView()  
        {   
            Try  
            {  
                if (db_conn.State == ConnectionState.Closed)  
                {  
                    db_conn.Open();  
                }  
                MySqlCommand livecmd = new MySqlCommand("Select * from User ORDER BY UID DESC;",  
db_conn);  
                MySqlDataAdapter adp = new MySqlDataAdapter(livecmd);  
                DataSet ds = new DataSet();  
                adp.Fill(ds);  
                GridViewUser.DataSource = ds;  
                GridViewUser.DataBind();  
                lbltotalcount.Text = GridViewUser.Rows.Count.ToString();  
            }  
            catch (MySqlException ex)  
            {  
                DisplayMessage(ex.Message);  
            }  
            Finally  
            {  
                if (db_conn.State == ConnectionState.Open)  
                {  
                   db_conn.Close();  
                }  
            }  
        }  
        #endregion  
        #region Insert Data   
        protected void btnSubmit_Click(object sender, EventArgs e)  
        {  
            Try  
            {  
                db_conn.Open();  
                MySqlCommand livecmd = new MySqlCommand("Insert into user (Name,Address,userPhone,Email )  
values (@Name,@Address,@userPhone,@Email)", db_conn);  
                livecmd.Parameters.AddWithValue("@Name",txtUserName.Text);  
                livecmd.Parameters.AddWithValue("@Address", txtUserAddress.Text);  
                livecmd.Parameters.AddWithValue("@userPhone",txtUserPhone.Text);  
                livecmd.Parameters.AddWithValue("@Email",txtUserEmail.Text);  
                livecmd.ExecuteNonQuery();                 
                livecmd.Dispose();   
                DisplayMessage("Registered successfully......!");               
                clear();  
                BindGridView();  
            }  
            catch (MySqlException ex)  
            {  
                DisplayMessage(ex.Message);  
            }  
            Finally  
            {  
                db_conn.Close();  
            }  
        }  
          
        #endregion   
        #region SelectedIndexChanged  
  
        protected void GridViewUser_SelectedIndexChanged(object sender, EventArgs e)  
        {  
            GridViewRow row = GridViewUser.SelectedRow;  
            lblUID.Text = row.Cells[2].Text;  
            txtUserName.Text = row.Cells[3].Text;  
            txtUserAddress.Text = row.Cells[4].Text;  
            txtUserEmail.Text = row.Cells[5].Text;  
            txtUserPhone.Text = row.Cells[6].Text;  
            btnSubmit.Visible = false;  
            btnUpdate.Visible = true;  
        }  
        #endregion  
        #region Delete User Data  

        protected void GridViewUser_RowRemove(object sender, GridViewDeleteEventArgs e)  
        {  
            Try  
            {  
                db_conn.Open();  
                int UID = Convert.ToInt32(GridViewUser.DataKeys[e.RowIndex].Value);  
                MySqlCommand livecmd = new MySqlCommand("Delete From user where UID='" + UID + "'",  
db_conn);  
                livecmd.ExecuteNonQuery();  
                livecmd.Dispose();  
                DisplayMessage("User Data Delete Successfully......!");  
                GridViewUser.EditIndex = -1;  
                BindGridView();  
            }  
            catch (MySqlException ex)  
            {  
                DisplayMessage(ex.Message);  
            }  
            Finally  
            {  
                db_conn.Close();  
            }  
        }  
        #endregion  
        #region user data update  

        protected void btnUpdate_Click(object sender, EventArgs e)  
        {  
            Try  
            {  
                db_conn.Open();  
                string UID = lblUID.Text;                
                MySqlCommand livecmd = new MySqlCommand("update user Set  
Name=@Name,Address=@Address,userPhone=@userPhone,Email=@Email where UID=@UID", db_conn);  
                livecmd.Parameters.AddWithValue("@Name", txtUserName.Text);  
                livecmd.Parameters.AddWithValue("@Address", txtUserAddress.Text);  
                livecmd.Parameters.AddWithValue("@userPhone", txtUserPhone.Text);  
                livecmd.Parameters.AddWithValue("@Email", txtUserEmail.Text);  
                livecmd.Parameters.AddWithValue("UID",UID);  
                livecmd.ExecuteNonQuery();  
                livecmd.Dispose();  
                DisplayMessage("User Data update Successfully......!");  
                GridViewUser.EditIndex = -1;  
                BindGridView(); btnUpdate.Visible = false;  
            }  
            catch (MySqlException ex)  
            {  
                DisplayMessage(ex.Message);  
            }  
            Finally  
            {  
                db_conn.Close();  
            }  
        }  
        #endregion  
        #region textbox clear  
        protected void btnCancel_Click(object sender, EventArgs e)  
        {  
            clear();  
        }  
        #endregion  
    }  
}   

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Also Read :   Login with Facebook in Socialite using Laravel 5.6





Example

I hope you have Got What is Insert, Update, Delete In GridView Using ASP.Net C# And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.