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#.
In previous article I already explained about How to bind repeater using SqlDataAdapter, DataTable and Stored procedure in Sql server using ASP.NET,VB.NET/C#,How to bind gridview using SqlDataAdapter, DataTable and Stored procedure in Sql server,How to bind data to Gridview using SqlDataAdapter, SqlCommand, DataSet and Stored procedure in ASP.NET.
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();
}
|
No comments:
Post a Comment