简体   繁体   中英

Asp.net Gridviews and databases

This may be a long shot, but I'm trying to transfer data from GridView1(product database table) into GridView2(order database table), and I'm lost.

Here's what I'm trying:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gr1 = GridView1.SelectedRow;

    gr1.Cells[1].Text = SqlDataSource3.InsertParameters["ProdId"].DefaultValue;
    TextBox tb1 = gr1.FindControl("TextBox1") as TextBox;
    SqlDataSource2.InsertParameters["OrderQty"].DefaultValue = tb1.Text;


}

The 3rd Line GridViewRow gr1..... has a select button. I created a new column with a textbox box that should transfer the OrderQty .

I have two SqlDataSources, one for Product(SqlDataSource2) and one for Order(SqlDataSource3). I keep getting an error:

System.NullReferenceException: Object reference not set to an instance of an object.

Can anyone see where I'm going wrong here?

尝试使用以下代码:

GridViewRow gr1 = (sender as Control).NamingContainer as GridViewRow;

Here's what I did to get it to work:

GridViewRow gr1 = GridView1.SelectedRow;

    Label lblProdID = gr1.FindControl("Label1") as Label;

    SqlDataSource3.InsertParameters["OrderDate"].DefaultValue = "11/06/2017";
    SqlDataSource3.InsertParameters["CustID"].DefaultValue = TextBox2.Text;
    SqlDataSource3.InsertParameters["OrderID"].DefaultValue = "12346";
    SqlDataSource3.InsertParameters["ProdID"].DefaultValue = lblProdID.Text;
    SqlDataSource3.InsertParameters["OrderQty"].DefaultValue = ((TextBox)gr1.FindControl("TextBox1")).Text;
    SqlDataSource3.Insert();

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