Can anyone check my search code?
What seems to be the problem?
**ERROR: **Object variable or with block variable not set.
Dim con As New SqlConnection(connectionString:="Data Source=APBIPHAP06; Initial Catalog=PMT; Integrated Security=SSPI")
Dim ds As New DataSet()
Dim da As New SqlDataAdapter()
Dim query As String = "SELECT * FROM tblPM WHERE UsageDate BETWEEN @UsageDate1 AND @UsageDate2"
Try
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@UsageDate1", dtpSearchDateFrom.Value)
cmd.Parameters.AddWithValue("@UsageDate2", dtpSearchDateTo.Value)
da.SelectCommand = cmd
da.Fill(ds, "tblPMData")
With dgvPMData
.DataSource = ds.Tables("tblPM")
.DataMember = "tblPM"
.DisplayMember = "UsageDate"
.ValueMember = "UsageDate"
End With
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close() 'Whether there is error or not. Close the connection.
End Try
Thank you.