locked
How to save data from gridview into another sql table? RRS feed

  • Question

  • User2131269066 posted
    Hi! I'm new in programming using Visual Web Developer 2008 express. I using gridview to present data from SQL table on to my program interface. Here where I stuck... How to save the whole gridview data into a test database. Also, there will be a save buttom to execute the program.
     
    I can't find any sample code related to my scenario, also where can I get the sqldataadapter from the toolsbox? Some site mention that you need sqldataadapter components in order to save your data into the database.
     
    Please help cause I been doing the program for 1 months already & still haven't completed yet. Thank!
    Wednesday, December 17, 2008 4:57 AM

Answers

  • User-1635004338 posted

    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();
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 22, 2008 3:26 AM

All replies

  • User-1635004338 posted

    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();
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 22, 2008 3:26 AM
  • User-304683412 posted

    hi MSFT

     

    can i have this code in vb

     

    Wednesday, December 24, 2008 11:31 AM
  • User-1635004338 posted

    Hi haifa,

    Try this:

    Protected Sub SaveButton(ByVal sender As Object, ByVal e As EventArgs)
        Dim conn As New SqlConnection("your_connection_string")
        conn.Open()
        For Each gvr As GridViewRow In GridView1.Rows
            Dim data1 As String = gvr.Cells(0).Text
            'get data from BoundField in each row
            Dim data2 As String = DirectCast(gvr.FindControl("Label1"), Label).Text
            'get data from control in TemplateField in each row
            Dim cmd As New SqlCommand("insert_sql", conn)
            'generate insert sql using above data
            cmd.ExecuteNonQuery()
        Next
        conn.Close()
    End Sub

    Thanks,

    Wednesday, December 24, 2008 9:23 PM