How can i insert number of new rows with help of sessions.... public partial class DataTable_Full : System.Web.UI.Page { DataTable dt = new DataTable(); prote
public partial class DataTable_Full : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
DataColumn col1 = new DataColumn("Empid", typeof(System.Int32));
DataColumn col2 = new DataColumn("Name", typeof(string));
DataColumn col3 = new DataColumn("City", typeof(string));
DataColumn col4 = new DataColumn("Mobile", typeof(System.Int64));
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.Columns.Add(col4);
col1.AutoIncrement = true;
col1.AutoIncrementSeed = 1;
col1.ReadOnly = true;
//If you want a particular column to be a unique column ie. you don't want duplicate records into that column, then set its Unique property to true like below.
//auto.Unique = true;
DataColumn[] pk = new DataColumn[1];
pk[0] = col1;
dt.PrimaryKey = pk;
DataRow dr;
dr = dt.NewRow();
dr["Empid"] = "33";
dr["Name"] = "joshua";
dr["City"] = "Illinois";
dr["Mobile"] = "9999367456";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Empid"] = "66";
dr["Name"] = "Amanda";
dr["City"] = "Kentuchky";
dr["Mobile"] = "9965465456";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["mytable"] = dt;
}
protected void txtsubmit_Click(object sender, EventArgs e)
{
DataRow dr;
dr = dt.NewRow();
dr["Name"] = txtname.Text;
dr["City"] = txtcity.Text;
dr["Mobile"] = txtmobile.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
txtname.Text = GridView1.SelectedRow.Cells[0].Text;
txtcity.Text = GridView1.SelectedRow.Cells[1].Text;
txtmobile.Text = GridView1.SelectedRow.Cells[2].Text;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
dt = (DataTable)Session["mytable"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//
}
}
First I have to say that sorry but you are wrong in concept——You put creating DataTable codes into Page_Load,which will cause the problem of creating new DataTable again and again……You should put it into the if(!IsPostBack){……}block and then use the thing
in session or viewstate:
【Solution】
public partial class DataTable_Full : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
DataTable dt =newDataTable();
DataColumn col1 =newDataColumn("Empid",typeof(System.Int32)); DataColumn col2 =newDataColumn("Name",typeof(string)); DataColumn col3 =newDataColumn("City",typeof(string)); DataColumn col4 =newDataColumn("Mobile",typeof(System.Int64)); dt.Columns.Add(col1); dt.Columns.Add(col2); dt.Columns.Add(col3); dt.Columns.Add(col4); col1.AutoIncrement=true; col1.AutoIncrementSeed=1; col1.ReadOnly=true; //If you want a particular column to be a unique column ie. you don't want duplicate records into that column, then set its Unique property to true like below. //auto.Unique = true; DataColumn[] pk =newDataColumn[1]; pk[0]= col1; dt.PrimaryKey= pk; DataRow dr; dr = dt.NewRow(); dr["Empid"]="33"; dr["Name"]="joshua"; dr["City"]="Illinois"; dr["Mobile"]="9999367456"; dt.Rows.Add(dr);
Error: object reference not set to an instance of an object
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//DataTable dt = ViewState["mytable"] as DataTable;
DataTable dt = new DataTable();
dt = (DataTable)ViewState["mytable"];
dt.Rows[e.RowIndex][1] = (GridView1.Rows[e.RowIndex].FindControl("txtname") as TextBox).Text;
dt.Rows[e.RowIndex][2] = (GridView1.Rows[e.RowIndex].FindControl("txtcity") as TextBox).Text;
dt.Rows[e.RowIndex][3] = (GridView1.Rows[e.RowIndex].FindControl("txtmobile") as TextBox).Text;
GridView1.DataSource = dt;
GridView1.DataBind();
}
Make sure that your GridView's cell index isn't out of bound!
txtname.Text = GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
txtcity.Text = GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
txtmobile.Text = GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
rookie tiro
Member
86 Points
130 Posts
DataTable
Apr 28, 2012 04:23 PM|LINK
How can i insert number of new rows with help of sessions.... public partial class DataTable_Full : System.Web.UI.Page { DataTable dt = new DataTable(); prote
public partial class DataTable_Full : System.Web.UI.Page { DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { DataColumn col1 = new DataColumn("Empid", typeof(System.Int32)); DataColumn col2 = new DataColumn("Name", typeof(string)); DataColumn col3 = new DataColumn("City", typeof(string)); DataColumn col4 = new DataColumn("Mobile", typeof(System.Int64)); dt.Columns.Add(col1); dt.Columns.Add(col2); dt.Columns.Add(col3); dt.Columns.Add(col4); col1.AutoIncrement = true; col1.AutoIncrementSeed = 1; col1.ReadOnly = true; //If you want a particular column to be a unique column ie. you don't want duplicate records into that column, then set its Unique property to true like below. //auto.Unique = true; DataColumn[] pk = new DataColumn[1]; pk[0] = col1; dt.PrimaryKey = pk; DataRow dr; dr = dt.NewRow(); dr["Empid"] = "33"; dr["Name"] = "joshua"; dr["City"] = "Illinois"; dr["Mobile"] = "9999367456"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Empid"] = "66"; dr["Name"] = "Amanda"; dr["City"] = "Kentuchky"; dr["Mobile"] = "9965465456"; dt.Rows.Add(dr); GridView1.DataSource = dt; GridView1.DataBind(); Session["mytable"] = dt; } protected void txtsubmit_Click(object sender, EventArgs e) { DataRow dr; dr = dt.NewRow(); dr["Name"] = txtname.Text; dr["City"] = txtcity.Text; dr["Mobile"] = txtmobile.Text; dt.Rows.Add(dr); GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { txtname.Text = GridView1.SelectedRow.Cells[0].Text; txtcity.Text = GridView1.SelectedRow.Cells[1].Text; txtmobile.Text = GridView1.SelectedRow.Cells[2].Text; } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; dt = (DataTable)Session["mytable"]; GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { // } }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: DataTable
Apr 30, 2012 01:18 AM|LINK
Hello:)
First I have to say that sorry but you are wrong in concept——You put creating DataTable codes into Page_Load,which will cause the problem of creating new DataTable again and again……You should put it into the if(!IsPostBack){……}block and then use the thing in session or viewstate:
【Solution】
public partial class DataTable_Full : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) {rookie tiro
Member
86 Points
130 Posts
Re: DataTable
Apr 30, 2012 03:38 AM|LINK
Error: object reference not set to an instance of an object protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //DataTable dt = ViewState["mytable"] as DataTable; DataTable dt = new DataTable(); dt = (DataTable)ViewState["mytable"]; dt.Rows[e.RowIndex][1] = (GridView1.Rows[e.RowIndex].FindControl("txtname") as TextBox).Text; dt.Rows[e.RowIndex][2] = (GridView1.Rows[e.RowIndex].FindControl("txtcity") as TextBox).Text; dt.Rows[e.RowIndex][3] = (GridView1.Rows[e.RowIndex].FindControl("txtmobile") as TextBox).Text; GridView1.DataSource = dt; GridView1.DataBind(); }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: DataTable
Apr 30, 2012 06:49 AM|LINK
At which statement?
rookie tiro
Member
86 Points
130 Posts
Re: DataTable
Apr 30, 2012 06:55 AM|LINK
dt.Rows[e.RowIndex][1] = (GridView1.Rows[e.RowIndex].FindControl("txtname") as TextBox).Text;
TimoYang
Contributor
3732 Points
1275 Posts
Re: DataTable
Apr 30, 2012 06:59 AM|LINK
(GridView1.Rows[e.RowIndex].Cells[index].Controls[0] as TextBox).Text;
rookie tiro
Member
86 Points
130 Posts
Re: DataTable
Apr 30, 2012 09:51 AM|LINK
Specified argument was out of the range of valid values. Parameter name: index protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { DataTable dt = new DataTable(); dt = (DataTable)ViewState["mytable"]; dt.Rows[e.RowIndex][1] = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]; dt.Rows[e.RowIndex][2] = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]; dt.Rows[e.RowIndex][3] = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]; GridView1.DataSource = dt; GridView1.DataBind(); }TimoYang
Contributor
3732 Points
1275 Posts
Re: DataTable
May 01, 2012 02:29 AM|LINK
Check row number, cell number……
And plz show us your aspx codes of GridView!
rookie tiro
Member
86 Points
130 Posts
Re: DataTable
May 01, 2012 04:06 AM|LINK
<table align="center" > <tr> <td> NAME</td> <td> <asp:TextBox ID="txtname" runat="server"></asp:TextBox> </td> </tr> <tr> <td> CITY</td> <td> <asp:TextBox ID="txtcity" runat="server"></asp:TextBox> </td> </tr> <tr> <td> MOBILE</td> <td> <asp:TextBox ID="txtmobile" runat="server"></asp:TextBox> </td> </tr> <tr> <td colspan="2" align="center"> <asp:Button ID="txtsubmit" runat="server" Text="SUBMIT" onclick="txtsubmit_Click" /> </td> </tr> </table><asp:GridView ID="GridView1" runat="server" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" onrowcancelingedit="GridView1_RowCancelingEdit"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowSelectButton="True" /> </Columns> </asp:GridView>public partial class DataTable_Full : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); DataColumn col1 = new DataColumn("Empid", typeof(System.Int32)); DataColumn col2 = new DataColumn("Name", typeof(string)); DataColumn col3 = new DataColumn("City", typeof(string)); DataColumn col4 = new DataColumn("Mobile", typeof(System.Int64)); dt.Columns.Add(col1); dt.Columns.Add(col2); dt.Columns.Add(col3); dt.Columns.Add(col4); col1.AutoIncrement = true; col1.AutoIncrementSeed = 1; col1.ReadOnly = true; DataColumn[] pk = new DataColumn[1]; pk[0] = col1; dt.PrimaryKey = pk; DataRow dr; dr = dt.NewRow(); dr["Empid"] = "33"; dr["Name"] = "joshua"; dr["City"] = "Illinois"; dr["Mobile"] = "9999367456"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Empid"] = "66"; dr["Name"] = "Amanda"; dr["City"] = "Kentuchky"; dr["Mobile"] = "9965465456"; dt.Rows.Add(dr); GridView1.DataSource = dt; GridView1.DataBind(); ViewState["mytable"] = dt; } } protected void txtsubmit_Click(object sender, EventArgs e) { DataRow dr; DataTable dt = new DataTable(); dt = (DataTable)ViewState["mytable"]; dr = dt.NewRow(); dr["Name"] = txtname.Text; dr["City"] = txtcity.Text; dr["Mobile"] = txtmobile.Text; dt.Rows.Add(dr); GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { txtname.Text = GridView1.SelectedRow.Cells[2].Text; txtcity.Text = GridView1.SelectedRow.Cells[3].Text; txtmobile.Text = GridView1.SelectedRow.Cells[4].Text; } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; DataTable dt = new DataTable(); dt = (DataTable)ViewState["mytable"]; GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { txtname.Text = GridView1.Rows[e.RowIndex].Cells[3].Text; txtcity.Text = GridView1.Rows[e.RowIndex].Cells[4].Text; txtmobile.Text = GridView1.Rows[e.RowIndex].Cells[5].Text; DataRow dr = ((DataTable)ViewState["mytable"]).Rows[e.RowIndex]; GridView1.DataSource = (DataTable)ViewState["mytable"]; GridView1.DataBind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { DataTable dt = new DataTable(); dt = (DataTable)ViewState["mytable"]; GridView1.EditIndex = -1; GridView1.DataSource = dt; GridView1.DataBind(); } }TimoYang
Contributor
3732 Points
1275 Posts
Re: DataTable
May 01, 2012 04:58 AM|LINK
Hello——
Please use this instead——
Make sure that your GridView's cell index isn't out of bound!