Best and Cheap Classic ASP Windows Hosting : Insert data in ASP GridView Without Having Making Use of Database

AspGrid is definitely an active server component which brings your ASP application the power of a data-bound grid control. With AspGrid, you’ll be able to construct browser-independent editable grid interfaces to your databases in as small as three traces of ASP code.

AspGrid handle provides many built-in capabilities that enable the user to type, update, delete, pick, and page by means of products within the control. AspGrid handle may be bound to a knowledge resource handle, in order to bind a data resource manage, set the DataSourceID property in the AspGrid handle to the ID value from the information supply handle.

In this publish I’ll clarify the best way to insert data in ASP GridView without utilizing database in ASP.Net using C# and VB.Net.

Insert data in ASP GridView Without Having Making Use of Database

Multiple Rows and columns will be added to GridView with the help of DataTable in ViewState or Session thus the GridView is populated without using database.

HTML Markup

The following HTML Markup consists of an ASP.Net GridView along with some TextBoxes and a Button in order to insert data in GridView control.

<asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false"
EmptyDataText="No records has been added.">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120" />
</Columns>
</asp:GridView>
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
    <td style="padding-bottom: 10px">
        Name:<br />
        <asp:TextBox ID="txtName" runat="server" />
    </td>
</tr>
<tr>
    <td style="padding-bottom: 10px">
        Country:<br />
        <asp:TextBox ID="txtCountry" runat="server" />
    </td>
</tr>
<tr>
    <td style="width: 100px">
        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
    </td>
</tr>
</table>

 You will need to import the following namespace.

C#

using System.Data;

VB.Net

Imports System.Data

Binding the GridView utilizing an Empty DataTable

Originally a DataTable needs to be produced and it is saved inside the ViewState variable after which the BindGrid technique is executed which populates the GridView from your DataTable saved in ViewState variable.

Right here the principal cause to make use of ViewState variable is to preserve the GridView data across PostBacks.

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] {new DataColumn("Name"), new DataColumn("Country") });
        ViewState["Customers"] = dt;
        this.BindGrid();
    }
}
 
protected void BindGrid()
{
    GridView1.DataSource = (DataTable)ViewState["Customers"];
    GridView1.DataBind();
}

 VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Country")})
        ViewState("Customers") = dt
        Me.BindGrid()
    End If
End Sub
 
Protected Sub BindGrid()
    GridView1.DataSource = DirectCast(ViewState("Customers"), DataTable)
    GridView1.DataBind()
End Sub

Inserting data in GridView without using database in ASP.Net

First the DataTable is fetched from the ViewState variable and then a new Row is added to the DataTable by making use of the data from the TextBoxes.

Finally the DataTable is saved back in ViewState variable and the BindGrid method is executed which updates the GridView data.

C#

protected void Insert(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["Customers"];
    dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim());
    ViewState["Customers"] = dt;
    this.BindGrid();
    txtName.Text = string.Empty;
    txtCountry.Text = string.Empty;
}

 VB.Net

Protected Sub Insert(sender As Object, e As EventArgs)
    Dim dt As DataTable = DirectCast(ViewState("Customers"), DataTable)
    dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim())
    ViewState("Customers") = dt
    Me.BindGrid()
    txtName.Text = String.Empty
    txtCountry.Text = String.Empty
End Sub

ASPHostPortal.com : Best and Cheap Windows Hosting With ASPGrid Recommendation

ASPHostPortal.com was established in 2008. It’s been topping the checklist of just about each of the world wide web hosting review so far. Still have websites running on ASP 3.0 or Classic ASP? Don’t worry; they still support legacy technology like Classic ASP Hosting. You can also mix and match Classic ASP and ASP.NET code in one single web site. They will make sure that Classic ASP runs smoothly on their servers and that your website is safer, faster and better supported than anywhere else! Their best and cheap Classic ASP hosting plan is starting at $5.00/mo. ASPHostPortal.com is now providing free domain and double SQL server space for new clients to enjoy the company’s outstanding web hosting service.
[stextbox id=”asphostportal”]ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for the ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, .NET 4.5.2/ASP.NET 4.5.1, ASP.NET MVC 6.0/5.2, Silverlight 5 and Visual Studio Lightswitch. Click here for more information[/stextbox]

Posted in ASP.NET Hosting and tagged , , , , , , .