简体   繁体   English

从gridview单元格获取值到数据库

[英]get values from gridview cell to datatbase

i want to get the values in the cell and store it in a variable which will then be inserted into a database 我想获取单元格中的值并将其存储在变量中,然后将其插入数据库中

i displaing products in girdview i want to lett the user to chose the product when that hapend i want the program to send an order to the data base 我在girdview中放置产品我想让用户在这种情况下选择产品我希望程序将订单发送到数据库

i want just to insert it in to order table but i dont konw how this code is fine when i using a textbox 我只想将其插入订单表中,但我不知道我使用文本框时此代码的效果如何

 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Insert Into OrdarTable (TotalPrice ) VALUES (@Price)", conn);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@Price", .............);
    conn.Open();
    cmd.ExecuteNonQuery();

but not with girdview i sid if i convert the data in the cell to string and desplaing them in lable then inserting them to the table like that 但是如果我将单元格中的数据转换为字符串并将其放在标签中,然后将它们插入表中,则不使用girdview

 string s = grdCart.SelectedRow.Cells[4].Text;

it does not wark and i get this error 它不会扭曲,我得到这个错误

Object reference not set to an instance of an object.

The error you have means that there is no SelectedRow, it returns null so you can get a cell value or even access cells from it cause it's null. 您遇到的错误意味着没有SelectedRow,它返回null,因此您可以获取一个单元格值,甚至可以从中访问单元格,因为它为null。

Since you have used "SelectedRow" i assume that you have AutoGenerateSelectButton propery set to true, if so then you have to go the gridview_SelectedIndexedChaning event and put your code in it. 由于您使用了“ SelectedRow”,因此我假设您将AutoGenerateSelectButton属性设置为true,如果是这样,则必须进行gridview_SelectedIndexedChaning事件并将代码放入其中。 if not then enable it and do what i have described above, else put extra details so we can help you. 如果没有,则启用它并执行我上面描述的操作,否则,请添加其他详细信息,以便我们为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM