|
|
|
ช่วยหน่อยค่ะ เพิ่มข้อมูลไม่ได้ Error Index or primary key cannot contain a Null value. |
|
|
|
|
|
|
|
Code (C#)
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 Datateacher
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string strConn;
OleDbConnection Conn = new OleDbConnection();
OleDbDataAdapter da;
DataSet ds = new DataSet();
BindingSource bs = new BindingSource();
bool IsFind = false;
private void Form2_Load(object sender, EventArgs e)
{
strConn = "Provider=Microsoft.JET.OLEDB.4.0;data source=D:/aa/Hotmail_2/Datateacher/Datateacher/db_teach.mdb";
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
string sqlclass = "";
sqlclass = "select * from tb_class";
da = new OleDbDataAdapter(sqlclass, Conn);
if (ds.Tables["tb_class"] != null)
ds.Tables["tb_class"].Clear();
da.Fill(ds, "tb_class");
if (ds.Tables["tb_class"].Rows.Count > 0)
{
cmbclass.DataSource = ds.Tables["tb_class"];
cmbclass.ValueMember = "id_class";
cmbclass.DisplayMember = "name_class";
}
ClearAll();
ShowAll();
FormatAllTeacher();
}
private void ShowAll()
{
string sqlteach;
sqlteach= "select tb_teach.open_program,tb_teach.year_program,tb_teach.id_teacher,tb_program.id_program,tb_program.name_program,tb_program.credit_program,tb_program.class_program from tb_teach inner join tb_program on (tb_teach.id_program = tb_program.id_program) where tb_teach.id_program ";
if (IsFind == true)
{
ds.Tables["teachpro"].Clear();
}
da = new OleDbDataAdapter(sqlteach, Conn);
da.Fill(ds, "teachpro");
if (ds.Tables["teachpro"].Rows.Count != 0)
{
IsFind = true;
dgvshowall2.ReadOnly = true;
dgvshowall2.DataSource = ds.Tables["teachpro"];
}
else
{
IsFind = false;
}
}
private void ClearAll()
{
mtbopen.Clear();
mtbyear.Clear();
mtbidteacher.Clear();
mtbidprogram.Clear();
txtnameprogram.Clear();
mtbcredit.Clear();
cmbclass.Text = "เลือก";
}
private void FormatAllTeacher()
{
DataGridViewCellStyle cs = new DataGridViewCellStyle();
cs.Font = new Font("Angsana New", 12, FontStyle.Bold);
dgvshowall2.ColumnHeadersDefaultCellStyle = cs;
dgvshowall2.Columns[0].HeaderText = "ภาคเรียนที่";
dgvshowall2.Columns[1].HeaderText = "ปี";
dgvshowall2.Columns[2].HeaderText = "รหัสอาจารย์";
dgvshowall2.Columns[3].HeaderText = "รหัสวิชา";
dgvshowall2.Columns[4].HeaderText = "ชื่อวิชา";
dgvshowall2.Columns[5].HeaderText = "หน่วยกิต";
dgvshowall2.Columns[6].HeaderText = "ประเภทวิชา";
dgvshowall2.Columns[0].Width = 80;
dgvshowall2.Columns[1].Width = 80;
dgvshowall2.Columns[2].Width = 100;
dgvshowall2.Columns[3].Width = 100;
dgvshowall2.Columns[4].Width = 180;
dgvshowall2.Columns[5].Width = 80;
dgvshowall2.Columns[6].Width = 100;
}
private void btnclear_Click(object sender, EventArgs e)
{
ClearAll();
}
private void btnadd_Click(object sender, EventArgs e)
{
if (mtbidprogram.Text == "")
{
MessageBox.Show("กรุณากรอกรหัสวิชาของคุณด้วยค่ะ !", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
mtbidprogram.Focus();
mtbidprogram.SelectAll();
return;
}
else if (txtnameprogram.Text == "")
{
MessageBox.Show("กรุณากรอกชื่อวิชาของคุณด้วยค่ะ !", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtnameprogram.Focus();
txtnameprogram.SelectAll();
return;
}
else if (mtbcredit.Text == "")
{
MessageBox.Show("กรุณากรอกหน่วยกิตวิชาของคุณด้วยค่ะ !", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
mtbcredit.Focus();
mtbcredit.SelectAll();
return;
}
else if (cmbclass.Text == "เลือก")
{
MessageBox.Show("กรุณาเลือกประเภทวิชาของคุณด้วยค่ะ !", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
cmbclass.Focus();
cmbclass.SelectAll();
return;
}
string sqladd;
OleDbCommand comadd = new OleDbCommand();
sqladd = "INSERT INTO tb_teach (open_program,year_program,id_teacher) values ('" + mtbopen.Text + "','" + mtbyear.Text + "','" + mtbidteacher.Text + "')";
comadd.CommandType = CommandType.Text;
comadd.CommandText = sqladd;
comadd.Connection = Conn;
comadd.ExecuteNonQuery(); // Erorr ตรงนี้ค่ะ
string sqlAdd;
OleDbCommand comAdd = new OleDbCommand();
if (MessageBox.Show("คุณต้องการเพิ่มข้อมูลใหม่ใช่หรือไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
sqlAdd = "INSERT INTO tb_program (id_program,name_program,credit_program,class_program) values ('" + mtbidprogram.Text + "','" + txtnameprogram.Text + "','" + mtbcredit.Text + "','" + cmbclass.Text + "')";
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
comAdd.CommandType = CommandType.Text;
comAdd.CommandText = sqlAdd;
comAdd.Connection = Conn;
comAdd.ExecuteNonQuery();
MessageBox.Show("เพื่มข้อมูลเรัยบร้อยแล้ว", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearAll();
ShowAll();
}
}
private void btnedit_Click(object sender, EventArgs e)
{
if (mtbidprogram.Text == "")
{
MessageBox.Show("กรุณากรอกข้อมูลด้วยค่ะ !", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
mtbidprogram.Focus();
mtbidprogram.SelectAll();
return;
}
else if (txtnameprogram.Text == "")
{
MessageBox.Show("กรุณากรอกข้อมูลด้วยค่ะ !", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtnameprogram.Focus();
txtnameprogram.SelectAll();
return;
}
else if (mtbcredit.Text == "")
{
MessageBox.Show("กรุณากรอกข้อมูลด้วยค่ะ !", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
mtbcredit.Focus();
mtbcredit.SelectAll();
return;
}
else if (cmbclass.Text == "เลือก")
{
MessageBox.Show("กรุณากรอกข้อมูลด้วยค่ะ !", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
cmbclass.Focus();
cmbclass.SelectAll();
return;
}
string sqlEdit;
OleDbCommand comEdit = new OleDbCommand();
if (MessageBox.Show("คุณต้องการแก้ไขข้อมูลหรือไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
sqlEdit = "update tb_program set name_program = '" + txtnameprogram.Text + "',credit_program = '" + mtbcredit.Text + "',class_program = '" + cmbclass.Text + "' where id_program = '" + mtbidprogram.Text + "'";
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
comEdit.CommandType = CommandType.Text;
comEdit.CommandText = sqlEdit;
comEdit.Connection = Conn;
comEdit.ExecuteNonQuery();
MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearAll();
ShowAll();
}
}
private void btndelete_Click(object sender, EventArgs e)
{
string sqlDelete;
OleDbCommand comDelete = new OleDbCommand();
if (MessageBox.Show("คุณต้องการลบข้อมูลหรือไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
sqlDelete = "delete From tb_program where id_program = '" + mtbidprogram.Text + "' ";
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
comDelete.CommandType = CommandType.Text;
comDelete.CommandText = sqlDelete;
comDelete.Connection = Conn;
comDelete.ExecuteNonQuery();
MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearAll();
ShowAll();
}
}
private void txtSearch_Click(object sender, EventArgs e)
{
Form5 frmprogramall = new Form5();
frmprogramall.Show();
this.Hide();
}
private void dgvshowall2_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == dgvshowall2.Rows.Count - 1)
{
return;
}
if (e.RowIndex == -1)
{
return;
}
mtbopen.Text = dgvshowall2.Rows[e.RowIndex].Cells[0].Value.ToString();
mtbyear.Text = dgvshowall2.Rows[e.RowIndex].Cells[1].Value.ToString();
mtbidteacher.Text = dgvshowall2.Rows[e.RowIndex].Cells[2].Value.ToString();
mtbidprogram.Text = dgvshowall2.Rows[e.RowIndex].Cells[3].Value.ToString();
txtnameprogram.Text = dgvshowall2.Rows[e.RowIndex].Cells[4].Value.ToString();
mtbcredit.Text = dgvshowall2.Rows[e.RowIndex].Cells[5].Value.ToString();
cmbclass.Text = dgvshowall2.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}
}
Tag : .NET, Ms Access, C#, VS 2008 (.NET 3.x)
|
|
|
|
|
|
Date :
2011-10-22 12:25:37 |
By :
alo |
View :
2507 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|