Saturday, 7 September 2013

How to insert the data from the TextBox into the Grid View without using any database in ASP.NET using VB.NET/C#

Hi friends, in this article I will explain about how to Insert the data from the textbox into the Grid View without using any database in ASP.NET using VB.NET/C#.
Let’s see how to insert the data from the textbox into the Grid View without using any database in ASP.NET using VB.NET/C#.
First take one web page and take two labels ,two textboxes ,one button and one GridView as shown in the below figure.

Code for ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" Width="193px"></asp:TextBox><br />
        <asp:Label ID="Label2" runat="server" Text="Sitename"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" Width="204px"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Display" />
        <asp:GridView ID="gddisplay" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

And add namespace
Imports System.Data for VB.NET
using System.Data for C#. 
Actually we save the Data into DataTable after that we bind that data to GridView.
Code for VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dtbl = New DataTable()
        Dim data_col1 As New DataColumn("Username")
        Dim data_col2 As New DataColumn("Sitename")
        dtbl.Columns.Add(data_col1)
        dtbl.Columns.Add(data_col2)
        Dim drw1 As DataRow = dtbl.NewRow()
        gddisplay.DataSource = dtbl
        gddisplay.DataBind()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim drw1 As DataRow = dtbl.NewRow()
        drw1(0) = TextBox1.Text
        drw1(1) = TextBox2.Text
        dtbl.Rows.Add(drw1)
        gddisplay.DataSource = dtbl
        gddisplay.DataBind()
    End Sub

Code for C#.NET:
protected void Page_Load(object sender, System.EventArgs e)
{
       dtbl = new DataTable();
       DataColumn data_col1 = new DataColumn("Username");
       DataColumn data_col2 = new DataColumn("Sitename");
       dtbl.Columns.Add(data_col1);
       dtbl.Columns.Add(data_col2);
       DataRow drw1 = dtbl.NewRow();
       gddisplay.DataSource = dtbl;
       gddisplay.DataBind();
}

protected void Button1_Click(object sender, System.EventArgs e)
{
       DataRow drw1 = dtbl.NewRow();
       drw1[0] = TextBox1.Text;
       drw1[1] = TextBox2.Text;
       dtbl.Rows.Add(drw1);
       gddisplay.DataSource = dtbl;
       gddisplay.DataBind();
}

Save and run the program. The output of above program as shown in the below figure.

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.