Click here to Skip to main content
15,876,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys i am making an application where i want to fill my checkedlistbox from my SQL database table and then save back the checked items in the same database and i am able to fill it from the database but i am getting an error while trying to get the checked items from the checkedlistbox and saving them in the same database.





What I have tried:

Here is my code:-

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
    Private pre As DataTable


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckedListBox1.DataSource = getdata()
        CheckedListBox1.DisplayMember = "sem1"

    End Sub

    Private Function getdata() As DataTable
        pre = New DataTable
        Dim str As String = "select sem1 from sem"
        Dim connectionString As String = "Server=DESKTOP-V12PTAV ;Database=test ;User Id=sa ;Password=wills8877"
        Using conn As New SqlConnection(connectionString)
            Using adapter As New SqlDataAdapter(str, conn)
                conn.Open()
                adapter.Fill(pre)
                conn.Close()
            End Using
        End Using

        Return pre

    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim SqL As String = "INSERT INTO sem (sem2) " & "VALUES (sem2)"

        Dim connectionString As String = "Server=DESKTOP-V12PTAV ;Database=test ;User Id=sa ;Password=wills8877"
        Using conn As New SqlConnection(connectionString)
            conn.Open()
            Try
                Using cmd As New SqlCommand(SqL, conn)

                    For i As Integer = 0 To CheckedListBox1.Items.Count - 1

                        Dim chkstate As CheckState

                        chkstate = CheckedListBox1.GetItemCheckState(i)

                        If (chkstate = CheckState.Checked) Then

                            cmd.Parameters.AddWithValue("sem2", CheckedListBox1.Items(i))
                            cmd.ExecuteNonQuery()
                            cmd.Parameters.Clear()

                        End If
                    Next
                End Using

                MessageBox.Show("Students saved")

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            conn.Close()
        End Using

    End Sub
End Class


I am getting this error when i select any item and press the button:-
No mapping exists from object type system.data.datarowview to a known managed provider native type.
Posted
Updated 10-Apr-17 6:46am

1 solution

If you debug it, it should be pretty easy to see. Also, when you get an error also show which line of code causes the error. In this case I can tell it is when you are adding the parameter to cmd because you tried to add the entire Item instead of the items Text. Change it to
C#
CheckedListBox1.Items(i).Text
 
Share this answer
 
v2
Comments
Member 13118967 10-Apr-17 12:46pm    
Ok i tried it i am getting this error now

Public member 'text' on type 'datarowview' not found.
ZurdoDev 10-Apr-17 13:42pm    
Turns out it does not have a text property. You just need to look up the documentation. Call .ToString() instead of .Text.

I apologize, I haven't done Windows Forms in many years.
Member 13118967 10-Apr-17 23:46pm    
You mean getitemtext method ?
Member 13118967 11-Apr-17 10:10am    
I tried .ToString its still not working.
ZurdoDev 11-Apr-17 10:12am    
"Not working" does not tell us anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900