2014-03-03 65 views
0

我有一個子功能或我也不是什麼任期確實是和下面的代碼:如何解決數據網格與ConnectionString屬性尚未初始化

Public Sub GridMiner() 
    Try 
    Con.Open() 
    sql = "Select roomnumber AS [Room], [end] AS [Check out at], hours as [on] FROM rooms where [end] between ? and ?" 
    cmd = New OleDb.OleDbCommand(sql, Con) 
    cmd.Parameters.AddWithValue("@from", Now.AddDays(0).ToString("MM/dd/yyyy")) 
    cmd.Parameters.AddWithValue("@to", Now.AddDays(1).ToString("MM/dd/yyyy")) 

    da = New OleDb.OleDbDataAdapter(cmd) 
    da.Fill(ds) 

    gridSummary.DataSource = ds.Tables(0) 
    ds = Nothing 
    da = Nothing 
    Con.Close() 
    Con.Dispose() 
    Catch ex As Exception 
    MsgBox(ex.Message) 
    End Try 
End Sub 

代碼完美的作品。現在我已經創建了一個刷新按鈕來刷新它的數據。不過,我得到了以下錯誤:

The ConnectionString property has not been initialized.

現在我有調試和它指向我con.Open()線。這是我的按鈕代碼:

Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click 
    GridMiner() 
End Sub 

,我也嘗試着寫一遍代碼我的按鈕內:

Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click 
    con.Open() 
    sql = "Select roomnumber AS [Room], [end] AS [Check out at], hours as [on] FROM rooms where [end] between ? and ?" 
    cmd = New OleDb.OleDbCommand(sql, con) 
    cmd.Parameters.AddWithValue("@from", Now.AddDays(0).ToString("MM/dd/yyyy")) 
    cmd.Parameters.AddWithValue("@to", Now.AddDays(1).ToString("MM/dd/yyyy")) 

    da = New OleDb.OleDbDataAdapter(cmd) 
    da.Fill(ds) 

    gridSummary.DataSource = ds.Tables(0) 
    ds = Nothing 
    da = Nothing 
    con.Close() 
    con.Dispose() 
End Sub 

更新:

我嗨更新我的代碼,並刪除con.dispose()然而,當我刪除它說。

值不能爲空 參數名:數據集

然後我刪除

ds = Nothing 

然後,它的工作原理,但它補充,而不是它刷新到DataGrid的信息。

回答

0

你正在處理連接,因此錯誤。

刪除:

Con.Dispose()

+0

查看更新後的問題 –

+0

是的,但是這是關於錯誤「ConnectionString屬性尚未初始化」的初始問題的答案。無論如何,我很高興你解決了你的錯誤。 –

+0

Yeap你是正確的,但我怎樣才能刪除數據網格中的第一個值,並刷新它? –

相關問題