Saving ListViewItemCollection

Connection.Open()
Dim cmd As New OleDbCommand
cmd.CommandText = "INSERT INTO PANELDETAIL (PanelID,PanelName,ExamName) VALUES (@PanelID,@PanelName,@ExamName)"
cmd.Parameters.AddWithValue("@PanelID", tboxID.Text)
cmd.Parameters.AddWithValue("@PanelName", tboxName.Text)
cmd.Parameters.AddWithValue("@ExamName", lviewIncluded.Text)

cmd.Connection = Connection
cmd.ExecuteNonQuery()
Connection.Close()

Basically, this code just inserted a certain record on my database...All seems fine but the one with the question mark, I don't know how to insert but I am certain that it's wrong.I just want to ask how can I inserted all the items in a listview(lviewIncluded) in my database thanks in advance...


Answer this question

Saving ListViewItemCollection

  • Tkanos

    Perhaps I had to go back on this tread again...


    Connection.
    Open()
    For Each _index As ListViewItem In lviewIncluded.Items

    Dim myCommand As New OleDbCommand
    myCommand.CommandText = "UPDATE PANELDETAIL SET PanelCode=@PanelCode,ExamName=@ExamName WHERE PanelID=@PanelID"
    myCommand.Parameters.AddWithValue("@PanelCode", tboxCode.Text)
    myCommand.Parameters.AddWithValue("@ExamName", _index.Text)
    myCommand.Parameters.AddWithValue("@PanelID",
    CInt(Me.tboxDummy.Text))
    myCommand.Connection = Connection
    myCommand.ExecuteNonQuery()
    Next
    Connection.
    Close()
    Well what I am trying to do now is to update my database..Problem is whenever I am saving and implementing this code, the item being save was the last item I had inputed..Well perhaps I had to give you more details of what I am trying to accomplish here.Firstly, I had a form wherein I want to implement a header/detail type.PanelHeader and PanelDetail was two different tables on my db,now on the form I had textboxes wherein details about PanelHeader was displayed, at the same time I also had a listview(lviewIncluded) which displays all details of that particular panel, so certainly this data coming from the listview is from PanelDetails.Now, hope you still with me here, whenever I add(inserted) a certain details (PanelDetail) in lviewIncluded say 2 items, all was fine but when I edited that said details say I add another 1 item say "item03", using the code above and upon saving, displaying the data again would cause for all details(now still 2 items ) to be renamed as the same as the last detail inputed(2 items now named "item03")....What should be done to achieve my goal that whenver I updated, what had been added would be added as well as what had been deleted would be deleted...thanks.

  • Andrew Todd

    Try this

     

    dim currentItem As String

    Connection.Open()
                For Each currentItem In lviewIncluded.Items

                    Dim cmd As New OleDbCommand
                    cmd.CommandText = "INSERT INTO PANELDETAIL (PanelID,PanelCode,ExamName) VALUES (@PanelID,@PanelCode,@ExamName)"
                    cmd.Parameters.AddWithValue("@PanelID", tboxDummy.Text)
                    cmd.Parameters.AddWithValue("@PanelCode", tboxCode.Text)
                    cmd.Parameters.AddWithValue("@ExamName", currentItem)

                    cmd.Connection = Connection
                    cmd.ExecuteNonQuery()

                    cmd.Parameters.Clear()
                Next
                Connection.Close()



  • Menachem_P

    Problem is whenever I am saving and implementing this code, the item being save was the last item I had inputed

    Hmm, I dont understand how it can be possible, take for example, my solution above. It should go for each item in the listview, unless each item is the same

    What values are you getting in the foreach loop

    Which value is the same as the last value



  • Dick Campbell

    Using your code an exception was thrown stating:
    Conversion from type 'ListViewItem' to type 'String' is not valid. pointing to the line
    For Each currentItem In lviewIncluded.Items
    Notice also that I am using this code for the Update statement and not an insert one, so if that is an issue then perhaps there some changes to be made..thanks.


  • Paul Diston

    Ok, thanks...Such a great help....

  • sajohnstone

    Connection.Open()
    For Each *ListViewItem In lviewIncluded

    Dim cmd As New OleDbCommand
    cmd.CommandText = "INSERT INTO PANELDETAIL (PanelID,PanelCode,ExamName) VALUES (@PanelID,@PanelCode,@ExamName)"
    cmd.Parameters.AddWithValue("@PanelID", tboxDummy.Text)
    cmd.Parameters.AddWithValue("@PanelCode", tboxCode.Text)
    cmd.Parameters.AddWithValue("@ExamName", lviewIncluded.Text)

    cmd.Connection = Connection
    cmd.ExecuteNonQuery()
    Next
    Connection.Close()

    *ListViewItem is a type and cannot be used as an expression

    this was the debug error i am getting after your suggestion.I know there's a little mistake here on my part, but I beg your pardon as I am new here in programming...thanks.

  • GS80

    anyone please...

  • CESAR DE LA TORRE

    interesting.

    I've never worked with listview just yet so I am just guessing from the top of my head.

    What items are stored in the ListViewItem

    how about this:



    dim counter as int
    dim currentItemValue as string
    for counter = 0 to me.listItem.Count
    'Your SQL statements here
    currentItemValue = me.listItem[counter].Text
    next counter



  • Aquineas

    Great, glad it worked!

  • anf600

    I hope this helps...

    since there will be a collection of items in the listview, you cannot insert the entire list for 1 record like this.

    Remember in SQL, 1 row = 1 record

    From the code shown, to me, it seems as if you want to insert the text value of the control lviewIncluded.Text. is this correct

    If so, then it should be fine. Are you getting any errors if so - where and what is the error

    You are appearing to be asking how to insert all the items in the listview in SQL.

    you would insert an entry per item in listview. for example, here is a pseudocode:

    open connection

    foreach item in lviewIncluded

    Create SQL Insert command.

    Add current item in Insert Statement

    Execute query

    Clear parameters

    end foreach

    close connection.

    I hope this gives you a starting point.



  • Saving ListViewItemCollection