Dear fellow programmers,

I am trying to build a form that has two datetimepickers (for date range) and one button. When a user clicks the button the datagridview will show results according on the date range (by the two datetimepickers).

This is my code so far:

Dim dbconnection As New OleDbConnection(Get_Constring) 'database path from a module
        Dim connectionstring As String = "SELECT * From EncodingComplete where (DATEVALUE(Date_Bill) >= DATEVALUE('" & DateTimePicker1.Text & "') and DATEVALUE(Date_Bill) <= DATEVALUE('" & DateTimePicker2.Text & "')) order by Date_Bill asc "
        Dim dbadapter As New OleDbDataAdapter(connectionstring, dbconnection)
        Dim clientdataset As New DataSet()
        dbadapter.Fill(clientdataset, "date1")
        Dim clientview As New DataView(clientdataset.Tables("date1"))
        DataGridView1.DataSource = clientview
        DataGridView1.DataBind() 'this is where the error is.. its purpose is to bind results to the datagridview

The error is on DataGridView1.DataBind(). Can somebody help me??

Recommended Answers

You got to have a DataSource and a BindingSource. See this article.

Jump to Post

All 2 Replies

You got to have a DataSource and a BindingSource. See this article.

@CodingSource: DataBindings is a Readonly property of DataGridView object, used to get the databindings of the object. So, remove the line 8.
And also there is a need to modify your SQl Statement. It should be

"SELECT * From EncodingComplete where Date_Bill Between " & DateTimePicker1.Value & " And " & DateTimePicker2.Value & " Order by Date_Bill"
'By default order by cause always producess result in ascending order.
' use of 'asc' is your choice.

Get_Constring should be the connection string variable.
You say that your error arised on the line 8, but you do not post the exception what it showed.
I am surprised, How can your codes run the lines 3 to 7 with a entirely closed connection. I am sure you got the error for the line 3

Dim dbadapter As New OleDbDataAdapter(connectionstring, dbconnection)

because dbconnection was still a closed connection. You didn't try to open it. Open it before the line no 3.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.