2009-05-17 12 views
0

2 페이지를 연결하기 위해 값 id를 보내려고합니다. 검색 페이지에 설명 페이지가 있습니다. 여기 asp.net mobile 2.0

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: } 

는 코멘트 페이지 내 코드입니다 :

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

    } 
} 

내가 제대로 을이 문을 사용하고 어디에 c.r_id ==에서 Int32.Parse

이 오류가 내가 얻을 수있다 (세션 [ "r_id"]. ToString())

그렇다면 ID를 어떻게 연결합니까? 이 방법으로 문제가있는 경우

답변

0

나는 잘 모르겠지만, 당신이 경우 블록의 범위에 r_id를 선언하고 그것으로 아무것도 할 수 없기 때문에이 코드 ...

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

는 아무것도 실행하지 않습니다. 따라서 세션 [ "r_id"]이 LINQ 문으로 반복 할 때 null이 아닌지 성공적으로 확인하지 않았습니다. 세션으로 돌아가는 대신 c.r_id와 비교하기 위해 GetComment에 r_id를 전달하겠습니까?

0

먼저하더라도 mySite/Comments.aspx? r_id = 18 다음 사용해야 요청 [ "r_id"같은 URL에 웹 양식에서 매개 변수를 전달하면

... 
if (Session["r_id"] != null) 
    { 
     String r_id = Session["r_id"].ToString(); 
     int id = int.Parse(r_id); 
     GetComment(id); 
    } 
... 
    protected void GetComment(int id) 
{ 
    var comm = from c in Comments.mobile_comments 
       where c.r_id == id 
       select c; 

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

} 

에 GetComment 메소드를 호출으로 이동하려고 ] 개체를 만듭니다. About Request object, MSDN 하지만 제출 또는 기타 사용자 작업 세션 [ "r_id"] = 18로 설정하면 Session [ "r_id"]을 사용하여 값을 가져와야합니다. About Session.