Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys i want to save checked items in my checkedlistbox to my sql databases i am managed to get the string of checked items and i saved it in a string variable to make sure that i am getting the text i displayed it on a label but i dont know how do i insert it in my sql database.

What I have tried:

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



        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
                Dim str As String
                str = itemChecked.item("sem1").ToString
                Label1.Text = str
            Next
            conn.Close()

        End Using
Posted
Updated 21-Apr-17 22:15pm

1 solution

I hope i did not forget something
you sould use transaction,parameters and try
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 

 try
            Dim connectionString As String = "Server=DESKTOP-V12PTAV ;Database=test ;User Id=sa ;Password=wills8877"
            Using conn As New SqlConnection(connectionString)
                conn.Open()
                Dim cmd as new System.Data.SqlCommand("insert into x (z) values (@z)",     conn) 
                cmd.Parameters.AddWithValue("@z", "")
                System.Data.SqlTransaction strns=conn.BeginTransaction()
                try
                    cmd.Transaction=strns
                    Dim itemChecked As Object
                    For Each itemChecked In CheckedListBox1.CheckedItems
                        Dim str As String
                        str = itemChecked.item("sem1").ToString
                        cmd.Parameters(0).value=str
                        cmd.ExecuteNonQuery
                        Label1.Text = str
                    Next
                    strns.Commit()
                Catch ex As Exception
                    strns.RollBack()
                    msgbox(ex.message)
                End Try
                strns.Dispose()
                cmd.dispose()
                conn.Close()
                conn.Dispose()
            Catch ex As Exception
                msgbox(ex.message)
            End Try
        End Using
 
Share this answer
 

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