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.
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.
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.
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]
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 & "')"
}
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.
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?
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 & "')"
Comment