【问题标题】:invalid operation exception was unhandled (Additional information: Fill: SelectCommand.Connection property has not been initialized.)未处理无效操作异常(附加信息:填充:SelectCommand.Connection 属性尚未初始化。)
【发布时间】:2017-01-08 05:45:25
【问题描述】:
 namespace WindowsFormsApplication5
{
 public partial class Form1 : Form
{
    string conn = "Data Source=JUTT;Initial Catalog=product;Integrated Security=True";
    SqlConnection connection;
    SqlCommand command;
    SqlDataAdapter adapt;
    SqlConnection con;
    DataTable dt;
    string prnt;
    string sql = null;
    ReportDocument crystal = new ReportDocument();
    public Form1()
    {
        InitializeComponent();
    }

    private void crystalReportViewer1_Load(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        crystal.Load(@"c:\users\asad\documents\visual studio 2013\Projects\WindowsFormsApplication5\WindowsFormsApplication5\CrystalReport1.rpt");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        adapt = new SqlDataAdapter("select id,pd_name,price from sale where id like '" + textBox1.Text + "%'", con);
        DataSet ds = new DataSet();
        adapt.Fill(ds, "sale");
        CrystalReport1 rpt = new CrystalReport1();
        rpt.SetDataSource(ds);
        crystalReportViewer1.ReportSource = rpt;
        dt = new DataTable();
    }

    private void button2_Click(object sender, EventArgs e)
    {

                adapt = new SqlDataAdapter("select * from sale", con);
                DataSet ds = new DataSet();
                adapt.Fill(ds, "sale");
                CrystalReport1 rpt = new CrystalReport1();
                rpt.SetDataSource(ds);
                crystalReportViewer1.ReportSource = rpt;
                dt = new DataTable();
    }
}
 }

当我点击 button2 时出现异常:

“System.InvalidOperationException”类型的未处理异常 发生在 System.Data.dll 的 rpt.setdatasource 行

【问题讨论】:

    标签: c# .net crystal-reports datasource dataadapter


    【解决方案1】:

    在您的代码中,您已声明 SqlConnection con; 并且从未对其进行初始化,但后来您尝试在数据适配器构造函数中使用它:adapt = new SqlDataAdapter(".......", con);

    在使用之前初始化连接(使用适当的连接字符串创建它的实例),例如在表单构造函数中:

    public Form1()
    {
        InitializeComponent();
    
        con = new SqlConnection(conn);
    }
    

    【讨论】:

    • 现在此异常正在发生 mscorlib.dll 中出现“System.IO.FileNotFoundException”类型的未处理异常附加信息:无法加载文件或程序集“file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' 或其依赖项之一。系统找不到指定的文件。
    • 嗯,这个新的扩展与您展示的代码没有直接关系。它的文字非常不言自明:检查引用的程序集是否存在于指定位置
    猜你喜欢
    • 2013-02-25
    • 2014-03-06
    • 1970-01-01
    • 2011-08-10
    • 2014-04-20
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2017-10-01
    相关资源
    最近更新 更多