痛苦几天的一段代码:真的不知道原因出在什么地方,请各路高手赐教!
我在GridView里面添加一个hyperlink,点击hyperlink要求显现详细信息
显示错误的信息:string sqlstr = "select USERID,UserName,UserPwd,RealName,Sex from customers where USERID='" + Request["USERID"].ToString() + "'"
我不知道这错在什么地方了调试时的错误信息:
使用“new”关键字创建对象实例
在调用方法前通过检查确定对象是否为空
获取此异常的常规帮助

以下是我的写的代码:GridView页面内的代码:Default1.aspx.csusing System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;DataBase=martin");
            con.Open();
            SqlCommand com = new SqlCommand("select USERID,UserName,UserPwd,RealName,Sex from customers",con);
            com.ExecuteNonQuery();
            SqlDataAdapter ada = new SqlDataAdapter("select * from customers", con);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            con.Close();
           
        }    }
}点击hyperlink显示详细信息的页面的代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection("server=(Local);user id=sa;pwd=;DataBase=martin");
            con.Open();
            string sqlstr = "select USERID,UserName,UserPwd,RealName,Sex from customers where USERID='" + Request["USERID"].ToString() + "'";
            SqlDataAdapter myAapter = new SqlDataAdapter(sqlstr, con);
            DataSet myDS = new DataSet();
            myAapter.Fill(myDS,"customers");
            DataRowView rowView = myDS.Tables["customers"].DefaultView[0];
            TextBox1.Text = Convert.ToString(rowView["USERID"]);
            TextBox2.Text = Convert.ToString(rowView["UserName"]);
            TextBox3.Text = Convert.ToString(rowView["UserPwd"]);
            TextBox4.Text = Convert.ToString(rowView["RealName"]);
            TextBox5.Text = Convert.ToString(rowView["Sex"]);
            con.Close();            }      }  
}错误提示:未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 20:             SqlConnection con = new SqlConnection("server=(Local);user id=sa;pwd=;DataBase=martin");
行 21:             con.Open();
行 22:             string sqlstr = "select USERID,UserName,UserPwd,RealName,Sex from customers where USERID='" + Request["USERID"].ToString() + "'";
行 23:             SqlDataAdapter myAapter = new SqlDataAdapter(sqlstr, con);
行 24:             DataSet myDS = new DataSet();
 源文件: e:\martiin\codeItem\WebSite7\Default2.aspx.cs    行: 22 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   Default2.Page_Load(Object sender, EventArgs e) in e:\martiin\codeItem\WebSite7\Default2.aspx.cs:22
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68
   System.Web.UI.Control.OnLoad(EventArgs e) +88
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3035
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版
本:2.0.50727.42 

解决方案 »

  1.   

    估计是错在这:Request["USERID"].ToString() 
    可能USERID没有定义,所以取不到值.
      

  2.   

    string sqlstr = "select USERID,UserName,UserPwd,RealName,Sex from customers where USERID='1'"; 直接写看看先把,应该是USERID没有值递交过来
      

  3.   

    if (!IsPostBack)
      首次加载Request["USERID"].ToString() 为NULL 
    跟踪下吧
      

  4.   

    做一个判断吧 
    if(Request["USERID"]!=null)
    {
      
    }
    else
    {
       respone.end();
    }
    这样看看是否对了
      

  5.   

    楼上正解.你先判断Request["USERID"]是不是空.不是空才能tostring().
      

  6.   

    if(string.isempty....(Request["USERID"].ToString()))
    {
    string sqlstr = "select USERID,UserName,UserPwd,RealName,Sex from customers where USERID="+Request["USERID"].ToString();
    }
    哈,第一行的具体名称忘记了,就是判断空字符串的那个
      

  7.   

    GridView页面内hyperlink没有传值给参数USERID
      

  8.   

    USERID你也没传值就Request["USERID"]当然没有了
      

  9.   

    没有传USERID过来所以Request["USERID"]为null
      

  10.   

    在你点击hyperlink这的URL上加"xxx.aspx?USERID='“+ds.table[0].rows[0]["USERID"].ToString()+"'"
      

  11.   

    先用 
    if(!string.IsNullOrEmpty(Request["USERID"]))
    {
        //处理你的代码
          SqlConnection con = new SqlConnection("server=(Local);user id=sa;pwd=;DataBase=martin");
                con.Open();
                string sqlstr = "select USERID,UserName,UserPwd,RealName,Sex from customers where USERID='" +    Request["USERID"].ToString() + "'";
                SqlDataAdapter myAapter = new SqlDataAdapter(sqlstr, con);
                DataSet myDS = new DataSet();
                myAapter.Fill(myDS,"customers");
                DataRowView rowView = myDS.Tables["customers"].DefaultView[0];
                TextBox1.Text = Convert.ToString(rowView["USERID"]);
                TextBox2.Text = Convert.ToString(rowView["UserName"]);
                TextBox3.Text = Convert.ToString(rowView["UserPwd"]);
                TextBox4.Text = Convert.ToString(rowView["RealName"]);
                TextBox5.Text = Convert.ToString(rowView["Sex"]);
                con.Close(); 
    }
    还是先检查一下你的hypelink有没有传值吧..
      

  12.   

    SqlCommand com = new SqlCommand("select USERID,UserName,UserPwd,RealName,Sex from customers",con); 
                com.ExecuteNonQuery(); //这句有用么?代码冗余
    Request.QueryString["USERID"].ToString();
    用的时候先判断
      

  13.   

    肯定是userid值没有传递过来
    再就是不推荐你这种sql语句拼接写法,而且userid如果不是字符类型,没必要加单引号
      

  14.   

    1、楼主的Request["USERID"]是获取什么值,这种是Post方法,USERID应该是本页控件ID/变量。2、get方法:你如果是获取传参,USERID是别的页面的变量,
    a.aspx:如:"aspect.aspx?USERID="+userid ; //userid是a.aspx中的一个变量
                                                    //如string userid=txtUserName.text;控件txtUserName的值。
    b.aspx: 接收  string ss=Request.QueryString["USERID"].Tostring(); //USERID为a.aspx的aspect.aspx?USERID中的USERID。