Bob
Bob

Reputation: 91

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?

Upvotes: 0

Views: 39

Answers (2)

Bob
Bob

Reputation: 91

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

Upvotes: 0

user1773603
user1773603

Reputation:

Try it with this code:

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

Upvotes: 1

Related Questions