Inserting DATA in SQL using Listview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teqkillah
    New Member
    • Dec 2007
    • 12

    Inserting DATA in SQL using Listview

    guys good day! i have a problem using listview. can you help with the syntax? i have no idea how to code this. after populating the listview i want it to save all data from the collection to the table in sql.

    TIA
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    You can try this :

    foreach(ListIte m l in ListBox1.Items)
    {
    if(l.Selected)
    {
    //insert data
    }
    }


    Originally posted by teqkillah
    guys good day! i have a problem using listview. can you help with the syntax? i have no idea how to code this. after populating the listview i want it to save all data from the collection to the table in sql.

    TIA

    Comment

    • teqkillah
      New Member
      • Dec 2007
      • 12

      #3
      sory for my ignorance. im a noobie. can u expound it a little more?

      Comment

      • teqkillah
        New Member
        • Dec 2007
        • 12

        #4
        by the way heres my code.

        Dim iCount As Integer
        Dim iLoop As Integer
        query3 = New SqlCommand
        query3.Connecti on = connection
        iCount = ListView1.Items .Count
        If Not iCount = 0 Then
        Do Until iLoop = iCount

        query3.CommandT ext = "insert into inv_dtl(invid,m atgroup)values " _
        & "(" & Me.TextBox1.Tex t & ",'" _
        & "'" & ListView1.Items .Item(iCount).S ubItems(0).ToSt ring & "')"

        query3.ExecuteN onQuery()
        iLoop = iLoop + 1

        Loop
        End If

        I always get the error
        "Specified argument was out of the range of valid values.Paramete r name: '1'is not a valid value for 'displayindex'

        Ive notice that the number depends on how many rows i have in my listview. I desperately need your help guys.

        TIA

        Comment

        • CyberSoftHari
          Recognized Expert Contributor
          • Sep 2007
          • 488

          #5
          Your list count will return “1 to count” and your index value will go in “0 to count-1”
          did you tried like
          [CODE=vbnet]Do Until iLoop = iCount -1[/CODE]

          Comment

          • shweta123
            Recognized Expert Contributor
            • Nov 2006
            • 692

            #6
            Hi,

            You are getting the exception 'Out of Range Argument' exception because value of iCount is getting above the total no of ListView items present.
            You can do it both the ways

            1) Do until iloop= icount -1

            2) If you do it using the following way :

            foreach(ListIte m l in ListView1.Items )
            {
            query3.CommandT ext = "insert into inv_dtl(invid,m atgroup)values " _
            & "(" & Me.TextBox1.Tex t & ",'" _
            & "'" & l.SubItems(0).T oString & "')"
            }

            You do not have to keep count of iCount variable.

            Comment

            • teqkillah
              New Member
              • Dec 2007
              • 12

              #7
              Originally posted by CyberSoftHari
              Your list count will return “1 to count” and your index value will go in “0 to count-1”
              did you tried like
              [CODE=vbnet]Do Until iLoop = iCount -1[/CODE]
              ive tried this one. i havent receive any error but the data in my listview didnt save in sql.

              Comment

              • teqkillah
                New Member
                • Dec 2007
                • 12

                #8
                Originally posted by shweta123
                Hi,

                You are getting the exception 'Out of Range Argument' exception because value of iCount is getting above the total no of ListView items present.
                You can do it both the ways

                1) Do until iloop= icount -1

                2) If you do it using the following way :

                foreach(ListIte m l in ListView1.Items )
                {
                query3.CommandT ext = "insert into inv_dtl(invid,m atgroup)values " _
                & "(" & Me.TextBox1.Tex t & ",'" _
                & "'" & l.SubItems(0).T oString & "')"
                }

                You do not have to keep count of iCount variable.
                im trying to use the 2nd one. is this for a vb.net? the item in the for each listitem and I are variable?

                Comment

                • Shashi Sadasivan
                  Recognized Expert Top Contributor
                  • Aug 2007
                  • 1435

                  #9
                  Hi,
                  That code provided is C#, vb .net code is not a C type code

                  Following is the (hopefully) convert version of the C# code to VB .Net using an online coverter. Hope this will explain what the code meant.

                  [CODE=vbnet]foreach(ListIte m l in ListView1.Items )
                  {
                  query3.CommandT ext = "insert into inv_dtl(invid,m atgroup)values " _
                  & "(" & Me.TextBox1.Tex t & ",'" _
                  & "'" & l.SubItems(0).T oString & "')"
                  }[/CODE]

                  Comment

                  • teqkillah
                    New Member
                    • Dec 2007
                    • 12

                    #10
                    Originally posted by Shashi Sadasivan
                    Hi,
                    That code provided is C#, vb .net code is not a C type code

                    Following is the (hopefully) convert version of the C# code to VB .Net using an online coverter. Hope this will explain what the code meant.

                    [CODE=vbnet]foreach(ListItem l in ListView1.Items )
                    {
                    query3.CommandT ext = "insert into inv_dtl(invid,m atgroup)values " _
                    & "(" & Me.TextBox1.Tex t & ",'" _
                    & "'" & l.SubItems(0).T oString & "')"
                    }[/CODE]
                    are this variables? do i have to decalre them? sori for my ignorance.

                    Comment

                    • teqkillah
                      New Member
                      • Dec 2007
                      • 12

                      #11
                      by the way back to my code. after debugging it its quite unusual eventhough ive added items to my listview still the icount returns 0 value. even if i set a value to icount it still returns 0. i think this were the problem is. your help is greatly appreciated. TIA

                      iCount = ListView1.Items .Count.ToString
                      If iCount > 0 Then
                      Do Until iLoop = iCount - 1

                      query3.CommandT ext = "insert into inv_dtl(invid,m atgroup,materia l_code,material ,qty,uom,price, gross,vat,net)v alues " _
                      & "(" & Me.TextBox1.Tex t & ",'" _
                      & "'" & ListView1.Items .Item(iCount).S ubItems(2).ToSt ring & "')"

                      query3.ExecuteN onQuery()
                      iLoop = iLoop + 1

                      Loop
                      End If

                      Comment

                      • teqkillah
                        New Member
                        • Dec 2007
                        • 12

                        #12
                        guys i already did it! yahoo! by the way heres the code for reference. thanks to all of you.

                        If Not ListView1.Items .Count = 0 Then
                        Do Until iLoop = ListView1.Items .Count

                        LvItem = ListView1.Items .Item(iLoop)
                        With LvItem
                        query3.CommandT ext = "insert into inv_dtl(invid,m atgroup,materia l_code,material ,qty,uom,price, gross,vat,net) values " _
                        & "('" & TextBox1.Text & "','" & .SubItems(0).Te xt & "','" & .SubItems(1).Te xt & "','" & .SubItems(2).Te xt & "','" & .SubItems(3).Te xt & "','" & .SubItems(4).Te xt & "','" & .SubItems(5).Te xt & "','" & .SubItems(6).Te xt & "','" & .SubItems(7).Te xt & "','" & .SubItems(8).Te xt & "')"
                        query3.ExecuteN onQuery()
                        End With

                        iLoop = iLoop + 1
                        LvItem = Nothing
                        Loop
                        End If

                        Comment

                        Working...