473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
Dec 6 '07 #1
11 6113
shweta123
692 Expert 512MB
Hi,

You can try this :

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


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
Dec 6 '07 #2
sory for my ignorance. im a noobie. can u expound it a little more?
Dec 6 '07 #3
by the way heres my code.

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

query3.CommandText = "insert into inv_dtl(invid,matgroup)values " _
& "(" & Me.TextBox1.Text & ",'" _
& "'" & ListView1.Items.Item(iCount).SubItems(0).ToString & "')"

query3.ExecuteNonQuery()
iLoop = iLoop + 1

Loop
End If

I always get the error
"Specified argument was out of the range of valid values.Parameter 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
Dec 6 '07 #4
CyberSoftHari
487 Expert 256MB
Your list count will return “1 to count” and your index value will go in “0 to count-1”
did you tried like
Expand|Select|Wrap|Line Numbers
  1. Do Until iLoop = iCount -1
Dec 6 '07 #5
shweta123
692 Expert 512MB
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(ListItem l in ListView1.Items)
{
query3.CommandText = "insert into inv_dtl(invid,matgroup)values " _
& "(" & Me.TextBox1.Text & ",'" _
& "'" & l.SubItems(0).ToString & "')"
}

You do not have to keep count of iCount variable.
Dec 6 '07 #6
Your list count will return “1 to count” and your index value will go in “0 to count-1”
did you tried like
Expand|Select|Wrap|Line Numbers
  1. Do Until iLoop = iCount -1
ive tried this one. i havent receive any error but the data in my listview didnt save in sql.
Dec 7 '07 #7
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(ListItem l in ListView1.Items)
{
query3.CommandText = "insert into inv_dtl(invid,matgroup)values " _
& "(" & Me.TextBox1.Text & ",'" _
& "'" & l.SubItems(0).ToString & "')"
}

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?
Dec 7 '07 #8
Shashi Sadasivan
1,435 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. foreach(ListItem l in ListView1.Items)
  2. {
  3. query3.CommandText = "insert into inv_dtl(invid,matgroup)values " _
  4. & "(" & Me.TextBox1.Text & ",'" _
  5. & "'" & l.SubItems(0).ToString & "')"
  6. }
Dec 7 '07 #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.

Expand|Select|Wrap|Line Numbers
  1. foreach(ListItem l in ListView1.Items)
  2. {
  3. query3.CommandText = "insert into inv_dtl(invid,matgroup)values " _
  4. & "(" & Me.TextBox1.Text & ",'" _
  5. & "'" & l.SubItems(0).ToString & "')"
  6. }
are this variables? do i have to decalre them? sori for my ignorance.
Dec 7 '07 #10
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.CommandText = "insert into inv_dtl(invid,matgroup,material_code,material,qty, uom,price,gross,vat,net)values " _
& "(" & Me.TextBox1.Text & ",'" _
& "'" & ListView1.Items.Item(iCount).SubItems(2).ToString & "')"

query3.ExecuteNonQuery()
iLoop = iLoop + 1

Loop
End If
Dec 7 '07 #11
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.CommandText = "insert into inv_dtl(invid,matgroup,material_code,material,qty, uom,price,gross,vat,net) values " _
& "('" & TextBox1.Text & "','" & .SubItems(0).Text & "','" & .SubItems(1).Text & "','" & .SubItems(2).Text & "','" & .SubItems(3).Text & "','" & .SubItems(4).Text & "','" & .SubItems(5).Text & "','" & .SubItems(6).Text & "','" & .SubItems(7).Text & "','" & .SubItems(8).Text & "')"
query3.ExecuteNonQuery()
End With

iLoop = iLoop + 1
LvItem = Nothing
Loop
End If
Dec 7 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rico Alexander | last post by:
is there a way to retrieve the value of an autoincrement column inserted in the database?
2
by: Fahad | last post by:
I have a web forms that I insert data in database using arabic and Farsi language. The problem is when I insert a record, A record is added but I get blank fields in the columns. When I insert a...
2
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
4
by: Jaraba | last post by:
In Microsoft SQL I can run the following command: insert into table execute proc1 to insert data into a table. Is there a comparable way to do this in DB2?
0
by: pd123 | last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error " The name 'Peter' is...
0
by: le0 | last post by:
Guys, I create a simple insertion in a database using DetailsView control, when i run this page and try to insert some data, the browser returns an error like this ...
18
by: boss1 | last post by:
Hi all, i m having a problem with inserting data in oracle db. When i use form action =(call self page) then data is inserting properly.But problem with when using form...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: Andre Saputra | last post by:
Dear all. I really need help for my code. im using text box to filter data in listview, heres the code reloadlistview1 "select * from sales where TANGGAL between #" & Text1.Text & "#" & _ ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.