Shalvin.Com                                                                                                                      Home

Point of Sale Prototype using DataReader



The following example demonstartes retrieving the product information based on the selection from the database, followed by option to enter number of units. On clicking the calculate button the amout will be calculated and displayed.
 
using System.Data.SqlClient;

SqlConnection cnn = new SqlConnection(@"Integrated Security=sspi;Initial Catalog=Northwind;Data Source=.\sqlexpress");
SqlCommand cmd;
SqlDataReader dr, dr1;
int intPrice;

private void Form1_Load(object sender, EventArgs e)
{
  cnn.Open();
  cmd = new SqlCommand("select * from Products", cnn);
  dr = cmd.ExecuteReader();
  while (dr.Read())
     cboProduct.Items.Add(dr["ProductName"]);

  dr.Close();
  cnn.Close();
}
private void cboProduct_SelectedIndexChanged(object sender, EventArgs e)
{
  cnn.Open();
  string strSql;
  strSql = "select * from Products where ProductName ='" + cboProduct.Text + "'";
  cmd = new SqlCommand(strSql, cnn);
  dr1 = cmd.ExecuteReader();
  while (dr1.Read())
  {
    txtPrice.Text = dr1["Price"].ToString(); 
    intPrice = Int32.Parse(dr1["Price"].ToString());
  }
  cboProduct.Enabled = false;
}
private void btnCalculate_Click(object sender, EventArgs e)
{
  int intAmount = intPrice * Int32.Parse(txtQuantity.Text);
  lblTotal.Text = intAmount.ToString();
}
}


Contact : shalvin@gmail.com