<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demo.aspx.cs" Inherits=" Demosolution.demo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
Name:
<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
<br />
Address:<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
<br />
<br />
<asp:GridView ID="TempGrid" runat="server" ShowHeaderWhenEmpty="True" EmptyDataText="No
records Found"></asp:GridView><br />
<asp:Button ID="btnsavetodb" runat="server" Text="Save to Database" OnClick="btnsavetodb_Click" />
</form>
</body>
</html>
using Demosolution.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demosolution
{
public partial class demo : System.Web.UI.Page
{
Database1Entities db = new Database1Entities();
Tbl_address qt = new Tbl_address();
DataTable tb = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Createtable();
}
}
public void Createtable()
{
tb.Columns.Add("Slno", typeof(Int32));
tb.Columns.Add("Name", typeof(string));
tb.Columns.Add("Address", typeof(string));
TempGrid.DataSource = tb;
TempGrid.DataBind();
ViewState["table1"] = tb;
}
protected void btnSave_Click(object sender, EventArgs e)
{
tb = (DataTable)ViewState["table1"];
dr =
tb.NewRow();
int x = tb.Rows.Count;
dr["Slno"] = Convert.ToInt32(x + 1);
dr["Name"] = txtname.Text;
dr["Address"] =
txtaddress.Text;
tb.Rows.Add(dr);
TempGrid.DataSource = tb;
TempGrid.DataBind();
txtname.Text = "";
txtaddress.Text = "";
}
protected void btnsavetodb_Click(object sender, EventArgs e)
{
tb = (DataTable)ViewState["table1"];
int x = tb.Rows.Count;
for (int i = 0; i < x; i++)
{
string name = tb.Rows[i]["Name"].ToString();
string address = tb.Rows[i]["Address"].ToString();
tb.Name
= Convert.ToInt32(name);
tb.Address =
Convert.ToInt32(address);
db.Tbl_address.Add(tb);
db.SaveChanges();
}
}
No comments:
Post a Comment