Any suggestions are appreciated; please do not write the code for me
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.Formatters.Binary;
using System.Runtime.Serialization;
namespace KjSProduct
{
public partial class KjS : Form
{
protected string sPath = "";
public KjS()
{
InitializeComponent();
//Get user name
String sUser = Environment.UserName;
//execution check
MessageBox.Show(sUser + ", Hello!");
//set path for file storage to user's documents and settings folder
sPath = "C:\\Documents and Settings\\" + sUser + "\\My Documents\\products.dat";
//execution check
MessageBox.Show("Your current directory is: " + sPath);
}
private void btnSave_Click(object sender, EventArgs e)
{
//get name from form
string sName = txtName.Text;
//execution check
MessageBox.Show(sName);
//create decimal price for later use
decimal dPrice;
try
{
//get price from the form, convert to decimal, on error set dPrice to 0
dPrice = Convert.ToDecimal(txtPrice.Text);
MessageBox.Show("dPrice is " + dPrice);
}
catch
{
dPrice = 0;
throw;
}
//call save method
Save(sName, dPrice);
}
private void Save(string sName, decimal dPrice)
{
//Create object to serialize
Product products = new Product(sName, dPrice);
//Create filestream object
FileStream fs;
fs = new FileStream("products.dat", FileMode.OpenOrCreate, FileAccess.Write);
BinaryFormatter binformat = new BinaryFormatter();
try
{
//Serialize
binformat.Serialize(fs, products);
}
catch (SerializationException e)
{
MessageBox.Show("Serialization failed. Reason: " + e.Message);
}
finally
{
//Close filestream
fs.Close();
MessageBox.Show("File saved");
}
}
private void Read()
{
FileStream fs; // = null;
try
{
//Create filestream object
fs = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryFormatter binFormat = new BinaryFormatter();
//Clear list box
lstProducts.Items.Clear();
//Deserialize previous object
Product products = (Product)binFormat.Deserialize(fs);
//Add items to list box
lstProducts.Items.Add(products.getsName() + " " + products.getdPrice());
fs.Close();
}
//In case of error show file is unavailable
catch
{
MessageBox.Show("That file is unavailable");
}
}
private void btnRead_Click(object sender, EventArgs e)
{
//Call Read method
Read();
}
}
}
I have a Product class for the gets n sets:
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.Formatters.Binary;
namespace KjSProduct
{
[Serializable]
//Class containing sName and dPrice, and gets and sets for them
class Product
{
private string sName;
private decimal dPrice;
public Product(string sName, decimal dPrice)
{
this.sName = sName;
this.dPrice = dPrice;
}
public void setsName(string sName)
{
this.sName = sName;
}
public string getsName()
{
return sName;
}
public void setdPrice(decimal dPrice)
{
this.dPrice = dPrice;
}
public decimal getdPrice()
{
return dPrice;
}
}
}
and of course main:
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.Formatters.Binary;
namespace KjSProduct
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new KjS());
}
}
}
I've looked at tutorials and tried several different examples of how to handle this, I am stumped.
Thanks for your assistance.
Kevin

New Topic/Question
Reply




MultiQuote





|