I am trying to save checked items from a checkedlistbox to my SQL database and i am filling my checkedlistbox from the same SQL database,So far i am able to get the text of checked item from the checkedlistbox and i saved it in a string then i used a label to display if i am getting the text of checked item or not and its working but when i try to insert the checked data in database i get a error "Connection property has not been initialized." on ExecuteNonQuery() method.
Imports System.Data Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim da As New SqlDataAdapter Dim dt As New DataTable Dim connectionString As String = "Server=DESKTOP-V12PTAV ;Database=test ;User Id=sa ;Password=wills8877" Using conn As New SqlConnection(connectionString) conn.ConnectionString = connectionString conn.Open() Dim str As String str = "Select sem1 From sem" da = New SqlDataAdapter(str, conn) dt = New DataTable da.Fill(dt) CheckedListBox1.DataSource = dt CheckedListBox1.DisplayMember = "sem1" conn.Close() End Using End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim str As String Dim cmd As New SqlCommand Dim sql As String Dim connectionString As String = "Server=DESKTOP-V12PTAV ;Database=test ;User Id=sa ;Password=wills8877" Using conn As New SqlConnection(connectionString) conn.Open() Dim itemChecked As Object For Each itemChecked In CheckedListBox1.CheckedItems str = itemChecked.item("sem1").ToString Label1.Text = str sql = "insert into pretab(pre) values('" + str + "')" cmd.ExecuteNonQuery() Next conn.Close() End Using End Sub End Class