2013-10-29 45 views
0

我想創建一個RDLC報告,但同時我想執行程序錯誤,而試圖執行C#程序

Error: ""=Items collection cannot be modified when the DataSource property is set."

Private void btnGenerate_Click(object sender, EventArgs e) 
{ 
     string connectionstring = "MultipleActiveResultSets=True;Data Source=ECSTSRD;Initial Catalog=SSWSQL;User ID=sswuser;Password=sswuser123"; 
     SqlConnection myconnection = new SqlConnection(connectionstring); 

    myconnection.Open(); 

    string sql = "SELECT customer, imp_license_no, psq_level FROM customer WHERE customer= @cust1"; 

    SqlCommand custcom = new SqlCommand(sql, myconnection); 

    custcom.Parameters.AddWithValue("@cust1", cboFrom.SelectedValue.ToString()); 

    SqlDataAdapter da = new SqlDataAdapter(custcom); 
    DataSet1 ds = new DataSet1(); 
    da.Fill(ds, "whbal"); 

    cboFrom.DataBindings.Clear(); 
    cboTo.DataBindings.Clear(); 

    cboFrom.Items.Add(ds.Tables["whbal"]); 

    cboFrom.DataSource = ds.Tables["whbal"]; 
    cboFrom.DisplayMember = "customer"; 
    cboFrom.ValueMember = "customer"; 

    myconnection.Close(); 

    reportViewer1.Reset(); 
    reportViewer1.LocalReport.DataSources.Clear(); 

    LocalReport report = new LocalReport(); 


    reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", customerBindingSource)); 
    report.ReportPath = "Report1.rdlc"; 
    reportViewer1.LocalReport.Refresh(); 

} 

任何人都可以請評論並幫助如果有什麼我錯過了打的錯誤? 在此先感謝

回答

2

您的cboFrom.Items.Add()調用是完全錯誤的。
使用數據綁定時,根本無法操作Items

+0

那麼應該如何修改呢? 我發現了很多指導做rdlc報告的網站.. 但是我無法理解爲什麼有些人使用數據集,爲什麼有些人使用數據表.. confusing .. – user2901955

+0

只需刪除該行。你可能也不應該清除數據綁定。 – SLaks

+0

好了,現在錯誤已解決..謝謝這麼多,並讚賞.. BUT ..我可以問另一個問題嗎? 我點擊按鈕後,報表不顯示,組合框項目消失。 你知道錯誤是什麼?我上面的報告編碼是正確的? – user2901955