1

I want have the gridview on one page as below

<asp:GridView ID="gvDoctorList" runat="server" 
           AutoGenerateColumns="False" 
           DataSourceID="SqlDataSource1" 
           AllowPaging="True" 
           AllowSorting="True" 
           AutoGenerateEditButton="true" 
           AutoGenerateDeleteButton="true">
     <Columns>
         <asp:TemplateField>
              <ItemTemplate>
                   <asp:CheckBox runat="server" 
                                 ID="chk" 
                                 OnCheckedChanged="chk_CheckedChanged"
                                 AutoPostBack="true"/>
                   <asp:Label runat="server" ID="lblPID" Visible="false" Text='<%# Eval("PatientId") %>'></asp:Label>
              </ItemTemplate>
         </asp:TemplateField>
         <asp:BoundField DataField="PatientId" 
                         HeaderText="PatientId" 
                         SortExpression="PatientId" />
         <asp:BoundField DataField="firstname" 
                         HeaderText="firstname" 
                         SortExpression="firstname" />
         <asp:BoundField DataField="lastname" 
                         HeaderText="lastname" 
                         SortExpression="lastname" />
          <asp:BoundField DataField="sex" 
                         HeaderText="sex" 
                         SortExpression="sex" />
     </Columns>
  </asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                     ConnectionString="<%$ ConnectionStrings:MyDatabaseConnectionString %>"  
                     SelectCommand="SELECT [PatientId],[firstname], [lastname], [sex] FROM [PatientDetails]">
  </asp:SqlDataSource>

Now I put the session on the checkbox change event as below

 protected void chk_CheckedChanged(object sender, EventArgs e)
 {
         GridViewRow row = ((GridViewRow)((Control)sender).Parent.Parent);
        string requestid =  gvDoctorList.DataKeys[row.RowIndex].Value.ToString();
        string cellvalue = row.Cells[1].Text;
        Session["pId"] = cellvalue;   
             }

on the another page i want to get the value of session so i did something on another page as below

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pId"] != null)
        {
            lblsession.Text = Session["pId"].ToString();
        }
        else 
        { 

        }
    }

But the problem is that when i debug the code on checkbox change event particularly on session line it show me error that Object reference not set to an instance of an object.

I don't understand what wrong?

What i want to do is that take the value of patientid from gridview for which the user has check on checkbox....send the selected value to another page and want to insert the patientid into the sql table.

Please supply any example with coding or another good ideas.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Pratik
  • 171
  • 1
  • 5
  • 19
  • BTW `chk_CheckedChanged` event is firing or not ? – Suraj Singh Oct 08 '13 at 12:33
  • yes chk_CheckedChanged event is firing.... – Pratik Oct 08 '13 at 12:46
  • Possible duplicate: http://stackoverflow.com/questions/11694835/get-item-from-gridviewrow-asp-net – Francis Oct 08 '13 at 13:01
  • not duplicate problem....but their is no value in the session.....below you can see the code – Pratik Oct 08 '13 at 13:07
  • protected void chk_CheckedChanged(object sender, EventArgs e) { GridViewRow row = ((GridViewRow)((Control)sender).Parent.Parent); string cellvalue = row.Cells[1].Text; Session["pId"] = cellvalue; – Pratik Oct 08 '13 at 13:08
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 13 '13 at 02:40

3 Answers3

0

Aren't you setting value for key "pId" and trying to access some other key "patientid"? Use the same key to set and get the value from Session.

AbhinavRanjan
  • 1,638
  • 17
  • 21
0
 protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        GridViewRow Row = ((GridViewRow)((Control)sender).Parent.Parent);
       // string requestid = grdrequestlist.DataKeys[Row.RowIndex].Value.ToString();
        string cellvalue=Row.Cells[1].Text;
        Session["pId"] =cellvalue;    

    }

ON Page_Load event

protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["patientid"] != null) you have defined pID as session variable 
        // so check for pID not patientid
       string patientID =Convert.Tostring(Session["pId"]);
         if(!string.IsNullOrEmpty(patientID ))
        {
            lblsession.Text = patientID;
                  //Session["pId"].ToString();
        }
        else 
        { 

        }
    }

UPDATE

Sorry i missed some part of your question, as your gridview, you have used BoundField so you can fetch cell value as row.Cells[index].text

string PatientID= row.Cells[0].Text;
Session ["pID"] = PatientID;

-- Hope it helps

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
  • thx...a lot i have try ur code....but i am getting error : Index was out of range. Must be non-negative and less than the size of the collection on line string requestid = gvDoctorList.DataKeys[row.RowIndex].Value.ToString();............my code is as follows..... – Pratik Oct 08 '13 at 12:39
  • protected void chk_CheckedChanged(object sender, EventArgs e) { GridViewRow row = ((GridViewRow)((Control)sender).Parent.Parent); string requestid = gvDoctorList.DataKeys[row.RowIndex].Value.ToString(); string cellvalue = row.Cells[1].Text; Session["pId"] = cellvalue; } – Pratik Oct 08 '13 at 12:40
  • i have to remove the line string requestid = gvDoctorList.DataKeys[row.RowIndex].Value.ToString(); – Pratik Oct 08 '13 at 12:50
  • i have check the value of session are empty....what wrong i don't understand....there is no error...but still the session doesnot have the value? – Pratik Oct 08 '13 at 13:30
  • Well one check first, when you assign session you sure it contains required value, I mean you are getting `PatientID` value ? – Suraj Singh Oct 08 '13 at 13:34
  • no I am not getting the PatientID value in the session where i am putting the value...can you please let me know what wrong it's urgent – Pratik Oct 08 '13 at 14:56
  • That means you are not getting patientID anywhere ?? – Suraj Singh Oct 08 '13 at 14:58
  • `string cellvalue = row.Cells[1].Text;` You are getting `cellvalue` value ? – Suraj Singh Oct 08 '13 at 14:59
  • yes i try with cells[1] but still when i debug the session in another page the value shows only ""......but session is not null....bec. it goes inside the if condition – Pratik Oct 09 '13 at 06:17
  • @Pratik You are checking for `NULL` only you have to check for `empty` also, "" is not null but it's empty so your if condition will not work, Try `string patientID=Convert.Tostring(Session["pId"]);` then check for null or empty `if(!string.IsnullOrEmpty(patientID)) { // goes your code }` Check updated **If** condition in answer . – Suraj Singh Oct 09 '13 at 07:18
  • it try with ur updated code....yes session value is coming empty...so what should i do now...where is wrong? – Pratik Oct 09 '13 at 07:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38847/discussion-between-suraj-singh-and-pratik) – Suraj Singh Oct 09 '13 at 07:41
0

I don't know how your checkbox could be firing, there is no AutoPostback="True". Also, Datasource is lost every postback. You can store the Id in a hidden field or in a value property of the checkbox to retrieve it in your event.

EDIT: see these links: http://www.daveparslow.com/2007/08/assigning-value-to-aspnet-checkbox.html Why I can't set a value on a asp:CheckBox?

Community
  • 1
  • 1
Francis
  • 367
  • 1
  • 10
  • no i have put the AutoPostBack="true" in the checkbox...but still there is not value in string cid = rows.Cells[1].Text;.....what wrong? – Pratik Oct 08 '13 at 13:16