你调用的对象是空的

时间:2013-04-12 11:58:38

标签: c# asp.net

出于任何原因,遇到了一个错误,即对象引用没有设置为对象的实例..

这是我的代码plz帮我解决我的问题 提前谢谢..

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetUserScraps(int.Parse(Request.QueryString["id"].ToString()));// this line have such error..

        }
    }

    public void GetUserScraps(int id)
    {
        string getUserScraps = "SELECT r.uid ,r.unm,p.photo,s.uid,s.unm,s.text,s.date,s.sid FROM register as r, status as s, profile as p WHERE p.pid.s.sid AND r.email=p.email AND s.id='" + Request.QueryString["id"].ToString() + "'";
        dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
        if (dt.Rows.Count > 0)
        {
            GridViewUserScraps.DataSource = dt;
            GridViewUserScraps.DataBind();
        }
    }

6 个答案:

答案 0 :(得分:2)

int.parse无法处理null和

您的Request.QueryString["id"].ToString()的值为空。

这就是为什么有例外:object refrence not set to an insance of an object

使用Request.QueryString["id"].ToString()的适当值进行检查,或者在任何情况下它是否为null,然后使用处理空值的convert.toInt()函数。

答案 1 :(得分:1)

Request.QueryString [“id”]。ToString()具有空值。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
          if(!string.IsNullOrEmpty(Request.QueryString["id"]))
             {
                GetUserScraps(int.Parse(Request.QueryString["id"].ToString()));
             }
        }
     }
    //Here u use id not querystring
    public void GetUserScraps(int id)
    {
        string getUserScraps = "SELECT r.uid ,r.unm,p.photo,s.uid,s.unm,s.text,s.date,s.sid FROM register as r, status as s, profile as p WHERE p.pid.s.sid AND r.email=p.email AND s.id='" + id + "'";
        dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
        if (dt.Rows.Count > 0)
        {
            GridViewUserScraps.DataSource = dt;
            GridViewUserScraps.DataBind();
        }
    }

答案 2 :(得分:0)

使用Convert.ToString()代替.ToString()来处理空值,并确保将id查询字符串传递给此页面。

string strID=Convert.ToString(Request.QueryString["id"]);         
int intID;
if (int.TryParse(strID, out intID))
  { 
    GetUserScraps(intID);
  } 

答案 3 :(得分:0)

使用此:

protected void Page_Load(object sender, EventArgs e)
{
        if (!Page.IsPostBack)
        {
          if (Request.QueryString.Count > 0 && Request.QueryString["id"] != null)
          {
            GetUserScraps(int.Parse(Request.QueryString["id"].ToString()));// this line have such error..
          }
        }
    }

    public void GetUserScraps(int id)
    {
        string getUserScraps = "SELECT r.uid ,r.unm,p.photo,s.uid,s.unm,s.text,s.date,s.sid FROM register as r, status as s, profile as p WHERE p.pid.s.sid AND r.email=p.email AND s.id='" + Request.QueryString["id"].ToString() + "'";
        dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
        if (dt.Rows.Count > 0)
        {
            GridViewUserScraps.DataSource = dt;
            GridViewUserScraps.DataBind();
        }
 }

希望得到这个帮助。

答案 4 :(得分:0)

只需更改您的代码,如:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetUserScraps(Convert.ToInt32(Request.QueryString["id"]));    
        }
    }

    public void GetUserScraps(int id)
    {
        string getUserScraps = "SELECT r.uid ,r.unm,p.photo,s.uid,s.unm,s.text,s.date,s.sid FROM register as r, status as s, profile as p WHERE p.pid.s.sid AND r.email=p.email AND s.id='" + id + "'";
        dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
        if (dt.Rows.Count > 0)
        {
            GridViewUserScraps.DataSource = dt;
            GridViewUserScraps.DataBind();
        }
    }

答案 5 :(得分:0)

 GetUserScraps(Int32.Parse(Request.QueryString["id"] ?? "0"));