views:

190

answers:

1

i am trying to send the value id inorder to connect 2 pages. the search page with the comments page.

this is the error i get Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code

Exception Details:System.NullReferenceException :Object reference not set to an instance of an object

Source Error:

Line 101:
Line 102:        ListComment.DataSource = comm;
Line 103:        ListComment.DataBind();
Line 104:
Line 105:    }

here is my code for the comments page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Comment.aspx.cs" Inherits="Comment"  Debug="true"%>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile"%>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <mobile:Form id="Form1" runat="server" BackColor="#fcf9f3" Paginate="True">

        <mobile:Label ID="CommentError" Runat="server" ForeColor="red" 
            Font-Size="Small" Font-Bold="True" Visible="false" />

        <mobile:Label ID="Label2" Runat="server" ForeColor="#d3af63" Font-Italic="True" 
            Font-Name="Monotype Covasia" Font-Bold="True" text="Write Comments" />
        <mobile:Label ID="lblCommentTitle" Runat="server" text="Title" />
        <mobile:TextBox ID="txtCommentTitle" Runat="server" text="" />
        <br />

        <mobile:Label ID="lblCommentText" Runat="server" text="Comment" />
        <mobile:TextBox ID="txtCommentText" Runat="server" text=""/>

        <br />
        <mobile:Command ID="submit" Runat="server" text="Submit" OnClick="submitform_Click" />
        <br />

        <mobile:Label ID="Label1" Runat="server" ForeColor="#d3af63" Font-Italic="True" 
            Font-Name="Monotype Covasia" Font-Bold="True" text="Read Comments"/>
        <mobile:list runat="server" id="ListComment" DataTextfield="comment_text"
             DataValueField="comment_text" Font-Size="Small" Font-Italic="True" 
             ItemsPerPage="5" Wrapping="Wrap"  BreakAfter="True"/>

    </mobile:Form>
</body>
</html>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Data.Linq;
using System.Xml.Linq;
using System.Linq;

public partial class Comment : System.Web.UI.MobileControls.MobilePage
{
    CommentDataContext Comments = new CommentDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["error_msg"] != null)
        {
            CommentError.Text = Session["error_msg"].ToString();
            Session["error_msg"] = null;
            CommentError.Visible = true;
        }

        if (Session["r_id"] != null)
        {
            String r_id = Session["r_id"].ToString();

        }       

        GetComment();
    }

    protected void submitform_Click(object sender, EventArgs e)
    {
        if (Page.IsValid) 
        {
            AddComment();
        }
    }


    #region Inserting a comment
    protected void AddComment()
    {
        mobile_comment mobilecon = new mobile_comment();
        mobilecon.comment_text = txtCommentText.Text;
        mobilecon.comment_title = txtCommentTitle.Text;

        Comments.mobile_comments.InsertOnSubmit(mobilecon);
        Comments.SubmitChanges();

        Session["error_msg"] = "Your comment has been added";
        Response.Redirect("Comment.aspx");
    }
    #endregion


    protected void GetComment()
    {
        var comm = from c in Comments.mobile_comments
                   where c.r_id == Int32.Parse(Session["r_id"].ToString())
                   //where c.r_id == Int32.Parse(Request["r_id"].ToString())
                   select c;

        ListComment.DataSource = comm;
        ListComment.DataBind();

    }
}

am i using this statement correctly where c.r_id == Int32.Parse(Session["r_id"].ToString())

if not then how do i link d ids?

A: 

I am not sure if this is your problem, but this code ...

if (Session["r_id"] != null)        
{            
  String r_id = Session["r_id"].ToString(); 
}

Does nothing because you declare r_id in the scope of the if block and then do nothing with it. So, you have not successfully insured that Session["r_id"] is not null when you iterate over it with your LINQ statement. Did you mean to pass r_id to GetComment for use to compare to c.r_id instead of going back to the session?

JP Alioto