如何将gridview数据保存到具有下拉列表的数据库中。 [英] How to save gridview data to a database which has dropdown lists.

查看:82
本文介绍了如何将gridview数据保存到具有下拉列表的数据库中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个网格视图,它有5列。前2列是学生卷号,其他3列的名称有下拉列表(使用模板创建)。我正在从数据库表中显示rollno和student名称。显示此信息后,如果用户更改了这些下拉列表并希望保存到另一个表,则会出现错误。请查看我的代码。

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.

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

            }



这是从gridview存储数据的最佳方法,还是有其他方法可用?



先谢谢你为我带来痛苦。


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.

推荐答案

我对你的错误提出了建议。你提到错误来自以下行:



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

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





也许将Parse更改为TryParse以捕获任何数据错误,如下所示:





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





由于用户错误,当一个非整数放入该单元格时,上述应该使应用程序不会崩溃。至于从网格视图存储数据的更广泛问题,它取决于您的需求。为了获得最佳性能,您可能会考虑使用数据的本地副本,而不是为每行单独访问数据库(即,将gridview的数据源设置为等于本地副本,然后使用本地副本更新数据库)根据需要提供数据)。本地副本可以使用EntityFramework,LinqToSql,ADO.NET(DataSet,DataTable等),甚至是简单的集合(例如,StudentInfo的通用List,包含您要保留的信息的类)。



但是,如果应用程序仅供内部使用,并且您没有重大数据库问题,则可能无需进行任何操作变化。这一切都取决于你的需求。



祝你好运!



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!


这篇关于如何将gridview数据保存到具有下拉列表的数据库中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆