我正在使用Visual Studio 2010和SQL SERVER管理工作室2008 R2
我有一张表格参考表格预订
CREATE TABLE [Order]
(OrderNumber int IDENTITY (1,1) NOT NULL PRIMARY KEY,
CustNumber int FOREIGN KEY REFERENCES Customer(CustNumber),
EmpNumber int FOREIGN KEY REFERENCES Employee(EmpNumber),
OrderDate date,
Deliver char,
StatusID int FOREIGN KEY REFERENCES [Status] (StatusID),
BookingNumber int FOREIGN KEY REFERENCES Booking(BookingNumber),
TotalAmount float,
OutstandingAmount float);
GO
CREATE TABLE Booking
(BookingNumber int IDENTITY (1,1) NOT NULL PRIMARY KEY,
BookingDate datetime,
BookingTime datetime,
StatusID int FOREIGN KEY REFERENCES [Status](StatusID),
CustNumber int FOREIGN KEY REFERENCES Customer(CustNumber),
EmpNumber int FOREIGN KEY REFERENCES Employee(EmpNumber)
);
GO
我还有一个继承orderClass
的Order类和ClassList OrderList public class Order
{
public int OrderNumber { set; get;}
public int CustNumber { set; get;}
public int EmpNumber { set; get;}
public DateTime OrderDate { set; get;}
public char Deliver { set; get;}
public int StatusID { set; get;}
public int BookingNumber { set; get; }
public float TotalAmount { set; get;}
public float OutstandingAmount { set; get; }
public Order()
{
OrderNumber = 0;
CustNumber = 0;
EmpNumber = 0;
OrderDate = DateTime.Today;
Deliver = ' ';
StatusID = 0;
BookingNumber = 0;
TotalAmount = 0;
OutstandingAmount = 0;
}
public Order(int a, int b, int c, DateTime d, char e,int f, int g, float h, float i)
{
OrderNumber = a;
CustNumber = b;
EmpNumber = c;
OrderDate = d;
Deliver = e;
StatusID = f;
BookingNumber = g;
TotalAmount = h;
OutstandingAmount = i;
}
namespace EAMS_BusinessTier
{
public class OrderList : System.ComponentModel.BindingList <Order>
{private EAMS_DBDataSetTableAdapters.OrderTableAdapter OrderTA = new EAMS_DBDataSetTableAdapters.OrderTableAdapter();
private EAMS_DBDataSet ds = new EAMS_DBDataSet();
/////////// Read OrderList //////////////////////////
public OrderList()
{
OrderTA.FillbyOrder(ds.Order);
foreach (EAMS_DBDataSet.OrderRow r in ds.Order.Rows)
{
Order m = new Order(r.OrderNumber,r.CustNumber,r.EmpNumber,r.OrderDate,Convert.ToChar(r.Deliver),r.StatusID,r.BookingNumber,Convert.ToInt32(r.TotalAmount),Convert.ToInt32(r.OutstandingAmount));
this.Add(m);
}
}
public OrderList(int id)
{
OrderTA.FillbyOrder(ds.Order);
foreach (EAMS_DBDataSet.OrderRow r in ds.Order.Rows)
{
if (id == r.OrderNumber)
{
Order m = new Order(r.OrderNumber, r.CustNumber, r.EmpNumber, r.OrderDate, Convert.ToChar(r.Deliver), r.StatusID, r.BookingNumber, Convert.ToInt32(r.TotalAmount), Convert.ToInt32(r.OutstandingAmount));
this.Add(m);
}
}
}
/////END////
///////////////////// get OrderList ////////////////////////
public OrderList getOrderList()
{
return this;
}
public OrderList getOrderList(int id)
{
OrderList t = new OrderList(id);
return t;
}
////END////
///////////////////// Save Order ////////////////////////
public void SaveOrder()
{
EAMS_DBDataSet.BankDataTable temp = new EAMS_DBDataSet.BankDataTable();
foreach (Order r in this)
{
EAMS_DBDataSet.OrderRow nr = ds.Order.NewOrderRow();
nr.OrderNumber = r.OrderNumber;
nr.CustNumber = r.CustNumber;
nr.EmpNumber = r.EmpNumber;
nr.OrderDate = r.OrderDate;
nr.Deliver = Convert.ToString(r.Deliver);
nr.StatusID = r.StatusID;
nr.BookingNumber = r.BookingNumber;
nr.TotalAmount = r.TotalAmount;
nr.OutstandingAmount = r.OutstandingAmount;
temp.Rows.Add(nr.ItemArray);
}
ds.Order.Merge(temp, false);
foreach (EAMS_DBDataSet.OrderRow r in ds.Order.Rows)
if (r.RowState == DataRowState.Unchanged)
{
r.Delete();
}
OrderTA.Update(ds.Order);
}
////END////
//////////////// Insert Order //////////////////////////////
public void InsertOrder(int a, int b, int c, DateTime d, char e, int f,int g,float h,float i)
{
Order m = new Order(a, b, c, d, e, f, g, h, i);
this.Add(m);
}
////END////
/////////////////Delete Order /////////////////////////
public void DeleteOrder(int del)
{
int i = 0;
int d = -1;
foreach (Order r in this)
{
if (r.OrderNumber == del)
{
d = i;
}
i++;
}
if (d > -1)
{
this.RemoveAt(d);
}
}
////END////
/////////////// Update Order ///////////////////////
public void UpdateOrder(int a, int b, int c, DateTime d, char e, int f, int g, float h, float i)
{
Order r = new Order(a, b, c, d, e, f, g, h, i);
foreach (Order nr in this)
{
if (r.OrderNumber == nr.OrderNumber)
{
nr.OrderNumber = r.OrderNumber;
nr.CustNumber = r.CustNumber;
nr.OrderDate = r.OrderDate;
nr.Deliver = r.Deliver;
nr.StatusID = r.StatusID;
nr.BookingNumber = r.BookingNumber;
nr.TotalAmount = r.TotalAmount;
nr.OutstandingAmount = r.OutstandingAmount;
break;
}
}
}
////END////
这必须起作用的方式并非每个订单都有预订编号,因此订单表中的预订编号可以为空值。
当我在构建业务层并连接到OrderList Data对象后尝试在表示层中加载OrderList时,会发生错误。这是我用来从表示层加载OrderList的代码
using EAMS_BusinessTier;
namespace DXApplication2
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
internal OrderList OrderLis = new OrderList();
private void Form5_Load(object sender, EventArgs e)
{
orderListBindingSource.DataSource = OrderLis;
}
当我尝试在运行时加载OrderList时,我继续收到此错误 &#34;列&#39; BookingNumber&#39;的值在表格&#39;订单&#39;是DBNull。&#34;我该如何处理这个错误?
提前谢谢
答案 0 :(得分:0)
将Order.BookingNumber的类型从int更改为Nullable<int>
。或者只是int?
。
验证您的EDM。表Order中的BookingNumber字段必须允许空值。
更改
Order m = new Order(r.OrderNumber,r.CustNumber,r.EmpNumber,r.OrderDate,Convert.ToChar(r.Deliver),r.StatusID,r.BookingNumber,Convert.ToInt32(r.TotalAmount),Convert.ToInt32(r.OutstandingAmount));
为:
Order m = new Order(r.OrderNumber,r.CustNumber,r.EmpNumber,r.OrderDate,Convert.ToChar(r.Deliver),r.StatusID,(int?)r.BookingNumber,Convert.ToInt32(r.TotalAmount),Convert.ToInt32(r.OutstandingAmount));