0

当我从下拉列表中选择一个值时,我不断收到错误消息。

**The SelectCommand property has not been initialized before calling 'Fill'.**

看起来我的数据集返回为空。

我想坚持 3 层结构。

如何使用我的代码返回有效数据集?

达尔

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

看起来我下面有一些冗余代码。我想在下面使用我的连接类:

   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;
       }
   }

连接类

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();
}
4

2 回答 2

1

根据我在对 OP 问题的评论中提到的假设,您需要遵循。

将 DAL 更改为拥有此public static DataTable GetCustomer(string customer_ref)并使用此DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, customer_ref);

我看到 BAL 中没有完成任何工作,所以我跳过了它的使用。

bobj而不是在使用 DAL 实例之一时使用 BLL 对象,因为它已经GetCustomer并且有一些工作可以从数据库中获取信息。

所以假设bobj是 DAL 的一个实例,接下来,改变 PL,像这样 -txtCustomerRef.Text = bobj.GetCustomer(selectedValue).Tables[0].Rows[0][0].ToString();

于 2015-04-19T11:21:03.613 回答
1

像这样更改您的 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;
       }
   }
于 2015-04-19T11:24:48.817 回答