Click here to Skip to main content
11,645,087 members (70,727 online)
Rate this: bad
Please Sign up or sign in to vote.
Hi Every body

I am trying to pass a value form a GridView to a TextBox or a session.
Session["SEmpID"] = GridView1.SelectedRow.FindControl("SubjectID").ToString();
TXTTest.Text = GridView1.SelectedRow.FindControl("SubjectID").ToString();
But I always recive the error:
Object reference not set to an instance of an object.

I hope any one can solve it to me.

Thank you
Posted 18-Jan-13 4:12am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

GridView1.SelectedRow.FindControl("SubjectID") is null.
When you try to cast null to ToString(), you will get an error.

Try to find out why GridView1.SelectedRow.FindControl("SubjectID") is not returning anything.
Espen Harlinn at 18-Jan-13 11:48am
Good point :-D
Abhinav S at 18-Jan-13 12:03pm
Thank you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


1) Is your given code in GridView1_SelectedIndexChanged() event.

2) Where you set GridView DataSource
if it is in page_load() event then it should be in

 if (!IsPostBack) { GridView1.DataSource = "xyzDataset" }

Please mark it as ans & rate if you find it helps you.
Thank You
Asp.Net/C#.Net Help[^]
Hemant Singh
Nasser Abu Farah at 18-Jan-13 10:50am
I added a button form the edit tiplatte in the grid view. and place the code in the button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 700
1 494
2 Sergey Alexandrovich Kryukov 439
3 Afzaal Ahmad Zeeshan 404
4 CPallini 386
0 OriginalGriff 1,065
1 Sergey Alexandrovich Kryukov 683
2 DamithSL 681
3 Afzaal Ahmad Zeeshan 662
4 CPallini 555

Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100