I am currently working in Microsoft visual studio express 2013 with an sql back end. I am trying to run a loop through 2 comboboxes and a datetimepicker for any instance a checkbox is checked. However, I am running into an error that reads "System.ArgumentException: No Mapping exists from Object type system.windows.forms.datetimepicker to a known managed provider native type." When I run the code I have put a watch on the parameter value and it is not saving the data into the variable before the sql command fires. I think I need to store the variable in a different way to allow access to the variable. Here is my code:
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("SELECT isnull(max(AuditID) + 1, 1) as 'AuditID' FROM table1", conn1)
Dim reader1 As SqlDataReader = comm1.ExecuteReader
reader1.Read()
Dim AuditID As Integer = reader1("AuditID")
reader1.Dispose()
'Loop through all checkboxes and write into sql tables
Dim Run As Integer
For Run = 1 To 5
Dim LineItem = DirectCast(Me.Controls("CB" & Run), CheckBox)
If LineItem.Checked = True Then
Dim DateTime = DirectCast(Me.Controls("DTP" & Run), DateTimePicker)
Dim Frequency = DirectCast(Me.Controls("CBWeek" & Run), ComboBox)
Dim Repeat = DirectCast(Me.Controls("CBRepeat" & Run), ComboBox)
'sql statements
'select ID
Using conn2 As New SqlConnection(connstring)
conn2.Open()
Using comm2 As New SqlCommand("SELECT isnull(max(AuditID) + 1, 1) as 'ID' FROM table1", conn1)
Dim reader As SqlDataReader = comm2.ExecuteReader
reader.Read()
Dim ID As Integer = reader("ID")
reader.Dispose()
'Insert into table audit line
Using conn3 As New SqlConnection(connstring)
conn3.Open()
Using comm3 As New SqlCommand("INSERT INTO table1 (ID, AuditID, DateStart, Freq, repeats) " _
& "VALUES (@ID, @AuditID, @DateStart, @Freq, @Repeats)", conn3)
With comm3.Parameters
.AddWithValue("@ID", ID)
.AddWithValue("@AuditID", AuditID)
.AddWithValue("@DateStart", DateTime)
.AddWithValue("@Freq", Frequency)
.AddWithValue("@Repeats", Repeat)
End With
comm3.ExecuteNonQuery()
End Using
conn3.Close()
End Using
End Using
conn2.Close()
End Using
End If
Next
End Using
conn1.Close()
End Using
Catch ex As Exception
MsgBox(ex.ToString)
My try statement stop my code on this line:
comm3.ExecuteNonQuery()
However, I know that this error is coming from when I add my parameters, specifically these 3 lines:
.AddWithValue("@DateStart", DateTime)
.AddWithValue("@Freq", Frequency)
.AddWithValue("@Repeats", Repeat)
I am trying to get tese variables with a loop statement based on the design name here:
Dim DateTime = DirectCast(Me.Controls("DTP" & Run), DateTimePicker)
Dim Frequency = DirectCast(Me.Controls("CBWeek" & Run), ComboBox)
Dim Repeat = DirectCast(Me.Controls("CBRepeat" & Run), ComboBox)
It does not seem like the program likes using these dimensions above to be inserted into the sql table. Does anyone know a way I can carry these values over to the sql parameters statement?