hi I want a C code to insert multiple rows records from GridView based on CheckBox selection through paging into sql server database and display how many records are inserted. Database faculty contains fidint and fnamevarchar. Please help. Its urgent..
Please refer below articles
I hope this will help you out.
Hi,
Thanks for the prompt reply.I had visited both the links before but still the question from only last page are going into database. Records selected from previous pages are not saved. It would be great if i can get the direct code.
Thanks
Please share your code.
This is the code. It is maintaining the persistence.But i am not able to write code on insert button on web form. When i tried writing for insert button using sqlbulk command it inserted only the last page records. As i had erased that code there only so its not present in this code. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.GetData(); } } protected void PaginateTheData(object sender, GridViewPageEventArgs e) { List<int> list = new List<int>(); if (ViewState["SelectedRecords"] != null) { list = (List<int>)ViewState["SelectedRecords"]; } foreach (GridViewRow row in GridView1.Rows) { CheckBox chk = (CheckBox)row.FindControl("chkSelect"); var selectedKey = int.Parse(GridView1.DataKeys[row.RowIndex].Value.ToString()); if (chk.Checked) { if (!list.Contains(selectedKey)) { list.Add(selectedKey); } } else { if (list.Contains(selectedKey)) { list.Remove(selectedKey); } } } ViewState["SelectedRecords"] = list; GridView1.PageIndex = e.NewPageIndex; this.GetData(); } /// <summary> /// Gets the data. /// </summary> private void GetData() { // DataTable table = new DataTable(); // get the connection using (con) { using (SqlCommand cmd = new SqlCommand("Select fid, fname from tblfaculty", con)) { using (SqlDataAdapter da = new SqlDataAdapter()) { con.Open(); da.SelectCommand = cmd; using (DataTable dt = new DataTable()) { da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } } // GridView1.DataSource = table; // GridView1.DataBind(); /// <summary> /// Gets the selected records. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> /// instance containing the event data.</param> protected void GetSelectedRecords(object sender, EventArgs e) { Response.Write("<h3>Selected records</h3>"); List<int> list = ViewState["SelectedRecords"] as List<int>; if (list != null) { foreach (int id in list) { Response.Write(id.ToString() + "<br />"); } } } /// <summary> /// Looks for selection. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <seecref="System.Web.UI.WebControls.GridViewRowEventArgs"/> /// instance containing the event data.</param> protected void ReSelectSelectedRecords(object sender, GridViewRowEventArgs e) { List<int> list = ViewState["SelectedRecords"] as List<int>; if (e.Row.RowType == DataControlRowType.DataRow && list != null) { var autoId = int.Parse(GridView1.DataKeys[e.Row.RowIndex].Value.ToString()); if (list.Contains(autoId)) { CheckBox chk = (CheckBox)e.Row.FindControl("chkSelect"); chk.Checked = true; } } } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } } }
Please refer below article
you can write code to insertion instead of exporting
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.