0

i wanted to know the code for putting values from comboxes into Datagridview but i tried but im getting coversion exception..

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=grade;Integrated Security=True;Pooling=False")
        Dim cmd As SqlCommand

        If ComboClass.SelectedItem <> "" Or ComboDiv.SelectedItem <> "" Or ComboSubject.SelectedItem <> "" Or ComboTeacher.SelectedItem = "" Or ComboYrstart.SelectedItem <> "" Or ComboYrEnd.SelectedItem <> "" Then


            Try
                conn.Open()
                Dim query As String = "insert into TeacherSubject(Teacher,Class,Div,Subject,YearStart,YearEnd)values(@Teacher,@Class,@Div,@Subject,@YearStart,@YearEnd)"
                cmd = New SqlCommand(query, conn)
                cmd.Parameters.AddWithValue("@Teacher", (ComboTeacher.SelectedItem()))
                cmd.Parameters.AddWithValue("@Class", (ComboClass.SelectedItem()))
                cmd.Parameters.AddWithValue("@Div", (ComboDiv.SelectedItem()))
                cmd.Parameters.AddWithValue("@Subject", (ComboSubject.SelectedItem()))
                cmd.Parameters.AddWithValue("@YearStart", (ComboYrstart.SelectedItem()))
                cmd.Parameters.AddWithValue("@YearEnd", (ComboYrEnd.SelectedItem()))

                Dim NoOfRowsAffected As Integer = cmd.ExecuteNonQuery()  //Exception of Conversion
                MessageBox.Show("Teacher-Subject Details Inserted")
                LoadDataInDataGrid()
                ComboTeacher.SelectedItem = ""
                ComboClass.SelectedItem = ""
                ComboDiv.SelectedItem = ""
                ComboSubject.SelectedItem = ""
                ComboYrstart.SelectedItem = ""
                ComboYrEnd.SelectedItem = ""


            Catch ex As Exception
                MessageBox.Show(ex.ToString())

            Finally
                If conn.State = ConnectionState.Open Then
                    conn.Close()

                End If
            End Try
        Else
            MessageBox.Show("Please fill proper details")

        End If
    End Sub
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by artemix22
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.