GridView - NullReferenceException
-
Hello All, I am stuck on a very simple problem and I at a loss at this point. I have checked the message board but could not find a solution to my particular problem so that is why I am coming to you today. Here goes: I have a very simple page with a 2 GridViews and a text box (simple master/detail setup). The first Gridview is bound to an SQLDataSource which runs a simple stored procedure and fills the gridview upon it's first load. The gridview has a Selection link on it which when clicked passes an argument to the second gridview which is then populated with the details of the selected row. Here's the problem, I want to capture another field of the selected row an show it in a textbox. Simple. Not so (for me anyways). In the Page_Load and SelectedIndexChanged event I simply have 'this.TextBox1.Text = this.GridView1.SelectedRow.Cells[2].Text.ToString();' The SelectedIndexChanged Event doesn't fire before the postback because I get an error in my Page_Load event that there is a NullReferenceException on the Gridview. Can someone be kind enough to guide me in the right direction on how to cure this ill. Greatly appreciated.....FF
-
Hello All, I am stuck on a very simple problem and I at a loss at this point. I have checked the message board but could not find a solution to my particular problem so that is why I am coming to you today. Here goes: I have a very simple page with a 2 GridViews and a text box (simple master/detail setup). The first Gridview is bound to an SQLDataSource which runs a simple stored procedure and fills the gridview upon it's first load. The gridview has a Selection link on it which when clicked passes an argument to the second gridview which is then populated with the details of the selected row. Here's the problem, I want to capture another field of the selected row an show it in a textbox. Simple. Not so (for me anyways). In the Page_Load and SelectedIndexChanged event I simply have 'this.TextBox1.Text = this.GridView1.SelectedRow.Cells[2].Text.ToString();' The SelectedIndexChanged Event doesn't fire before the postback because I get an error in my Page_Load event that there is a NullReferenceException on the Gridview. Can someone be kind enough to guide me in the right direction on how to cure this ill. Greatly appreciated.....FF
Well, writing the above code in page load doesn't make sense. When the page loads first time,
SelectedRow
will beNULL
. Is this throwing error when you use it in the selected index changed event? :)Navaneeth How to use google | Ask smart questions
-
Well, writing the above code in page load doesn't make sense. When the page loads first time,
SelectedRow
will beNULL
. Is this throwing error when you use it in the selected index changed event? :)Navaneeth How to use google | Ask smart questions
Well I hope I am not the only one who has ever done this, I have faith that I am not. But, I thought about just what you replied Navaneeth, and realized the code in the page_load didn't make sense. So I removed it but the SelectedIndexChanged event never fired. So I (just now) reviewed the event. I works now because I put the code in the GridView1 event as opposed to the "GridView2" event. Thanks for your reply and I shall bow out humbly now...
-
Hello All, I am stuck on a very simple problem and I at a loss at this point. I have checked the message board but could not find a solution to my particular problem so that is why I am coming to you today. Here goes: I have a very simple page with a 2 GridViews and a text box (simple master/detail setup). The first Gridview is bound to an SQLDataSource which runs a simple stored procedure and fills the gridview upon it's first load. The gridview has a Selection link on it which when clicked passes an argument to the second gridview which is then populated with the details of the selected row. Here's the problem, I want to capture another field of the selected row an show it in a textbox. Simple. Not so (for me anyways). In the Page_Load and SelectedIndexChanged event I simply have 'this.TextBox1.Text = this.GridView1.SelectedRow.Cells[2].Text.ToString();' The SelectedIndexChanged Event doesn't fire before the postback because I get an error in my Page_Load event that there is a NullReferenceException on the Gridview. Can someone be kind enough to guide me in the right direction on how to cure this ill. Greatly appreciated.....FF
-
Hello All, I am stuck on a very simple problem and I at a loss at this point. I have checked the message board but could not find a solution to my particular problem so that is why I am coming to you today. Here goes: I have a very simple page with a 2 GridViews and a text box (simple master/detail setup). The first Gridview is bound to an SQLDataSource which runs a simple stored procedure and fills the gridview upon it's first load. The gridview has a Selection link on it which when clicked passes an argument to the second gridview which is then populated with the details of the selected row. Here's the problem, I want to capture another field of the selected row an show it in a textbox. Simple. Not so (for me anyways). In the Page_Load and SelectedIndexChanged event I simply have 'this.TextBox1.Text = this.GridView1.SelectedRow.Cells[2].Text.ToString();' The SelectedIndexChanged Event doesn't fire before the postback because I get an error in my Page_Load event that there is a NullReferenceException on the Gridview. Can someone be kind enough to guide me in the right direction on how to cure this ill. Greatly appreciated.....FF
ffowler wrote:
In the Page_Load and SelectedIndexChanged event I simply have 'this.TextBox1.Text = this.GridView1.SelectedRow.Cells[2].Text.ToString();' The SelectedIndexChanged Event doesn't fire before the postback because I get an error in my Page_Load event that there is a NullReferenceException on the Gridview.
Check Selected Row index for null before the statement.
cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net View My Recent Article