Monday, 13 January 2014

Small Inventory Software For Beginners In C#

Small Inventory Software For Beginners In C#




      ds = new InventoryDataSet();
            pta = new InventoryDataSetTableAdapters.ProductsTableAdapter();

            int id = GenerateBookID();
            ds.Products.Columns["ProductID"].AutoIncrement = true;
            ds.Products.Columns["ProductID"].AutoIncrementSeed = id;
            ds.Products.Columns["ProductID"].AutoIncrementStep = 1;

            PopulateID();
            txtProductID.DataBindings.Add("Text", ds.Products, "ProductID");
         
          
        }

        private void frmAddProduct_Load(object sender, EventArgs e)
        {
            dtpDate.MaxDate = DateTime.Today;
            dtpDate.Value = DateTime.Today;
        }

      private int GenerateBookID()
        {
            try
            {
                int id;
                if (pta.Connection.State == ConnectionState.Closed)
                {
                    pta.Connection.Open();
                }
                string cmd = "Select Count(ProductID) from products";
                pta.Adapter.SelectCommand = new SqlCommand(cmd, pta.Connection);
                int result = (int)pta.Adapter.SelectCommand.ExecuteScalar();
                if (result == 0)
                {
                    id = 700;
                }
                else
                {
                    cmd = "Select Max(ProductID) from products";
                    pta.Adapter.SelectCommand = new SqlCommand(cmd, pta.Connection);
                    id = (int)pta.Adapter.SelectCommand.ExecuteScalar() + 1;
                }
                return id;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return -1;
            }
        }
      

        private void PopulateID()
    {
        ds.Products.Clear();
        row = ds.Products.NewProductsRow();
        ds.Products.Rows.Add(row);
    }

private void ADDBTN_Click(object sender, EventArgs e)
        {
            try
            {
                row["ProductID"] = txtProductID.Text;
                row["Name"] = txtName.Text;
                row["Price"] = txtPrice.Text;
                row["PurchaseDate"] = dtpDate.Value;
                pta.Adapter.Update(ds.Products);
                MessageBox.Show("Product data Add SuccesFully");
                ClearText();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Product data not added");
                ds.RejectChanges();
            }
            }
 private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (!cancelling)
            {
                error = false;

                if (string.IsNullOrEmpty(txtName.Text))
                {
                    MessageBox.Show("Name cannot be empty");
                    error=true;
                }
                else
                {
                    IEnumerator OperandEnum = txtName.Text.GetEnumerator();
                    while (OperandEnum.MoveNext())
                    {
                        char chr = (char)OperandEnum.Current;
                        if (!char.IsLetter(chr))
                        {
                            error=true;
                            MessageBox.Show("Invalid Name","Error");
                            txtName.Text="";
                            break;
                        }
                    }
                }
                if (error)
                {
                    txtName.Focus();
                    e.Cancel=true;
                }
            }             
             }

        private void txtPrice_Validating(object sender, CancelEventArgs e)
        {
            if (!cancelling)
            {
                error = false;
                if (string.IsNullOrEmpty(txtPrice.Text))
                {
                    MessageBox.Show("Price cannot be empty");
                }
                else
                {
                    decimal price;
                    if (!Decimal.TryParse(txtPrice.Text, out price))
                    {
                        MessageBox.Show("Invalid Price");
                        txtPrice.Text = "";
                        error = true;
                    }
                }
                if (error)
                {
                    e.Cancel = true;
                }
            }

        }


1 comment: