public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string SQL = "select c.*,d.fName AS fContractType from tContractGoods a,tContract c,(select * from tContractType) d where a.fID=c.fID and c.fContractType = d.fType";
                GridView1.DataSource = Binding(SQL);
                GridView1.DataKeyNames = new string[] { "c.fID" };
                GridView1.DataBind();
            }
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string OrderID = Convert.ToString(GridView1.SelectedValue);
            string SQL = "select a.*,c.* from tContractGoods a ,tContract c where a.fGoodsType=2 and a.fID='" + OrderID + "'";
            GridView2.DataSource = Binding(SQL);
            GridView2.DataBind();
        }        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {        }
        /**/
        /// <summary>
        /// 执行SQL语句返回一个数据表
        /// </summary>
        /// <param name="SQL">所要执行的SQL语句</param>
        /// <returns>DataTable</returns>
        protected DataTable Binding(string SQL)
        {
           
SqlConnection myConn = new               SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString);
            DataTable dt = new DataTable();
            SqlDataAdapter myAdapter = new SqlDataAdapter(SQL, myConn);
            myAdapter.Fill(dt);
            return dt;        }出现如下问题;用户代码未处理 System.NullReferenceException
  Message="未将对象引用设置到对象的实例。"
  Source="hager"
  StackTrace:
       在 hager.WebForm2.Binding(String SQL) 位置 C:\Documents and Settings\develop\My Documents\Visual Studio 2008\Projects\hager\hager\WebForm2.aspx.cs:行号 47
       在 hager.WebForm2.Page_Load(Object sender, EventArgs e) 位置 C:\Documents and Settings\develop\My Documents\Visual Studio 2008\Projects\hager\hager\WebForm2.aspx.cs:行号 21
       在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       在 System.Web.UI.Control.OnLoad(EventArgs e)
       在 System.Web.UI.Control.LoadRecursive()
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 请问是什么问题该如何解决?

解决方案 »

  1.   

    估计问题应该出在连接字符串上,可以断点跟一下,看是否正常获取
    SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString);
    =》SqlConnection myConn = new SqlConnection(Configuration.ConfigurationManager.AppSettings["AppConnectionString1"]);
      

  2.   

     不行啊 
    我想知道这句是什么意思 SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString);
      

  3.   


    这句话的意思 是从webconfig 文件里的一个<connectionStrings> 节点里 命名为AppConnectionString1 的数据库连接字符串。为了sqlconnction 去实例化一个连接对象
      

  4.   

    [Quote=引用 3 楼  的回复:]
    这句话的意思 是从webconfig 文件里的一个<connectionStrings> 节点里 命名为AppConnectionString1 的数据库连接字符串。为了sqlconnction 去实例化一个连接对象谢谢这位大虾,解释得很清楚,问题解决了,我那段代码是网上找到,因为是新手有些看不明白,现在把数据库的连接字符串改成我这里的就可以了,真心感谢。
      

  5.   

    [Quote=引用 5 楼  的回复:]给分啊,。呵呵再问你个问题行不? 程序没错了,但是为什么只是主表有数据,从表没有啊?
    string OrderID = Convert.ToString(GridView1.SelectedValue); 这句是什么意思?
      

  6.   

    string orderid ------定义一个字符串变量
    convert.tostring ------强制类型转成字符串
    gridview.SelectedValue ------获取GridView 控件中选中行的数据键值意思就是定义一个字符串变量。让它指向GridView 控件中选中行的数据键值