Insert update without database in asp.net c#

Insert update and delete(crud operation) normally perform with the database, In This blog, I am going to explain to you, The crud operation in asp.net without using database.

In this example, I take 4 Textbox, ID, Name, Mobile, and Age

Follow The Steps

First, you have to create a new website.

Second, Take a Textbox as per your requirement.

After then, Take a button change the name that you want to write, like Submit or Add.

Next, Double click on button and write the below code on button click event.

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["CompDetails"] == null)
        {
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            dt.Columns.Add("age");
            dt.Columns.Add("mobile");
        }
        else
        {
            dt = (DataTable)ViewState["CompDetails"];
        }
        DataRow dr = dt.NewRow();
        dr["id"] = Convert.ToInt32(TextBox4.Text);
        dr["name"] = (TextBox1.Text);
        dr["age"] = TextBox2.Text;
        dr["mobile"] = TextBox3.Text;
        dt.Rows.Add(dr);
        ViewState["CompDetails"] = dt;
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dt = (DataTable)ViewState["CompDetails"];
        dt.Rows[e.RowIndex].Delete();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

Gridview Events

Now, Generate the RowEditing,RowUpdating,RowDeleting,RowCancelingEdit event of gridview.

RowEditing

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        dt = (DataTable)ViewState["CompDetails"];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

RowCancelingEdit

 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        dt = (DataTable)ViewState["CompDetails"];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

RowDeleting

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dt = (DataTable)ViewState["CompDetails"];
        dt.Rows[e.RowIndex].Delete();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

RowUpdating

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int Id = Convert.ToInt32(((TextBox)row.Cells[1].Controls[0]).Text);
string name = ((TextBox)row.Cells[2].Controls[0]).Text;
string age = ((TextBox)row.Cells[3].Controls[0]).Text;
string mobile = ((TextBox)row.Cells[4].Controls[0]).Text;

DataRow[] datarow = dt.Select(“ID='” + Id + “‘”);
dt.Rows[Id].BeginEdit();
dt.Rows[Id][“name”] = name;
dt.Rows[Id][“age”] = age;
dt.Rows[Id][“mobile”] = mobile;
dt.Rows[Id].EndEdit();
dt.AcceptChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();

}

Here is the whole code,

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

public partial class _Default : System.Web.UI.Page
{
DataTable dt= new DataTable();

protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState[“CompDetails”] == null)
{
dt.Columns.Add(“id”);
dt.Columns.Add(“name”);
dt.Columns.Add(“age”);
dt.Columns.Add(“mobile”);
}

else
{
dt = (DataTable)ViewState[“CompDetails”];
}
DataRow dr = dt.NewRow();
dr[“id”] = Convert.ToInt32(TextBox4.Text);
dr[“name”] = (TextBox1.Text);
dr[“age”] = TextBox2.Text;
dr[“mobile”] = TextBox3.Text;
dt.Rows.Add(dr);
ViewState[“CompDetails”] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dt = (DataTable)ViewState[“CompDetails”];
dt.Rows[e.RowIndex].Delete();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
dt = (DataTable)ViewState[“CompDetails”];
GridView1.DataSource = dt;
GridView1.DataBind();
}


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    int Id = Convert.ToInt32(((TextBox)row.Cells[1].Controls[0]).Text);
    string name = ((TextBox)row.Cells[2].Controls[0]).Text;
    string age = ((TextBox)row.Cells[3].Controls[0]).Text;
    string mobile = ((TextBox)row.Cells[4].Controls[0]).Text;


    DataRow[] datarow = dt.Select("ID='" + Id + "'");
    dt.Rows[Id].BeginEdit();
    dt.Rows[Id]["name"] = name;
    dt.Rows[Id]["age"] = age;
    dt.Rows[Id]["mobile"] = mobile;
    dt.Rows[Id].EndEdit();
    dt.AcceptChanges();
    GridView1.EditIndex = -1;
    GridView1.DataSource = dt;
    GridView1.DataBind();

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    dt = (DataTable)ViewState["CompDetails"];
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

}

Above example is in Asp.Net With c#.

if you have any questions or doubt, Please comment me, I will try to give you best solution.

Thanks for reading whole post,I hope, Now you can do insert update delete without database, Don’t forget to share, It can help to other.

admin Administrator
Hi, this is Brijesh Vishwakarma I am a web developer, at the part-time I write blogs, I like to write the blog and I want to share my knowledge. Mostly I write the blog about Blogging, Online earning, Seo, Website, Domain, and Hosting. Be happy
follow me
×
admin Administrator
Hi, this is Brijesh Vishwakarma I am a web developer, at the part-time I write blogs, I like to write the blog and I want to share my knowledge. Mostly I write the blog about Blogging, Online earning, Seo, Website, Domain, and Hosting. Be happy
Latest Posts
  • How to Start blogging for beginners
  • how to get adsense approval
  • blogging
  • top 5 youtubers

1 thought on “Insert update without database in asp.net c#”

  1. Good post. I learn something new and challenging on blogs I stumbleupon everyday. It will always be helpful to read through articles from other authors and practice a little something from their sites.

    Reply

Leave a Comment