Save multiple data, add contents to listbox

Save multiple data, add contents to listbox

Page 1 of 1

0 Replies - 2720 Views - Last Post: 22 January 2009 - 07:51 PM Rate Topic: -----

#1 pandaduc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-December 08

Save multiple data, add contents to listbox

Post icon  Posted 22 January 2009 - 07:51 PM

I'm beginner in C# . In this code I show below , I use serialization to save the data, but I only save one time and retrieve one data .How can I save multiple data by using Serialization , and add the text in the title textbox into listbox with the detailed contents such as :quantity , price and extended price . I have tried many ways but it's not working

Thank you for your help.

Class BookSale.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
	[Serializable()]public class BookSale
	{
		private string strTitle;
		private int intQuantity;
		private decimal decPrice, decExtendedPrice;
		private static decimal decSalesTotal;
		private static int intSalesCount;

		public BookSale(string Title, int Quantity, decimal Price)
		{
			this.Title = Title;
			this.Quantity = Quantity;
			this.Price = Price;
			CalculateExtendedPrice();
			AddToTotals();
		}
		public string Title
		{
			set
			{
				strTitle = value;
			}
			get
			{
				return strTitle;
			}
		}

		public int Quantity
		{
			set
			{
				intQuantity = value;
			}
			get
			{
				return intQuantity;
			}
		}
		public decimal Price
		{
			set
			{
				decPrice = value;
			}
			get
			{
				return decPrice;
			}
		}
		public decimal ExtendedPrice
		{
			set
			{
				decExtendedPrice = value;
			}
			get
			{
				return decExtendedPrice;
			}
		}
		public static int SalesCount
		{
			get
			{
				return intSalesCount;
			}
		}
		public static decimal SalesTotal
		{
			get
			{
				return decSalesTotal;
			}
		}
		private void CalculateExtendedPrice()
		{
			decExtendedPrice = intQuantity * decPrice;
		}
		private void AddToTotals()
		{
			decSalesTotal += decExtendedPrice;
			intSalesCount++;
		}
	}
}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		private BookSale booksaleobject;

		private void calculatebtn_Click(object sender, System.EventArgs e)
		{
			try
			{
				booksaleobject = new BookSale(titletxt.Text, int.Parse(quantitytxt.Text), decimal.Parse(pricetxt.Text));
				extendtxt.Text = booksaleobject.ExtendedPrice.ToString("N");
			}
			catch
			{
				MessageBox.Show("Error in quantity or price field","Invalid data");
			}
		}

		private void clearbtn_Click(object sender, System.EventArgs e)
		{
			titletxt.Clear();
			quantitytxt.Clear();
			pricetxt.Clear();
			extendtxt.Clear();
			titletxt.Focus();
		}

		private void savebtn_Click(object sender, EventArgs e)
		{
			FileStream bookFileStream = new FileStream("Books.txt", FileMode.Create);
			BinaryFormatter bookFormatter = new BinaryFormatter();
			
			bookFormatter.Serialize(bookFileStream, booksaleobject);
			bookFileStream.Close();
		   
		}

		private void retrievebtn_Click(object sender, System.EventArgs e)
		{
			BinaryFormatter bookFormatter = new BinaryFormatter();
			FileStream bookFileStream;

			try
			{
				bookFileStream = new FileStream("Books.txt", FileMode.Open);
				booksaleobject = (BookSale)bookFormatter.Deserialize(bookFileStream);
				bookFileStream.Close();

				titletxt.Text = booksaleobject.Title;
				quantitytxt.Text = booksaleobject.Quantity.ToString();
				pricetxt.Text = booksaleobject.Price.ToString("N");
				extendtxt.Text = booksaleobject.ExtendedPrice.ToString("C");
			}
			catch
			{
				MessageBox.Show("No Saved object found", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}

	}
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1