locked
How to save data from gridview into another sql table? Last post 12-24-2008 09:23 PM by Qin Dian Tang - MSFT RRS feed

  • Question

  • User-1847166894 posted

    Below is answer given to this question

    My Question is: Does that code works for all rows contained (even those paginated not shown) or just the values of current page displayed?

    I am trying to make this implementation for saving data to an intersection table but displaying first the data from one of its conforming wings.

    As you already know, in a many-to-many table with additional columns plus its own primary key, if that table is still empty, there is no way you can collect data unless you do that from its conforming wings (I don't even know if that's the right naming for them), like when you have the Employee-Regions combination. The many-to-many ASP.Net Dynamic Data Control works perfectly for doing this operation BUT problem is that that control only displays a CheckBox for Selecting/Unselecting and assigning them (Employees ) to specific Regions BUT that's not enough if you want to also gather some other data pertaining to such assignment, in which case the info displayed in the 1st grid plus all additional extra columns required to gather the complementary data (the one of the intersection table) once it has been completed (even better if it were kind of bulk insert) then data should be saved in the Employee-Regions table not in Regions neither in Employees

    **************************************************************************
    EXCUSE MY ENGLISH WRITING ... I AM SPANISH SPEAKING PERSON
    **************************************************************************

    Carlos Porras

    .

    .

    Re: How to save data from gridview into another sql table?

    12-22-2008 03:26 AM|LINK

    Hi zeroiris,

    You can loop through the whole GridView, and get data from each row. Then insert these data (each row) into test table row by row. Here is my sample:

    protected void SaveButton(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(@"your_connection_string");
            conn.Open();
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                string data1 = gvr.Cells[0].Text;    //get data from BoundField in each row
                string data2 = ((Label)gvr.FindControl("Label1")).Text;    //get data from control in TemplateField in each row
                SqlCommand cmd = new SqlCommand("insert_sql", conn);   //generate insert sql using above data
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }

    Friday, March 31, 2017 10:04 PM

Answers

  • User-1509636757 posted

    My Question is: Does that code works for all rows contained (even those paginated not shown) or just the values of current page displayed?

    Above code will only works for current page that is being displayed, if pagination is enabled in GridView.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 2, 2017 8:25 AM