You're disposing the connection, thus the error.
Remove:
Con.Dispose()
.
Question
I have a sub function or I don't what its term really is and here's the code:
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
that code works perfectly. Now I have created a refresh button to refresh its data. However, I got a following error:
The ConnectionString property has not been initialized.
Now I have debug it and it points me to con.Open()
line. This is my button code:
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
GridMiner()
End Sub
and I also try to write the code again inside my button:
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
UPDATED:
Hi I updated my code, and removed con.dispose()
however, when I removed it it says.
Value can't be null Parameter name: dataSet
Then I removed
ds = Nothing
Then it works, however it added the information instead of refreshing it to the datagrid.
Solution
You're disposing the connection, thus the error.
Remove:
Con.Dispose()
.