使用 asp.net c# 返回有效数据集
returning a valid dataset using asp.net c#
当我从下拉列表中 select 一个值时,我总是收到错误消息。
**The SelectCommand property has not been initialized before calling 'Fill'.**
看起来我的数据集 return 是空的。
我想坚持 3 层结构。
如何使用我的代码 return 一个有效的数据集?
DAL
public static DataTable GetCustomer(collection b)
{
{
DataTable table;
try
{
string returnValue = string.Empty;
DB = Connect();
DBCommand = connection.Procedure("getCustomer");
DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, b.CustomerRef1);
DbDataReader reader = DBCommand.ExecuteReader();
table = new DataTable();
table.Load(reader);
return table;
}
catch (Exception ex)
{
throw (ex);
}
}
}
BLL
看起来我下面有一些冗余代码。我想利用下面的连接 class:
public DataSet returnCustomer(collection b)
{
try
{
SqlDataAdapter adapt = new SqlDataAdapter();
DataSet table = new DataSet();
adapt.Fill(table, "table");
return table;
}
catch (Exception ex)
{
throw ex;
}
}
连接Class
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using System.Data.Common;
using System.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class connection
{
const string StrConnection = "CustomerHelperConnectionString";
internal static Database DB;
public static DbCommand DBCommand;
public static Database Connect()
{
try
{
DB = DatabaseFactory.CreateDatabase(StrConnection);
return DB;
}
catch (Exception ex)
{
throw (ex);
}
}
public static DbCommand Procedure(string procedure)
{
try
{
DBCommand = DB.GetStoredProcCommand(procedure);
return DBCommand;
}
catch (Exception ex)
{
throw (ex);
}
}
}
}
PL
protected void ddl_Customers_SelectedIndexChanged(object sender, EventArgs e)
{
DAL.collection cobj = new collection();
BLL.business bobj = new business();
string selectedValue = ddl_Customers.SelectedValue.ToString();
//populate the text boxes
txtCustomerRef.Text = bobj.returnCustomer(cobj).Tables[0].Rows[0][0].ToString();
}
根据我在对 OP 问题的评论中提到的假设,您需要遵循。
将 DAL 更改为具有此 public static DataTable GetCustomer(string customer_ref)
并使用此 DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, customer_ref);
我看到 BAL 中没有完成任何工作,所以我跳过了它的使用。
不要在 bobj
中使用 BLL 对象,而是使用 DAL 实例之一,因为它有 GetCustomer
并且有一些工作可以从数据库中获取信息。
所以假设bobj
是DAL的一个实例,接下来,改变PL,像这样-txtCustomerRef.Text = bobj.GetCustomer(selectedValue).Tables[0].Rows[0][0].ToString();
像这样更改您的 DAL 代码:
public static DataTable GetCustomer(collection b)
{
{
DataTable table = new DataTable();
try
{
string returnValue = string.Empty;
DB = Connect();
DBCommand = connection.Procedure("getCustomer");
DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, b.CustomerRef1);
SqlDataAdapter adptr = new SqlDataAdapter(DBCommand);
adptr.Fill(table);
return table;
}
catch (Exception ex)
{
throw (ex);
}
}
}
现在你的 BAL 是这样的,
public DataSet returnCustomer(collection b)
{
try
{
DataSet _ds = new DataSet();
_ds.Tables.Add(DAL.GetCustomer(b));
return _ds;
}
catch (Exception ex)
{
throw ex;
}
}
当我从下拉列表中 select 一个值时,我总是收到错误消息。
**The SelectCommand property has not been initialized before calling 'Fill'.**
看起来我的数据集 return 是空的。
我想坚持 3 层结构。
如何使用我的代码 return 一个有效的数据集?
DAL
public static DataTable GetCustomer(collection b)
{
{
DataTable table;
try
{
string returnValue = string.Empty;
DB = Connect();
DBCommand = connection.Procedure("getCustomer");
DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, b.CustomerRef1);
DbDataReader reader = DBCommand.ExecuteReader();
table = new DataTable();
table.Load(reader);
return table;
}
catch (Exception ex)
{
throw (ex);
}
}
}
BLL
看起来我下面有一些冗余代码。我想利用下面的连接 class:
public DataSet returnCustomer(collection b)
{
try
{
SqlDataAdapter adapt = new SqlDataAdapter();
DataSet table = new DataSet();
adapt.Fill(table, "table");
return table;
}
catch (Exception ex)
{
throw ex;
}
}
连接Class
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using System.Data.Common;
using System.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class connection
{
const string StrConnection = "CustomerHelperConnectionString";
internal static Database DB;
public static DbCommand DBCommand;
public static Database Connect()
{
try
{
DB = DatabaseFactory.CreateDatabase(StrConnection);
return DB;
}
catch (Exception ex)
{
throw (ex);
}
}
public static DbCommand Procedure(string procedure)
{
try
{
DBCommand = DB.GetStoredProcCommand(procedure);
return DBCommand;
}
catch (Exception ex)
{
throw (ex);
}
}
}
}
PL
protected void ddl_Customers_SelectedIndexChanged(object sender, EventArgs e)
{
DAL.collection cobj = new collection();
BLL.business bobj = new business();
string selectedValue = ddl_Customers.SelectedValue.ToString();
//populate the text boxes
txtCustomerRef.Text = bobj.returnCustomer(cobj).Tables[0].Rows[0][0].ToString();
}
根据我在对 OP 问题的评论中提到的假设,您需要遵循。
将 DAL 更改为具有此 public static DataTable GetCustomer(string customer_ref)
并使用此 DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, customer_ref);
我看到 BAL 中没有完成任何工作,所以我跳过了它的使用。
不要在 bobj
中使用 BLL 对象,而是使用 DAL 实例之一,因为它有 GetCustomer
并且有一些工作可以从数据库中获取信息。
所以假设bobj
是DAL的一个实例,接下来,改变PL,像这样-txtCustomerRef.Text = bobj.GetCustomer(selectedValue).Tables[0].Rows[0][0].ToString();
像这样更改您的 DAL 代码:
public static DataTable GetCustomer(collection b)
{
{
DataTable table = new DataTable();
try
{
string returnValue = string.Empty;
DB = Connect();
DBCommand = connection.Procedure("getCustomer");
DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, b.CustomerRef1);
SqlDataAdapter adptr = new SqlDataAdapter(DBCommand);
adptr.Fill(table);
return table;
}
catch (Exception ex)
{
throw (ex);
}
}
}
现在你的 BAL 是这样的,
public DataSet returnCustomer(collection b)
{
try
{
DataSet _ds = new DataSet();
_ds.Tables.Add(DAL.GetCustomer(b));
return _ds;
}
catch (Exception ex)
{
throw ex;
}
}