0

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

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

    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();
        }
    }
4

6 回答 6

2

int.parse无法处理 null 和,

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

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

要么检查适当的值,Request.QueryString["id"].ToString()要么在任何情况下它都会为空,然后使用convert.toInt()处理空值的函数。

于 2013-04-12T12:00:40.993 回答
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();
        }
    }
于 2013-04-12T12:02:07.567 回答
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();
        }
    }
于 2013-04-12T12:07:14.930 回答
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();
        }
 }

希望这有帮助。

于 2013-04-12T12:05:19.703 回答
0
 GetUserScraps(Int32.Parse(Request.QueryString["id"] ?? "0"));
于 2013-04-12T12:09:39.237 回答
0

使用Convert.ToString()而不是.ToString()处理空值,并确保将id查询字符串传递到此页面。

string strID=Convert.ToString(Request.QueryString["id"]);         
int intID;
if (int.TryParse(strID, out intID))
  { 
    GetUserScraps(intID);
  } 
于 2013-04-12T12:01:17.747 回答