retrieve records from db based on month and year concatenation

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have combobox for year and combobox for month concatenated to make the date format "2012-01"
My db field is RecordDate and is smallDateTime
In my select statement im trying to use the left function to find match my concatenated string to the RecordDate field.
Here is my code, hopefully someone can help me. Currently im getting the error "Conversion failed when converting date and/or time from character string."
<pre> Dim newRecordDate As String = (CStr(ComboBox2.SelectedValue)) & "-" & val1
Dim val10 As String = CStr(Convert.ToDateTime(newRecordDate))
Dim val21 As String = Microsoft.VisualBasic.Right(val10, 7)
MsgBox(val10)
MsgBox(val21)

Dim con As New SqlClient.SqlConnection, cmd As New SqlClient.SqlCommand, adp As New SqlClient.SqlDataAdapter, ds As New DataSet, dt As New DataTable
cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = val21
con.ConnectionString = "dfbafn"
Dim sql As String = "Select CatA, CatB, CatC, Cost, Currency, MarketingCode, Comment, RecordDate from vw_tblP_Usage_Details where puid = " & uid & " and left(RecordDate, 7) = @date"
cmd.Connection = con
cmd.CommandText = sql
adp.SelectCommand = cmd
cmd.Connection.Open()
adp.Fill(ds)
cmd.Connection.Close()
dt = ds.Tables(0)
Me.DataGridView1.DataSource = dt


For i As Integer = 0 To dt.Rows.Count - 1
DataGridView1.Rows(i).Cells(payment.Name).Value = dt.Rows(i)("CatA")
DataGridView1.Rows(i).Cells(Column1.Name).Value = dt.Rows(i)("CatB")
DataGridView1.Rows(i).Cells(Column2.Name).Value = dt.Rows(i)("CatC")
DataGridView1.Rows(i).Cells(price.Name).Value = dt.Rows(i)("Cost")
DataGridView1.Rows(i).Cells(currency.Name).Value = dt.Rows(i)("Currency")
DataGridView1.Rows(i).Cells(markCode.Name).Value = dt.Rows(i)("MarketingCode")
DataGridView1.Rows(i).Cells(lineComment.Name).Value = dt.Rows(i)("Comment")
Next[/code]

<br/>

View the full article
 
Back
Top