简体   繁体   中英

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

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

 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.

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. if not then enable it and do what i have described above, else put extra details so we can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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