I have a suggestion regarding your error. You mention the error is coming from the following line:
id = int.Parse(g1.Cells[1].Text);
Maybe change the Parse to a TryParse to catch any data errors, like the following:
if(int.TryParse(g1.Cells[1].Text, out id))
{
a1 = box2.SelectedValue.ToString();
a2 = box3.SelectedValue.ToString();
a3 = box4.SelectedValue.ToString();
SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[class7]([IdNo],
[SectionNo],[Date],[math],[science],[social]) VALUES
(" + id + "," + sect + ",getdate(),'" + a1 + "','" + a2 + "','"
+ a3 + "')", con);
cmd.ExecuteNonQuery();
}
The above should keep the application from crashing when a non-integer is put into that cell due to user error. As for the broader issue of storing the data from the grid view, it depends on your needs. For best performance, you might think about working with a local copy of the data rather than making separate trips to the database for each row (i.e., set the data source of the gridview equal to the local copy, then update the database with the local data as needed). The local copy could use EntityFramework, LinqToSql, ADO.NET (DataSet, DataTable, etc.), or even a simple collection (e.g., a generic List of StudentInfo, a class containing the info you want to keep).
However, if the application is just for internal use, for instance, and you don't have major database issues, then there may be no need to make any changes. It all depends on your needs.
Best of luck!