Saturday, January 8, 2011

add gridview data to database

     foreach (DataRow dr in GridView1.Rows)
        {
// assumed that you have four columns in your gridview
            string t1 = dr[0].ToString();
             string t2 = dr[1].ToString();
             string dd1 =  dr[2].ToString();   
             string t3 = dr[3].ToString();

             string sql = "insert into table1 values('" + t1 + "','" + t2 + "','" + dd1 + "','" + t3 + "')";

           SqlConnection cn = new SqlConnection("conn");
           SqlCommand cmd = new SqlCommand(sql, cn);
             cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }

Here iterate through all the rows in gridview, get each cell value
from the cell values genearate sql string and then execute query.

No comments:

Post a Comment