Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataAdapter SelectCommand With Parameters

Status
Not open for further replies.

Jables

Programmer
Aug 28, 2001
148
US
I have a datagrid on a windows form that gets populated by a DataTable when a button is clicked. I am trying to use the values provided by a combobox and two DateTime Pickers to supply parameters to the SelectCommand of the DataAdapter. Whenever I click the button I keep getting this error:

Input string not in the correct format.

Here is the Sub for the button's click event.
Code:
Private Sub btnLoad_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLoad.Click

'Define the string for the Select Command
Dim strSelectSQL As String = "SELECT * FROM tbl_Time_Record " & _
   "WHERE (EmployeeID = @EmployeeID) AND " & _
   "([Date Worked] >= @StartDate) AND " & _
   &quot;([Date Worked] <= @EndDate)&quot;

'Define SqlDataAdapter
Dim daEntries As SqlDataAdapter = New SqlDataAdapter

'Define new SqlCommand
Dim cmdSelect As SqlCommand = New SqlCommand(strSelectSQL, Me.oConnTimetracker)

'Set the SelectCommand property of the DataAdapter
daEntries.SelectCommand = cmdSelect

'Add Typed Parameters to the SelectCommand
cmdSelect.Parameters.Add(&quot;@EmployeeID&quot;, _
   SqlDbType.Int).Value = Me.cboEmployees.ValueMember
cmdSelect.Parameters.Add(&quot;@StartDate&quot;, _
   SqlDbType.DateTime).Value = Me.dtpStartDate.Value
cmdSelect.Parameters.Add(&quot;@EndDate&quot;, _
   SqlDbType.DateTime).Value = Me.dtpEndDate.Value

'Define a new dataset
Dim dsEntries As DataSet = New DataSet

'Fill the dataset and set the datagrid's datasource
daEntries.Fill(dsEntries)
Me.cboEmployees.DataSource = dsEntries.Tables(&quot;tbl_Time_Record&quot;)

End Sub
I haven't been very successful using SqlCommands with parameters. Can someone point me in the right direction?
 
Nevermind. I needed to set the @EmployeeID parameter value to cboEmployees.SelectedValue and convert the Date Time Picker values to ShortDateFormat.

Like I said, I haven't been working in VB .NET for that long.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top