Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

using

using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Data.OleDb;
System.Security.Cryptography;

namespace QLKho
{
public partial class frmnhap : Form
{
public frmnhap()
{
InitializeComponent();
tudongcapnhat();
}
String connstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + App
lication.StartupPath + "\\quanlykho.accdb;";
int i = 0, st, xoadong, sl;
String xoamahang;
String smaphieu;
private void label8_Click(object sender, EventArgs e)
{
}
void tudongcapnhat()
{
txtmahang.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtmahang.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection coll = new AutoCompleteStringCollection
();
String cpmahang = "SELECT * FROM danhmuchang";
OleDbConnection conn = new OleDbConnection(connstr);
OleDbCommand cmd = new OleDbCommand(cpmahang, conn);
OleDbDataReader olread;
conn.Open();
olread = cmd.ExecuteReader();
while (olread.Read())
{
coll.Add(olread.GetString(1));
}
txtmahang.AutoCompleteCustomSource = coll;
conn.Close();
}
private void diendulieu()
{
String comdulieu = "SELECT * FROM danhmuchang WHERE mahang='" + txtm
ahang.Text + "'";
OleDbConnection conn = new OleDbConnection(connstr);
OleDbCommand cmd = new OleDbCommand(comdulieu, conn);
OleDbDataReader olreader;
conn.Open();
olreader = cmd.ExecuteReader();
while (olreader.Read())
{

txtmahang.Text = olreader.GetString(1);
txttenhang.Text = olreader.GetString(2);
txtghichu.Text = olreader.GetString(4);
}
conn.Close();
txtsoluong.Focus();
}
private void btnauto_Click(object sender, EventArgs e)
{
diendulieu();
}
private void txtsoluong_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
e.Handled = true;
}
private Boolean kiemtrarong()
{
Boolean kq = false;
if (txtmahang.Text == "")
{
MessageBox.Show("B?n chua nh?p m hng");
txtmahang.Focus();
kq = true;
}
else if (txttenhang.Text == "")
{
MessageBox.Show("B?n chua nh?p tn hng hy click vo nt >> n?u b?n d di?n
thng tin m hng");
txttenhang.Focus();
kq = true;
}
else if (txtsoluong.Text == "")
{
MessageBox.Show("B?n chua nh?p so lu?ng");
txtsoluong.Focus();
kq = true;
}
return kq;
}
private void btnthem_Click(object sender, EventArgs e)
{
String ktmahang;
Boolean tontai = false;
for (int j = 0; j < dgvnhaphang.Rows.Count; j++)
{
ktmahang = Convert.ToString(dgvnhaphang.Rows[j].Cells["MaHang"].
Value);
if (txtmahang.Text == ktmahang)
{
tontai = true;
}
}
if (kiemtrarong() == false)
{
if (tontai == false)
{

DataGridViewRow row = new DataGridViewRow();


dgvnhaphang.Rows.Add(row);
dgvnhaphang.Rows[i].Cells["MaHang"].Value = txtmahang.Text;
dgvnhaphang.Rows[i].Cells["TenHang"].Value = txttenhang.Text
;
dgvnhaphang.Rows[i].Cells["SoLuong"].Value = txtsoluong.Text
;
dgvnhaphang.Rows[i].Cells["GhiChu"].Value = txtghichu.Text;
i++;
txtmahang.Text = "";
txttenhang.Text = "";
txtsoluong.Text = "";
txtghichu.Text = "";
txtmahang.Focus();
}
else
{
MessageBox.Show("M hng " + txtmahang.Text + " d du?c thm vo phi?u
xu?t. Vui lng ki?m tra l?i!", "L?i", MessageBoxButtons.OK, MessageBoxIcon.Error)
;
DialogResult dr = MessageBox.Show("B?n c mu?n xa d? li?u v?a n
h?p d? nh?p l?i khng", "L?i", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
txtmahang.Text = "";
txttenhang.Text = "";
txtsoluong.Text = "";
txtghichu.Text = "";
txtmahang.Focus();
}
}
}
diensothutu();
}
private void diensothutu()
{
st = 1;
for (int j = 0; j < dgvnhaphang.Rows.Count; j++)
{
dgvnhaphang.Rows[j].Cells["STT"].Value = st;
st++;
}
}
private void xoa()
{
DialogResult dr = MessageBox.Show("B?n th?t s? mu?n xa m hng " + xoamah
ang + " kh?i phi?u nh?p kho", "C?nh bo", MessageBoxButtons.YesNo, MessageBoxIcon.
Warning);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
dgvnhaphang.Rows.RemoveAt(xoadong);
diensothutu();
}
label11.Text = "";
}
private String getmd5(String txt)
{
String str = "";

Byte[] buffer = System.Text.Encoding.UTF8.GetBytes(txt);


MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
buffer = md5.ComputeHash(buffer);
foreach (Byte b in buffer)
{
str += b.ToString("X2");
}
return str;
}
private int laysoluonghangton(int j)
{
String kiemtra = "SELECT * FROM danhmuchang WHERE mahang='" + dgvnha
phang.Rows[j].Cells["MaHang"].Value + "';";
OleDbConnection conn = new OleDbConnection(connstr);
OleDbCommand cmd = new OleDbCommand(kiemtra, conn);
OleDbDataReader olreader;
conn.Open();
olreader = cmd.ExecuteReader();
while (olreader.Read())
{
sl = Convert.ToInt32(olreader.GetValue(3).ToString());
}
conn.Close();
return sl;
}
private void btnluu_Click(object sender, EventArgs e)
{
Random rand = new Random();
DateTime dt = DateTime.Now;
int randnum = rand.Next(0, 1000000);
String strtemp = Convert.ToString(dt) + Convert.ToString(randnum);
smaphieu = getmd5(strtemp);
// them du lieu vao phieu nhap
String themphieu = "INSERT INTO phieunhap (manhap, nguonnhap, ngaynh
ap, nguoigiao, diachia, lydonhap) VALUES ('" + smaphieu + "','" + txtnguonhap.Te
xt + "', '" + dtngaynhap.Text + "','" + txtnguoigiao.Text + "', '" + txtdiachi.T
ext + "', '" + txtlydonhap.Text + "');";
OleDbConnection conn = new OleDbConnection(connstr);
OleDbCommand cmd;
cmd = conn.CreateCommand();
cmd.CommandText = themphieu;
conn.Open();
cmd.ExecuteNonQuery();
for (int k = 0; k < dgvnhaphang.Rows.Count - 1; k++)
{
String mahangnhap = Convert.ToString(dgvnhaphang.Rows[k].Cells["
MaHang"].Value);
String tenhangnhap = Convert.ToString(dgvnhaphang.Rows[k].Cells[
"TenHang"].Value);
String soluonghangnhap = Convert.ToString(dgvnhaphang.Rows[k].Ce
lls["SoLuong"].Value);
String ghichuupdate = Convert.ToString(dgvnhaphang.Rows[k].Cells
["GhiChu"].Value);
String themchitietphieunhap = "INSERT INTO chitietphieunhap (map
hieunhap, mahangnhap, tenhangnhap, soluongnhap, ghichunhap) VALUES ('" + smaphie
u + "','" + mahangnhap + "', '" + tenhangnhap + "','" + soluonghangnhap + "', '"

+ ghichuupdate + "');";
cmd.CommandText = themchitietphieunhap;
cmd.ExecuteNonQuery();
int slupdate = laysoluonghangton(k) + Convert.ToInt32(soluonghan
gnhap);
String updatehanghoa = "UPDATE danhmuchang SET soluong='" + slup
date + "', ghichu='" + ghichuupdate + "' WHERE mahang='" + mahangnhap + "';";
cmd.CommandText = updatehanghoa;
cmd.ExecuteNonQuery();
}
conn.Close();
MessageBox.Show("Ghi d? li?u thnh cng");
}
private void dgvnhaphang_CellClick(object sender, DataGridViewCellEventA
rgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = dgvnhaphang.Rows[e.RowIndex];
xoadong = Convert.ToInt32(e.RowIndex);
xoamahang = row.Cells[1].Value.ToString();
label11.Text = xoamahang;
}
}
private void btnxoa_Click(object sender, EventArgs e)
{
xoa();
}
}
}

You might also like