Populating multiple ListViews

This is a windows form. I have a transaction form that I need a lot of help on. first off there's a textEnterISBN box that populates a listview from the text entered from the text box. I have coded this but I can't get my listview to populate from my query. I think the help is needed when the displayISBNNumber method is run. Once the first listview is populated I want another another button shown in my code as " addToReceipt" to grab the first listviews selected index and put it into the second listView which I'm not sure I have the knowledge to do. The delete button will just delete from the second listbox pretty strait forward. and the complete transaction will take the items from the second listbox and pop them into another DB and delete them from original memberBookInventoryDB. A txtTotal calculates the total from the second listbox. The query I made to retrieve the ISBN is provided along with my forms code with comments and some methods. Please any help would be great!
public partial class frmTransaction : Form
    {
        Form formParent = null;
        DateTime date = DateTime.Today;
        public frmTransaction(Form par)
        {
            formParent = par;
            InitializeComponent();
            
        }
        MemberBook mb = new MemberBook();

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
            formParent.Show();

        }

        private void frmTransaction_Load(object sender, EventArgs e)
        {
            //String.Format("[0:d]", currentDate);
            MessageBox.Show(date.ToShortDateString());

        }

        private void btnAddToReceipt_Click(object sender, EventArgs e)
        {
            //adds listview1s selected item to listview 2.
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //Deletes item from listview2 box
        }

        private void btnComplete_Click(object sender, EventArgs e)
        {
            //takes items in listView2 and inserts them in CashOut DB and deletes them from listView1s DB
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            string ISBNNumber = txtISBNNumber.Text;
            this.GetISBN(ISBNNumber);
             if (mb == null)
                {
                    MessageBox.Show("ISBNNumber not found");                   
                }
                else
                    this.DisplayISBNNumber();
            
        
            //populates listView1 with the ISBNNumber(s) returned from memberBook DB.
        }

        private void DisplayISBNNumber()
        {
            //populate listview here?
            MemberBook mb = new MemberBook();
            ListViewItem item = listView1.Items.Add(mb.ISBNNumber);
           // mb.BookID = Convert.ToInt32(item.SubItems[1].Text);
            //mb.ISBNNumber = item.SubItems[2].Text;
            //mb.Title = item.SubItems[3].Text;
            //mb.Author = item.SubItems[4].Text;
            //mb.Description = item.SubItems[5].Text;
            //mb.Condition = item.SubItems[6].Text;
            //mb.Price = Convert.ToDecimal(item.SubItems[7].Text);
        }

        private void GetISBN(string ISBNNumber)
        {
            try
            {
                mb = MemberBookInventoryDB.GetISBNNumber(ISBNNumber);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
            }
        }
    }
}




       public static MemberBook GetISBNNumber(string ISBNNumber)
        {

              SqlConnection connection = MMABooksDB.GetConnection();
            string selectStatement
                = "SELECT BookID, ISBNNumber, Title, Author, MemberID, Price "
                + "FROM MemberBookInventory "
                + "WHERE ISBNNumber = @ISBNNumber";
            SqlCommand selectCommand =
              new SqlCommand(selectStatement, connection);
            selectCommand.Parameters.AddWithValue("@ISBNNumber", ISBNNumber);
            try
            {
                connection.Open();
                SqlDataReader reader =
                 selectCommand.ExecuteReader(CommandBehavior.Default);
                if (reader.Read())
                {
                    MemberBook mb = new MemberBook();
                    mb.BookID = (int)reader["BookID"];
                    mb.ISBNNumber = reader["ISBNNumber"].ToString();
                    mb.Title = reader["Title"].ToString();
                    mb.Author= reader["Author"].ToString();
                    mb.Member.MemberID = (int)reader["MemberID"];
                    mb.Price = (decimal)reader["Price"];
                    return mb;
                }
                else
                {
                    return null;                   
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }


    }
}

Open in new window

linkcube1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

muckeypuckCommented:
First of all, have you ensured you are getting back the books from your GetISBNNumber call?

if so, the next problem i see is this:

private void DisplayISBNNumber()
        {
            //populate listview here?
            MemberBook mb = new MemberBook();
            ListViewItem item = listView1.Items.Add(mb.ISBNNumber);
           // mb.BookID = Convert.ToInt32(item.SubItems[1].Text);
            //mb.ISBNNumber = item.SubItems[2].Text;
            //mb.Title = item.SubItems[3].Text;
            //mb.Author = item.SubItems[4].Text;
            //mb.Description = item.SubItems[5].Text;
            //mb.Condition = item.SubItems[6].Text;
            //mb.Price = Convert.ToDecimal(item.SubItems[7].Text);
        }

All you are doing with this line:

ListViewItem item = listView1.Items.Add(mb.ISBNNumber);

is creating a listviewitem. it doesn't go anywhere or do anything. as a matter of fact i think i can hear it crying because it wants a listview
to call home. There's something else. A listview is highly suspect. I am not sure thats the control you want. Thats for things like pictures of folders etc.
Since you are adding an isbn number (which i am pretty sure are string values) what you want is a listBOX.

when you want to add an isbn number to the listbox (or listview if you are absolutely determined) what you want to do is

ListViewItem item = new ListViewItem(mb.ISBNNumber);
listView1.Items.Add(item);

you can also be ubercool and do it on one line like so:
listView1.Items.Add(new ListViewItem(mb.ISBNNumber));


OR for a listbox

listBox1.Items.Add(mb.ISBNNumber)

0
linkcube1Author Commented:
Thanks for the answer, I am getting the mb object but I want to populate more than one column in the first listview hence the subitems so any advice with that would be great.
0
muckeypuckCommented:
Sorry, sometimes i need to pay better attention.


		private void DisplayISBNNumber()
		{
			listView1.Items.Add(new ListViewItem(new string[] {mb.ISBN, mb.Author}));

		}

Open in new window


assuming the listview.View = Details and you've added two columns

i think this is what you're saying
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

linkcube1Author Commented:
Yeah That what I was looking for seems to do the job. For the second list view does this work to add and populate it from a selected line from the first listview?

      private void btnAddToReceipt_Click(object sender, EventArgs e)
        {
         
                listView2.Items.Add(listView1.SelectedItems.ToString());
           
        }

the second box is receipt like doesn't need to be columned
0
muckeypuckCommented:
if you need the two listbox items independent of each other:

listview2.Items.Add(listview1.Items[0].Clone();

if you want it to disappear from either listview if you delete it from 1 listview

listview2.Items.Add(listview1.Items[0]);
0
linkcube1Author Commented:
I'm getting this.            

System.ArgumentException was unhandled
  Message="Cannot add or insert the item '' in more than one place. You must first remove it from its current location or clone it.\r\nParameter name: item"
  Source="System.Windows.Forms"
  ParamName="item"
 
running code below

the clone code example you provided is not working for me
getting this error
Error      3      Argument '1': cannot convert from 'object' to 'System.Windows.Forms.ListViewItem'      

private void btnAddToReceipt_Click(object sender, EventArgs e)
        {
         
                //listView2.Items.Add(listView1.SelectedItems.ToString());
            //listView2.Items.Add(listView1.Items);
            listView2.Items.Add(listView1.Items[0]);
            
        }


 private void DisplayISBNNumber()
        {
            //populate listview here?
            //ListViewItem item = listView1.Items.Add(mb.ISBNNumber);
            listView1.Items.Add(new ListViewItem(new string[] { mb.ISBNNumber, mb.Author, mb.Title }));
       
        }

Open in new window

0
muckeypuckCommented:
try casting it

listview2.Items.Add((ListViewItem)listview1.Items[0].Clone());
0
linkcube1Author Commented:
Alright that worked, Now when it clones my first listview of whatever index I select can I run a sql query and pop items in that second ListView to a new database table which will remove it from the database that populated my first listview? This is a transaction form, I want to scan an item select the item from the first list and populate it on the second one like it is now and then when a transaction is complete do something with the items in the second listview. Can I do this how this is set it?
0
muckeypuckCommented:
i'm not really sure what you're asking.

You can create a trigger on the second table so that when an isbn number is inserted, delete it from the first table. That will take part of some of your requirements. You can then wait for that db call to return a flag and process when the proc returns the flag.

It depends i guess on what it is your trying to do.
0
linkcube1Author Commented:
My isbn is populated from my memberbook database, when an isbn is entered into listview1 it populates it. I then select one row from listview1 which now is cloned into listview2. I want put listview2's items into a separate table and then delete those items from the original database.
0
muckeypuckCommented:
for each(listviewitem item in listview2
{

  process the information you need from each item to insert it into the query

}

run the query

the query i guess will look something like:

Insert into Table2
    Table2 values

Delete from table1
Where ISBN = @ISBN

or you can go the trigger route.
0
linkcube1Author Commented:
Ran into another problem I can only clone items[0] the first index in a column how can I do that if I select  the second or third row?
0
muckeypuckCommented:
items[0] = item 1
items[1] = item 2
so on and so forth
0
linkcube1Author Commented:
Ok, I got a little help for another post I made. Using the code below I receive an exception just having the add method add just an item and I can't figure out how to clone the item so I have a toString for now. Is there anyway to clone this? or get around this issue?
private void btnAddToReceipt_Click(object sender, EventArgs e)
        {    
            if(listView1.SelectedItems.Count > 0)
            {  
                 foreach(ListViewItem item in listView1.SelectedItems)
                 {
                     listView2.Items.Add(item.ToString());

                 }
            }

Open in new window

0
muckeypuckCommented:
private void btnAddToReceipt_Click(object sender, EventArgs e)
        {    
            if(listView1.SelectedItems.Count > 0)
            {  
                 foreach(ListViewItem item in listView1.SelectedItems)
                 {
                     listView2.Items.Add(new ListviewItem(item.ToString()));

                 }
            }
0
linkcube1Author Commented:
It still shows the same thing, Is there a way I can show the items without a ToString?
it shows up as

ListViewItem {"information about the item...."}
is there a way to make it just

"Information about the item"   ?
0
muckeypuckCommented:
ohhh i see. i actually think when you do add("some string") the new is implied so that was right anyways. what youre looking for i think is listView2.Items.Add(new ListviewItem(item.Text));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
linkcube1Author Commented:
That'll work thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.