如题。
代码我是分两部分写的
先我是输出表Yq中的前五条记录  代码为
string SqlStr = "";
        SqlStr = "SELECT Top 5 * FROM Yy";
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;       //取出连接字符串
        DataSet ds = new DataSet();                             //创建DataSet数据集
        SqlConnection conn = new SqlConnection(connStr);        //创建连接对象
        try
        {
            if (conn.State.ToString() == "Closed")              //如果连接没有打开,打开连接
                conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
            da.Fill(ds);                                        //为DataSet数据集填充数据            GridView1.DataSource = ds.Tables["0"].DefaultView;    //为GridView指名数据源
            GridView1.DataBind();然后我想加上用..代替GirdView中yyYq列的超长字符,就在代码中插入了  如下代码
 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                DataRowView mydrv;
                string gIntro;
                if (GridView1.PageIndex == 0)
                {
                    mydrv = ds.Tables["Yq"].DefaultView[i];//表名
                    gIntro = Convert.ToString(mydrv["yyYq"]);//所要处理的字段
                    GridView1.Rows[i].Cells[0].Text = SubStr(gIntro, 2);
                }
                else
                {
                    mydrv = ds.Tables["Yq"].DefaultView[i + (5 * GridView1.PageIndex)];
                    gIntro = Convert.ToString(mydrv["yyYq"]);
                    GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
                }
            }运行后出现  System.NullReferenceException: 未将对象引用设置到对象的实例。
错误  错误处为 mydrv = ds.Tables["0"].DefaultView[i];//表名全部代码为
 protected void Page_Load(object sender, EventArgs e)
    {
        string SqlStr = "";
        SqlStr = "SELECT Top 5 * FROM Yy";
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;       //取出连接字符串
        DataSet ds = new DataSet();                             //创建DataSet数据集
        SqlConnection conn = new SqlConnection(connStr);        //创建连接对象
        try
        {
            if (conn.State.ToString() == "Closed")              //如果连接没有打开,打开连接
                conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
            da.Fill(ds);                                        //为DataSet数据集填充数据            GridView1.DataSource = ds.Tables["0"].DefaultView;    //为GridView指名数据源
            GridView1.DataBind();
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                DataRowView mydrv;
                string gIntro;
                if (GridView1.PageIndex == 0)
                {
                    mydrv = ds.Tables["0"].DefaultView[i];//表名
                    gIntro = Convert.ToString(mydrv["yyYq"]);//所要处理的字段
                    GridView1.Rows[i].Cells[0].Text = SubStr(gIntro, 2);
                }
                else
                {
                    mydrv = ds.Tables["0"].DefaultView[i + (5 * GridView1.PageIndex)];
                    gIntro = Convert.ToString(mydrv["yyYq"]);
                    GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
                }
            }
        }
        finally
        {
            if (conn.State.ToString() == "Open")                //操作完,如果连接处于打开,则关闭连接
                conn.Close();
        }
    }
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }求大家帮我解决这个问题,具体说说哪里错了,该怎么改!
谢谢.............

解决方案 »

  1.   

     da.Fill(ds,"tablename");   
     mydrv = ds.Tables["tablename"].DefaultView[i];//表名 
    或者改为 mydrv = ds.Tables[0].DefaultView[i];//表名 
                                       
      

  2.   

    我把ds.Tables["0"].DefaultView
    改为ds.Tables["Yy"].DefaultView
    提示错误一样  错误在GridView1.DataSource = ds.Tables["0"].DefaultView;    //为GridView指名数据源   行
      

  3.   

    另外提醒你一句,在使用SqlDataAdapter 对象时,数据连接会自动打开关闭!不需要手动去操作。
      

  4.   


    请注意一点,DataSet 对象的Tables 为属性,当输入ds.Tables[0]这里是指找ds 里的第一个表。 
    而输入ds.Tables["0"] 是指找ds里的表名为 0 这个表。 
    注意区分。