C# 我得到一个错误,说“对象引用未设置为对象的实例。”
我这里有麻烦。我在windows_加载事件中添加了它。代码如下:C# 我得到一个错误,说“对象引用未设置为对象的实例。”,c#,wpf,sql-server-ce,C#,Wpf,Sql Server Ce,我这里有麻烦。我在windows_加载事件中添加了它。代码如下: con.Open(); int i = 0, j = 0; string[] productCode = null; string[] productName = null; int[] quantity =null; float[] totalPrice = null; float[] totalTax = null; int orderID = 0; SqlCeCommand com5 = new SqlCeCommand(
con.Open();
int i = 0, j = 0;
string[] productCode = null;
string[] productName = null;
int[] quantity =null;
float[] totalPrice = null;
float[] totalTax = null;
int orderID = 0;
SqlCeCommand com5 = new SqlCeCommand("SELECT MAX(OrderID) AS Expr1 FROM Order_Details", con);
orderID = (int)com5.ExecuteScalar();
SqlCeCommand com1 = new SqlCeCommand("SELECT ProductCode, Quantity FROM Order_Products WHERE OrderID = ('"+ orderID.ToString() +"')", con);
SqlCeDataReader dr1 = com1.ExecuteReader();
while (dr1.Read())
{
productCode[i] = (string)dr1[0]; // Exception is here
quantity[i] = (int)dr1[1];
SqlCeCommand com3 = new SqlCeCommand("SELECT ProductName FROM Products_Master WHERE ProductCode = '" + productCode[i] + "'", con);
productName[i] = (string)com3.ExecuteScalar();
i++;
}
SqlCeCommand com2 = new SqlCeCommand("SELECT TotalPrice, TotalTax FROM Order_Details WHERE OrderID = (SELECT MAX(OrderID) AS Expr1 FROM Order_Details)", con);
SqlCeDataReader dr2 = com1.ExecuteReader();
while (dr2.Read())
{
totalPrice[j] = (float)dr2[0];
totalTax[j] = (float)dr2[1];
j++;
}
for (int k = 1; k <= i; k++)
{
List<Product> Products = new List<Product>();
Product p = new Product
{
ID = k,
ProductName = productName[k],
Quantity = quantity[k],
Tax = totalTax[k],
Total = totalPrice[k]
};
dgrdInvoice.Items.Add(p); // add a row
}
con.Close();
有人能为我找到解决这个问题的办法吗?我试图将两个数据库中的值添加到一个DataGrid中
编辑:
实际上,SQL查询中有一些内容:
该数据类型对于布尔表达式无效。[已知数据类型=int,未知数据类型=nvarchar]
您应该声明productName[]数组长度意味着必须初始化数组长度 你有
string[] productCode = null;
并且永远不要给这个变量分配任何实数数组。其他阵列也是如此
请注意,一旦创建了数组,就不能更改其长度。您可能需要查看该类。我认为您正在将OrderID转换为字符串,并且在数据库中OrderID是一个整数 你的查询不是很好
orderID = (int)com5.ExecuteScalar();
SqlCeCommand com1 = new SqlCeCommand("SELECT ProductCode, Quantity FROM Order_Products WHERE OrderID = "+ orderID+")", con);
或者类似的东西……
在使用变量之前,应该实例化它们。您不能这样做:
string[] productCode = null;
productCode[0] = "KD48Y";
//This results in: 'Object reference not set to an instance of an object.'
这将有助于:
string[] productCode = new string[3]; //can contain 3 strings
productCode[0] = "KD48Y";
但在您的情况下,我建议您使用一个列表,否则当达到初始限制时无法增加数组的大小
查询
这:
可替换为:
SqlCeCommand com1 = new SqlCeCommand("SELECT ProductCode, Quantity FROM Order_Products
WHERE OrderID = (SELECT MAX(OrderID) AS Expr1
FROM Order_Details)", con);
数据阅读器
为了从datareader对象获取值,我建议您使用适当的方法,如GetString或GetInt32。有关此的详细信息:
异常处理
在与数据源(如SQL数据库或其他导致异常的操作)交互时,最好使用异常处理
SqlConnection connection = new SqlConnection("...");
SqlCommand sql = new SqlCommand("...");
List<Product> products = new List<Product>();
//other instantiations
try
{
connection.Open();
sql.ExecuteScalar();
//other operations
}
catch (Ecxeption ex)
{
//handle exception and show a message to the user
MessageBox.Show(ex.Message);
}
finally
{
//this will always execute, even when an exception occurs
//good for closing used resources
connection.Close();
}
我希望这能让你走上正轨 哪一行?您是否尝试使用断点查看问题出在何处?while dr1.Read之后。代码中有注释。代码中有很多错误。你需要重构你的代码很多,你不能把所有的东西都放在windows中加载,需要使用参数化的查询,还有很多显式类型转换可能会出错。。。最后,dr1没有获取导致问题的任何记录。直接在DB中执行查询,看看表是否返回任何值巴拉斯:关于重构,您是正确的,但是关于DataReader,您是绝对错误的。它在whileRead声明中。如果不提取任何记录,该代码甚至不会被执行。您在这里分配productCode[i]=SOMEVALUE what is i,what position。因为数组没有i位置。从0递增到提取数据。数组声明错误吗?必须声明具有一定长度的数组。类似字符串[]产品名称=新字符串[记录的名称];但是您应该使用列表而不是数组,因为您不能增加数组长度。
SqlConnection connection = new SqlConnection("...");
SqlCommand sql = new SqlCommand("...");
List<Product> products = new List<Product>();
//other instantiations
try
{
connection.Open();
sql.ExecuteScalar();
//other operations
}
catch (Ecxeption ex)
{
//handle exception and show a message to the user
MessageBox.Show(ex.Message);
}
finally
{
//this will always execute, even when an exception occurs
//good for closing used resources
connection.Close();
}