如何从checkedlistbox中获取所选项并将其保存到SQL数据库表? [英] How to get selected items from checkedlistbox and save it to SQL database table ?

查看:71
本文介绍了如何从checkedlistbox中获取所选项并将其保存到SQL数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在制作一个应用程序,我想从我的SQL数据库表填充我的checkedlistbox,然后将检查的项目保存在同一个数据库中,我可以从数据库中填充它但我收到错误的同时试图从checkedlistbox中获取已检查的项目并将它们保存在同一个数据库中。







< b>



我尝试过:



这是我的代码: -



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





当我选择任何项目并按下按钮时出现此错误: -

从对象类型system.data.datarowview到已知的托管提供者本机类型不存在映射。



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.

推荐答案

如果你调试它,它应该很容易看到。此外,当您收到错误时,还会显示哪行代码导致错误。在这种情况下,我可以告诉它是什么时候将参数添加到cmd,因为您尝试添加整个项目而不是项目文本。将其更改为
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
CheckedListBox1.Items(i).Text


这篇关于如何从checkedlistbox中获取所选项并将其保存到SQL数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆