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.OleDb;
namespace Rice
{
public partial class StockEntry : Form
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=E:\\Rice\\Rice.mdb");
public StockEntry()
{
InitializeComponent();
}
private void Idgen()
{
int id = 0;
String str1 = "Select MAX(Riceid) from StockEntry";
OleDbCommand cmd = new OleDbCommand(str1, con);
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.IsDBNull(0) == false)
{
id = dr.GetInt32(0) + 1;
}
else
{
id = 1;
}
}
cbxRiceId.Text = id.ToString();
dr.Close();
con.Close();
}
private void Idshow()
{
cbxRiceId.Items.Clear();
cbxRiceId.Text = "Select";
String str = "Select * from StockEntry";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.IsDBNull(0) == false)
{
cbxRiceId.Items.Add(dr.GetValue(0).ToString());
}
}
dr.Close();
con.Close();
}
private void TextClear()
{
txtRname.Text = "";
cbxRunit .Text = "";
txtRUPkg.Text = "";
cbxRiceId.Text = "Select";
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
TextClear();
Idgen();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (cbxRiceId.Text == "Select")
{
MessageBox.Show("Select The Rice Id");
cbxRiceId.Focus();
return;
}
else
{
String str = "Select * from StockEntry where Riceid=" + cbxRiceId.Text + "";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.IsDBNull(0) == false)
{
txtRname.Text = dr.GetValue(1).ToString();
cbxRunit .Text = dr.GetValue(2).ToString();
txtRUPkg.Text = dr.GetValue(3).ToString();
}
}
dr.Close();
con.Close();
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
String str1 = "Select * from StockEntry";
OleDbCommand cmd1 = new OleDbCommand(str1, con);
con.Open();
OleDbDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
if (dr1.IsDBNull(0) == false)
{
string rid = dr1.GetValue(0).ToString();
if (cbxRiceId.Text == rid)
{
MessageBox.Show("This Number Already Exist");
cbxRiceId.Focus();
con.Close();
goto l1;
}
}
}
con.Close();
if (cbxRiceId.Text == "Select")
{
MessageBox.Show("Select The Rice Id");
cbxRiceId.Focus();
return;
}
if (txtRname.Text == "")
{
MessageBox.Show("Enter The Brand Name");
txtRname.Focus();
return;
}
if (cbxRunit .Text == "")
{
MessageBox.Show("Enter The No Unit Rice");
cbxRunit.Focus();
return;
}
if (txtRUPkg.Text == "")
{
MessageBox.Show("Enter The Rice Unit Per Kg");
txtRUPkg.Focus();
return;
}
String str = "Insert into StockEntry(Riceid,Ricename,Riceunit,Ricepkg)Values(" + cbxRiceId.Text + ",'" + txtRname.Text + "'," + cbxRunit.Text + "," + txtRUPkg .Text + ")";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Saved");
Idshow();
TextClear();
l1:
dr1.Close();
}
private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{
if (cbxRiceId.Text == "Select")
{
MessageBox.Show("Select The Rice Id");
cbxRiceId.Focus();
return;
}
else
{
String str = "Update StockEntry SET Ricename='" + txtRname.Text + "',Riceunit=" + cbxRunit.Text + ",Ricepkg=" + txtRUPkg.Text + " where Riceid=" + cbxRiceId.Text + "";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Updated");
Idshow();
TextClear();
}
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (cbxRiceId.Text == "Select")
{
MessageBox.Show("Select The Rice Id");
cbxRiceId.Focus();
return;
}
else
{
String str = "Delete * from StockEntry where Riceid=" + cbxRiceId.Text + "";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Deleted");
Idshow();
TextClear();
}
}
private void StockEntry_Load(object sender, EventArgs e)
{
cbxRiceId.Text = "Select";
String str = "Select * from StockEntry";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.IsDBNull(0) == false)
{
cbxRiceId.Items.Add(dr.GetValue(0).ToString());
}
}
dr.Close();
con.Close();
}
private void OnlyNumberWithDot(object sender, KeyPressEventArgs e)
{
if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != 8) && (e.KeyChar != 46))
{
e.Handled = true;
}
}
private void OnlyNumber(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && (e.KeyChar != 8))
{
e.Handled = true;
}
}
private void cbxRunit_KeyPress(object sender, KeyPressEventArgs e)
{
OnlyNumberWithDot(sender, e);
}
private void txtRUPkg_KeyPress(object sender, KeyPressEventArgs e)
{
OnlyNumberWithDot(sender, e);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.ExitThread();
}
private void txtRUPkg_TextChanged(object sender, EventArgs e)
{
}
private void cbxRunit_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxRunit.Text != "")
{
txtRUPkg.Text = ((Convert.ToInt32(cbxRunit.Text) * 25).ToString());
}
}
}
}
No comments:
Post a Comment