Click here to Skip to main content
15,896,475 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
I have a grid view, which has 5 columns. First 2 columns are student roll no,and name other 3 columns have dropdown lists( created using template). I am displaying rollno and student name from a database table. After displaying this, If a user changes these dropdownlist and want to save to another table, I am getting error. Kindly have a look at my code.
C#
int id;
            string a1,a2,a3;
            int rowIndex = 0;
            
           foreach (GridViewRow g1 in GridView1.Rows)
            {
                
                DropDownList box2 = (DropDownList)GridView1.Rows[rowIndex].Cells[0].FindControl("dd1");
                DropDownList box3 = (DropDownList)GridView1.Rows[rowIndex].Cells[0].FindControl("dd2");
                DropDownList box4 = (DropDownList)GridView1.Rows[rowIndex].Cells[0].FindControl("dd3");
                rowIndex++;
                id = int.Parse(g1.Cells[1].Text); I am getting error on this line
                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();

            }

Is this is the best approach to store data from gridview, or there any other way is available??

Thanks in advance for taking pain for me.
Posted
Updated 21-Apr-13 3:16am
v2

1 solution

I have a suggestion regarding your error. You mention the error is coming from the following line:

C#
id = int.Parse(g1.Cells[1].Text);


Maybe change the Parse to a TryParse to catch any data errors, like the following:

C#
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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900