Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi it me again PingLocalHost, can any one teach me or help me how
add data and display in listview only, and when the user is finish to input all data in listview and decide to save all data in listview they easily click a SAVE button and all data they input is save to the sql dataBase , i trued to search in google but i cant find the answer . i tried to use a sqlDatareader to listview but i not work for me . please me me guys. tnx
Posted

1 solution

Is it true that Brother Google can't help you? I'm not sure! There are tousdend of examples: inserting data into sql database[^]

Ok, i will show how to do this.
1) Create new project (Windows application)
2) On the Form1:
- add ListView and change it name to: LVData2Save
- add Button and change it name to: CmdSave
3) Copy code below and paste it into module Form1 class

Remember! This example is very simple. To be more proffessional you need to change it. Read more about: creating windows forms applications[^]
Take a look at:
Carl Prothman site[^]
connectionstrings site[^]

VB
Public Class Form1

    Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click
        Dim i As Integer = 0, retVal As Integer = 0
        Dim sSQL As String = String.Empty, sConn As String = String.Empty
        Dim oConn As SqlClient.SqlConnection = Nothing, oComm As SqlClient.SqlCommand = Nothing
        Try
            sConn = "Data Source=YourServerName;" & _
                    "Initial Catalog=A_TEST;Integrated Security=True"
            oConn = New SqlClient.SqlConnection(sConn)
            oConn.Open()

            For i = 0 To Me.LVData2Save.Items.Count - 1
                sSQL = "INSERT INTO [Table_1] (Field1)" & vbCr & _
                        "VALUES ('" & Me.LVData2Save.Items(i).Text & "')"
                oComm = New SqlClient.SqlCommand(sSQL, oConn)
                'get count of records affected
                retVal = oComm.ExecuteNonQuery()
                If retVal = 0 Then
                    MsgBox("Can't add this element: '" & Me.LVData2Save.Items(i).Text & "'", MsgBoxStyle.Information, "Error (row=" & i.ToString & ")")
                    'Exit For
                End If
            Next

        Catch ex As SqlClient.SqlException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")

        Finally
            oComm.Dispose()
            oComm = Nothing
            If Not oConn Is Nothing AndAlso oConn.State = ConnectionState.Open Then oConn.Close()
            oConn = Nothing
        End Try

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer = 0

        With Me.LVData2Save
            .View = View.Details
            .Columns.Add("Data")
            .Columns(0).TextAlign = HorizontalAlignment.Left
        End With

        For i = 1 To 10
            Me.LVData2Save.Items.Add("Item " & i.ToString)
        Next
    End Sub
End Class
 
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