Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > ช่วยหน่อยค่ะ เพิ่มข้อมูลไม่ได้ Error Index or primary key cannot contain a Null value.



 

ช่วยหน่อยค่ะ เพิ่มข้อมูลไม่ได้ Error Index or primary key cannot contain a Null value.

 



Topic : 068333

Guest




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)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-10-22 12:25:37 By : alo View : 2457 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Quote:
Error Index or primary key cannot contain a Null value.


Error ก็บอกแล้วน่ะครับ PK ไม่สามารถรับค่าว่างได้ครับ คุณน่าจะต้องกำหนด PK ให้เป็น Auto Number ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-10-22 20:19:42 By : webmaster
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยหน่อยค่ะ เพิ่มข้อมูลไม่ได้ Error Index or primary key cannot contain a Null value.
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่