ช่วยหน่อยคับ มันขึ้น Can't perform Create, Update, or Delete operations on 'Table(Expenditure)' because it has no primary key.
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Data.Linq;
using System.Transactions;
using System.Text;
using System.Windows.Forms;
namespace Laundry_and_Cleaning_Program
{
public partial class InsertExpenditure : Form
{
public InsertExpenditure()
{
InitializeComponent();
}
dbExpenditureDataContext db = new dbExpenditureDataContext();
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
}
private void InsertExpenditure_Load(object sender, EventArgs e)
{
ShowData();
FormatDgvExpenditure();
}
private void ShowData()
{
var ET = from et in db.Expenditures select et;
dgvExpenditureList.DataSource = ET.ToList();
}
private void FormatDgvExpenditure()
{
if (dgvExpenditureList.RowCount > 0)
{
dgvExpenditureList.Columns[0].HeaderText = "ลำดับ";
dgvExpenditureList.Columns[1].HeaderText = "วันเดือนปี";
dgvExpenditureList.Columns[2].HeaderText = "รายการ";
dgvExpenditureList.Columns[3].HeaderText = "จำนวนรายจ่าย";
dgvExpenditureList.Columns[0].Width = 35;
dgvExpenditureList.Columns[1].Width = 80;
dgvExpenditureList.Columns[2].Width = 148;
dgvExpenditureList.Columns[3].Width = 105;
}
}
private void ClearAllData()
{
ODET.Text="";
ExpenditureList.Text = "";
MoneyExpenditure.Text = "";
dtp.Value = DateTime.Now;
}
private bool CheckExpenditureData()
{
if((ExpenditureList.Text.Trim()=="") || (MoneyExpenditure.Text.Trim()==""))
{
MessageBox.Show("กรุณาป้อนข้อมูลให้ครบ !!!","ข้อผิดพลาด");
ExpenditureList.Focus();
MoneyExpenditure.Focus();
ExpenditureList.SelectAll();
MoneyExpenditure.SelectAll();
return false;
}
else{
return true;
}
}
private void dtp_ValueChanged(object sender, EventArgs e)
{
dtp.Value = DateTime.Now;
}
private void dgvExpenditureList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
{
return;
}
ODET.Text = dgvExpenditureList.Rows[e.RowIndex].Cells["ODET"].Value.ToString();
ExpenditureList.Text = dgvExpenditureList.Rows[e.RowIndex].Cells["Ed"].Value.ToString();
MoneyExpenditure.Text=dgvExpenditureList.Rows[e.RowIndex].Cells["AE"].Value.ToString();
ExpenditureList.Focus();
MoneyExpenditure.Focus();
ExpenditureList.SelectAll();
MoneyExpenditure.SelectAll();
}
private void AddExpenditure_Click_1(object sender, EventArgs e)
{
var result = CheckExpenditureData();
if (result == true)
{
Expenditure ep = new Expenditure();
ep.ED = ExpenditureList.Text;
ep.AE = MoneyExpenditure.Text;
ep.PD = dtp.Value.ToString();
if (MessageBox.Show("คุณต้องการเพิ่มรายจ่ายใหม่ ใช่หรือไม่?", "คำยืนยัน",
MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
using (TransactionScope ts = new TransactionScope())
{
db.Expenditures.InsertOnSubmit(ep);
db.SubmitChanges();
ts.Complete();
}
ClearAllData();
ShowData();
MessageBox.Show("เพิ่มรายจ่ายเรียบร้อยแล้ว", "ผลการทำงาน");
ExpenditureList.Focus();
MoneyExpenditure.Focus();
}
}
}
}
}
Tag : .NET, C#
Date :
2012-05-10 14:23:05
By :
เชิดชัย
View :
1075
Reply :
2
ผมเจอปัญหาเหมือนกันครับ
Date :
2012-08-08 20:46:35
By :
โต้ง
Load balance : Server 02