就是從兩個textbox獲取兩個日期,再在表格中列出這兩個日期中的值,下面的哪裏出錯了?    protected void Check_Click(object sender, EventArgs e)
    {
        
        try
        {
            DateTime from = DateTime.Parse(txtfrom.Text);
            DateTime to = DateTime.Parse(txtto.Text);//獲取textbox的值
                        
            warn.Visible = false;
            SqlConnection conn = new SqlConnection(
                    @"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=Mydb");
            conn.Open();
            DataSet ds = new DataSet();
            
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            
            cmd.Connection = conn;
            conn.Open();            cmd.CommandText = "OrdersbyDate";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Beginning_Date", SqlDbType.DateTime).Value = from;
            cmd.Parameters.Add("@Ending_Date", SqlDbType.DateTime).Value = to;
           
                                 
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
                    }
      
      catch (Exception a)
     {
       warn.Text = "Invalid Date  e.g. 200X-X-X";
       warn.Visible = true;
     } 
    
        
    }

解决方案 »

  1.   

    SqlDataAdapter da = new SqlDataAdapter(); 和cmd有关联么?
      

  2.   

    首先,楼主的代码不够精简
    DataReader在使用过程中不需要手动打开连接源其次,楼主使用将sql语句赋给command对象,但是却使用DataAdapter.Fill方法填充数据,可见楼主应该是初学者DataAdapter要想填充数据,必须将sql语句赋给DataAdapter才行,否则DataAdapter不包含任何可执行命令,怎么可能能够获得数据?
     try 
            { 
                DateTime from = DateTime.Parse(txtfrom.Text); 
                DateTime to = DateTime.Parse(txtto.Text);//獲取textbox的值 
                            
                warn.Visible = false; 
                SqlConnection conn = new SqlConnection( 
                        @"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=Mydb"); 
                conn.Open(); 
                DataSet ds = new DataSet(); 
                
                SqlCommand cmd = new SqlCommand(); 
                SqlDataAdapter da = new SqlDataAdapter(cmd); //这里传进command对象,忘了这个是不是一个参数了
                
                cmd.Connection = conn; 
                //conn.Open(); 不需要手动打开数据源            cmd.CommandText = "OrdersbyDate"; 
                cmd.CommandType = CommandType.StoredProcedure; 
                cmd.Parameters.Add("@Beginning_Date", SqlDbType.DateTime).Value = from; 
                cmd.Parameters.Add("@Ending_Date", SqlDbType.DateTime).Value = to; 
              
                                    
                da.Fill(ds); 
                GridView1.DataSource = ds; 
                GridView1.DataBind(); 
                        } 
          
          catch (Exception a) 
        { 
          warn.Text = "Invalid Date  e.g. 200X-X-X"; 
          warn.Visible = true; 
        } 
      

  3.   

    哦,謝謝樓上2位,我確實是個初學者^.^
    但是,我按照您的做了修改,輸入兩個參數之後,卻出現了“无法显示网页,错误代码:500 ”
    會不會是因爲我是xphome,沒有iis的原因嘛?
      

  4.   

    哦,謝謝樓上2位,我確實是個初學者^.^
    但是,我按照您的做了修改,輸入兩個參數之後,卻出現了“无法显示网页,错误代码:500 ”
    會不會是因爲我是xphome,沒有iis的原因嘛?
      

  5.   

    public static DataSet SelectSqlRows(string connectionString,
        string queryString, string tableName)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(queryString, connection);
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);        connection.Open();        DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, tableName);        //code to modify data in DataSet here        builder.GetUpdateCommand();        //Without the SqlCommandBuilder this line would fail
            adapter.Update(dataSet, tableName);        return dataSet;
        }
    }SqlDataAdapter 不会自动生成实现 DataSet 的更改与关联的 SQL Server 实例之间的协调所需的 Transact-SQL 语句。但是,如果设置了 SqlDataAdapter 的 SelectCommand 属性,则可以创建一个 SqlCommandBuilder 对象来自动生成用于单表更新的 Transact-SQL 语句。然后,SqlCommandBuilder 将生成其他任何未设置的 Transact-SQL 语句。