2014-02-09 50 views
0

在我的asp頁面我已經使用了兩個gridview。並在頁面加載事件中將數據綁定到這些gridview。但它表明"System.NullReferenceException: Object reference not set to an instance of an object.": 並得到例外,在這條線未將對象引用設置爲對象的實例。在手風琴控制

GridReturned.DataSource = ds2; 

這裏是我的代碼

<asp:Accordion ID="Accordion1" runat="server" SelectedIndex="0" AutoSize="None" 
      FadeTransitions="true" TransitionDuration="300" FramesPerSecond="25"> 
      <Panes> 
       <asp:AccordionPane ID="AccordionPane1" runat="server"> 
        <Header> 
         <br /> 
         <div class="headPane"> 
          Issued History 
         </div> 
        </Header> 
        <Content> 
         <div class="bodyPane"> 
          <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
           <ContentTemplate> 
            <asp:GridView ID="GridIssue" runat="server" CssClass="grid" AllowPaging="true" PageSize="8" OnPageIndexChanging="GridIssue_PageIndexChanging"> 
             <EmptyDataTemplate> 
              <p style="margin-left: 10px;">No Book Has Been Issued Yet.</p> 
             </EmptyDataTemplate> 
            </asp:GridView> 

           </ContentTemplate> 
          </asp:UpdatePanel> 
         </div> 
        </Content> 
       </asp:AccordionPane> 
       <asp:AccordionPane runat="server" ID="acd2"> 
        <Header> 
         <div class="headPane"> 
          Restore Books 
         </div> 
        </Header> 
        <Content> 
         <div class="bodyPane"> 
          <asp:GridView ID="GridReturned" runat="server" OnSelectedIndexChanged="GridReturned_SelectedIndexChanged"> 
           <EmptyDataTemplate> 
            <p>Books Are Not Required To Return At Present.</p> 
           </EmptyDataTemplate> 
           <Columns> 
            <asp:CommandField ButtonType="Button" SelectText="Return" ShowSelectButton="true" /> 
           </Columns> 
          </asp:GridView> 
         </div> 
        </Content> 
        <Content> 
        </Content> 
       </asp:AccordionPane> 
      </Panes> 
     </asp:Accordion> 

和後面的代碼是遵循

string str = ConfigurationManager.ConnectionStrings["connect"].ConnectionString; 
    SqlConnection con; 
    SqlCommand cmd; 
    string getQuery; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     getQuery = "select Issue_ID as 'Issue Id', IssueDate as 'Issue Date', ReturnDate as 'Return Date' , UserName as 'User Name' , BookName as 'Book Name', Category ,ActualReturnDate from IssueRecords"; 
     getData(getQuery,0); 
     getQuery = "select Issue_ID,BID,UserName,BookName,IssueDate,ReturnDate from IssueRecords where ActualReturnDate is null"; 
     getData(getQuery, 1); 
    } 
    public void getData(string query, int i) 
    { 
     con = new SqlConnection(str); 

     cmd = new SqlCommand(query, con); 
     if (i == 0) 
     { 
      con.Open(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      GridIssue.DataSource = ds; 
      GridIssue.DataBind(); 
      con.Close(); 
     } 
     else 
     { 
      con.Open(); 
      SqlDataAdapter da2 = new SqlDataAdapter(cmd); 
      DataSet ds2 = new DataSet(); 
      da2.Fill(ds2); 
      GridReturned.DataSource = ds2; 
      GridReturned.DataBind(); 
      con.Close(); 
     } 

    } 

沒有任何事件行改變頁面手風琴指數,以便我可以改變事件發生這些事件。

+0

你有沒有嘗試在'con.Open()'上放置一個斷點並逐步執行代碼? –

+0

我已經嘗試過,但無法理解要做什麼 –

回答

0
<div id="content"> 
     <asp:Accordion ID="Accordion1" runat="server" SelectedIndex="0" AutoSize="None" 
      FadeTransitions="true" TransitionDuration="300" FramesPerSecond="25"> 
      <Panes> 
       <asp:AccordionPane ID="AccordionPane1" runat="server"> 
        <Header> 
         <br /> 
         <div class="headPane"> 
          Issued History 
         </div> 
        </Header> 
        <Content> 
         <div class="bodyPane"> 
          <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
           <ContentTemplate> 
            <asp:GridView ID="GridIssue" runat="server" CssClass="grid" AllowPaging="true" PageSize="8" OnPageIndexChanging="GridIssue_PageIndexChanging"> 
             <EmptyDataTemplate> 
              <p style="margin-left: 10px;">No Book Has Been Issued Yet.</p> 
             </EmptyDataTemplate> 
            </asp:GridView> 

           </ContentTemplate> 
          </asp:UpdatePanel> 
         </div> 
        </Content> 
       </asp:AccordionPane> 
       <asp:AccordionPane runat="server" ID="acd2"> 
        <Header> 
         <div class="headPane"> 
          Restore Books 
         </div> 
        </Header> 
        <Content> 
         <div class="bodyPane"> 
          <asp:GridView ID="GridReturned" DataSourceID="sdSource" runat="server" OnSelectedIndexChanged="GridReturned_SelectedIndexChanged"> 
           <EmptyDataTemplate> 
            <p>Books Are Not Required To Return At Present.</p> 
           </EmptyDataTemplate> 
           <Columns> 
            <asp:CommandField ButtonType="Button" SelectText="Return" ShowSelectButton="true" /> 
           </Columns> 
          </asp:GridView> 
          <asp:SqlDataSource ID="sdSource" runat="server" SelectCommand="select Issue_ID,BID,UserName,BookName,IssueDate,ReturnDate from IssueRecords where ActualReturnDate is null" ConnectionString="<%$ConnectionStrings:connect %>" ></asp:SqlDataSource> 
         </div> 
        </Content> 

       </asp:AccordionPane> 
      </Panes> 
     </asp:Accordion> 
相關問題