INSERTING DATA IN GRIDVIEW WITHOUT DATABASE THEN INSERT IT INTO DB
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("data source=OPTIPLEX-31C5A6;database=12345;integrated security=true");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
//...................for single record binding.............................//
public void data()
{
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("city");
dt.Columns.Add("area");
DataRow dr = dt.NewRow();
dr["name"] = TextBox1.Text;
dr["city"] = TextBox2.Text;
dr["area"] = TextBox3.Text;
dt.Rows.Add(dr);
DataSet ds = new DataSet();
DataView dv = new DataView();
ds.Tables.Add(dt);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
data();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
foreach (GridViewRow dg in GridView1.Rows)
{
CheckBox c = (CheckBox)dg.FindControl("CheckBox1");
if (c.Checked)
{
string name = dg.Cells[1].Text;
string city = dg.Cells[2].Text;
string area = dg.Cells[3].Text;
string s = "insert into gtask(name,addrs,area)values('" + name + "','" + city + "','" + area + "')";
SqlCommand cmd = new SqlCommand(s, con);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
//...................for multipule record binding.............................//
private void BindGrid(int rowcount)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("TextBox1Column", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("TextBox2Column", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("TextBox3Column", typeof(String)));
if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();
}
}
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;
dt.Rows.Add(dr);
}
else
{
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;
dt.Rows.Add(dr);
}
// If ViewState has a data then use the value as the DataSource
if (ViewState["CurrentData"] != null)
{
GridView1.DataSource = (DataTable)ViewState["CurrentData"];
GridView1.DataBind();
}
else
{
// Bind GridView with the initial data assocaited in the DataTable
GridView1.DataSource = dt;
GridView1.DataBind();
}
// Store the DataTable in ViewState to retain the values
ViewState["CurrentData"] = dt;
}
protected void Button3_Click(object sender, EventArgs e)
{
// Check if the ViewState has a data assoiciated within it. If
if (ViewState["CurrentData"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentData"];
int count = dt.Rows.Count;
BindGrid(count);
}
else
{
BindGrid(1);
}
TextBox1.Text = string.Empty;
TextBox1.Focus();
}
}
No comments:
Post a Comment