0

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?

6
  • Did you debug the code? Which object in the code is null? Commented Nov 6, 2017 at 16:56
  • Both are. I deleted the first to see if the second would work, but I received the same error Commented Nov 6, 2017 at 17:03
  • Which Both are ? What is first and second? Commented Nov 6, 2017 at 17:05
  • ProdID and OrderQty Commented Nov 6, 2017 at 17:08
  • you have to use session for that or ? Commented Nov 6, 2017 at 18:04

2 Answers 2

1

Try it with this code:

GridViewRow gr1 = (sender as Control).NamingContainer as GridViewRow;
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.