Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace IT_Store_Management
- {
- public partial class Issue : Form
- {
- protected String ConString = Properties.Settings.Default.DefaultDB; //connection string of default database
- public Issue()
- {
- InitializeComponent();
- }
- private void Received_Load(object sender, EventArgs e)
- {
- ReceivedDate.CustomFormat = "ddd dd MMM yyyy";
- generateSerialNo();
- getVendorList();
- getItemList();
- }
- private void generateSerialNo()
- {
- try
- {
- SqlConnection con = new SqlConnection(this.ConString);
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandText = "SELECT TOP (1) [SL] + 1 AS [Serial No] FROM [it_store].[dbo].[Received_Items] ORDER BY [SL] DESC";
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- if (rdr.Read())
- {
- SlNoTextBox.Text = rdr["Serial No"].ToString();
- }
- con.Close();
- }
- catch (SqlException exc)
- {
- MessageBox.Show("Error: " + exc.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void getItemList()
- {
- try {
- SqlConnection con = new SqlConnection(this.ConString);
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandText = "SELECT [ID],[Name] FROM [it_store].[dbo].[Items] WHERE [Status] = @status ";
- cmd.Parameters.AddWithValue("@status", 1);
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- ItemsComboBox.Items.Add(new ComboBoxItem(rdr["Name"].ToString(), rdr["ID"].ToString()));
- }
- con.Close();
- }
- catch(SqlException exc)
- {
- MessageBox.Show("Error: " + exc.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void getVendorList()
- {
- try {
- SqlConnection con = new SqlConnection(this.ConString);
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandText = "SELECT [ID],[Name]FROM [it_store].[dbo].[Departments] WHERE [Status] = @status ";
- cmd.Parameters.AddWithValue("@status", 1);
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- VendorComboBox.Items.Add(new ComboBoxItem(rdr["Name"].ToString(), rdr["ID"].ToString()));
- }
- con.Close();
- }
- catch (SqlException exc)
- {
- MessageBox.Show("Error: " + exc.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void SaveButton_Click(object sender, EventArgs e)
- {
- //if form validation passed
- if (IsValid())
- {
- // form data save successful
- if (SaveReceivedItems())
- {
- MessageBox.Show("Issue information saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
- Form _Issue = new Issue();
- _Issue.Show();
- this.Close();
- }
- }
- }
- private bool SaveReceivedItems()
- {
- try
- {
- SqlConnection con = new SqlConnection(this.ConString);
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandText = "INSERT INTO [dbo].[Issued_Items]([Received Date],[Memo No],[DepartmentID],[ItemID],[Quantity],[Remarks],[Balance],[Status]) VALUES( @reeivedate, @memono, @vendorid, @itemid, @qty, @remarks, @balance , @status )";
- cmd.Parameters.AddWithValue("@reeivedate", ReceivedDate.Value);
- cmd.Parameters.AddWithValue("@memono", MemoNoTextBox.Text);
- cmd.Parameters.AddWithValue("@vendorid", int.Parse(((ComboBoxItem)VendorComboBox.SelectedItem).HiddenValue));
- cmd.Parameters.AddWithValue("@itemid", int.Parse(((ComboBoxItem)ItemsComboBox.SelectedItem).HiddenValue));
- cmd.Parameters.AddWithValue("@qty", QuantityTextBox.Text);
- cmd.Parameters.AddWithValue("@remarks", (RemarksRickTextBox.Text.Trim() != String.Empty) ? RemarksRickTextBox.Text.Trim() : (object) DBNull.Value); //If Remarks empty, assign value to DBNull.value
- cmd.Parameters.AddWithValue("@balance", (int.Parse(BfTextBox.Text.Trim()) - int.Parse(QuantityTextBox.Text.Trim())));
- cmd.Parameters.AddWithValue("@status", 1);
- con.Open();
- int a = cmd.ExecuteNonQuery();
- // check insertion unsuccessful
- if (a < 0)
- {
- return false;
- }
- }
- catch (SqlException exc)
- {
- MessageBox.Show(exc.Message);
- return false;
- }
- return true;
- }
- private bool IsValid()
- {
- if (ItemsComboBox.SelectedIndex == -1)
- {
- MessageBox.Show("An item must be selected", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- if (MemoNoTextBox.Text.Trim() == String.Empty)
- {
- MessageBox.Show("Memo No. field is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- if (VendorComboBox.SelectedIndex == -1)
- {
- MessageBox.Show("A department must be selected", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- if (QuantityTextBox.Text.Trim() == String.Empty)
- {
- MessageBox.Show("Department field is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- if (int.Parse(QuantityTextBox.Text.Trim()) > int.Parse(BfTextBox.Text.Trim()))
- {
- MessageBox.Show("Quantity should be less than or equal to BF", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- return true;
- }
- private void ItemsComboBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (ItemsComboBox.SelectedIndex != -1)
- {
- getBf();
- CodeTextBox.Text = ((ComboBoxItem)ItemsComboBox.SelectedItem).HiddenValue;
- }
- }
- private void getBf()
- {
- try
- {
- SqlConnection con = new SqlConnection(this.ConString);
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandText = "SELECT ISNULL((SELECT SUM(Received_Items.Quantity) FROM Received_Items WHERE Received_Items.ItemID = @itemid ),0) - ISNULL((SELECT SUM(Issued_Items.Quantity) FROM Issued_Items WHERE Issued_Items.ItemID = @itemid ),0) AS [Total Item]";
- cmd.Parameters.AddWithValue("@itemid", int.Parse(((ComboBoxItem)ItemsComboBox.SelectedItem).HiddenValue));
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- if (rdr.Read())
- {
- BfTextBox.Text = (Convert.IsDBNull(rdr["Total Item"])) ? "0" : rdr["Total Item"].ToString();
- BalanceTextBox.Text = (Convert.IsDBNull(rdr["Total Item"])) ? "0" : rdr["Total Item"].ToString();
- }
- con.Close();
- }
- catch(SqlException exc)
- {
- MessageBox.Show("Error: " + exc.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void QuantityTextBox_KeyUp(object sender, KeyEventArgs e)
- {
- //if item selected
- if (ItemsComboBox.SelectedIndex != -1)
- {
- int tmpnum;
- bool isSuccess;
- isSuccess = int.TryParse(QuantityTextBox.Text.Trim(), out tmpnum);
- // if number is valid
- if (isSuccess)
- {
- BalanceTextBox.Text = ( int.Parse(BfTextBox.Text.Trim()) - int.Parse(QuantityTextBox.Text.Trim()) ).ToString();
- }
- else
- {
- MessageBox.Show("Invalid number \"" + QuantityTextBox.Text.Trim() + "\"", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- QuantityTextBox.Focus();
- }
- }
- else
- {
- MessageBox.Show("Before entering quantity an item must be selected", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- ItemsComboBox.Focus();
- }
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- }
- private void ReceivedDate_ValueChanged(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement