2014-01-21 188 views
-1

我得到一个运行时错误“ConnectionString属性尚未初始化”,当我“建设”项目也没有显示的错误....ConnectionString属性尚未初始化

private void FrmPrincipal_Load(object sender, EventArgs e) 
    {    
     button4.Enabled = false; 
     this.ProductsTableAdapter.Fill(this.ProductDataSet.Products); 
     this.SalesTableAdapter.Fill(this.SalesDataSet.Sales); 
     string cnString = null; 
     cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\POS.mdb"; 
     conn = new OleDbConnection(cnString); 
     try 
     { 
      conn.Open(); 
     } 
     catch (OleDbException ex) 
     { 
      MessageBox.Show(ex.Message, "Error..", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     finally 
     { 

     } 
     txtVAT.Text = ""; 
     txtCash.Text = ""; 
     txtChange.Text = ""; 
     txtTotal.Text = ""; 
     SalesIDTextBox.Text = ""; 

    } 
+0

你从哪里得到那个异常?在创建OleDbConnection之前,您正在使用表格适配器,这是否有意义? –

+1

是你在这些代码之前使用'conn'的任何地方。你在哪里声明了这个变量? – Sachin

+0

您将不得不向我们提供更多信息... –

回答

2

我根据您提供的信息,您打算在连接打开之前尝试填充适配器。尝试将该代码移动到try/catch

string cnString = null; 
    cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\POS.mdb"; 
    conn = new OleDbConnection(cnString); 
    try 
    { 
     conn.Open(); 
     // Now that the connection is open, it can be used below 
     this.ProductsTableAdapter.Fill(this.ProductDataSet.Products); 
     this.SalesTableAdapter.Fill(this.SalesDataSet.Sales); 
    } 
    catch (OleDbException ex) 
    { 
     MessageBox.Show(ex.Message, "Error..", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
相关问题