C# 基于三层应用程序中的下拉列表填充文本框

C# 基于三层应用程序中的下拉列表填充文本框,c#,asp.net,3-tier,C#,Asp.net,3 Tier,我想根据从下拉列表中选择的值填充文本框 DAL: public static string GetTicket(collection b) { try { string returnValue = string.Empty; DB = Connect(); DBCommand = connection.Procedure("getTicket"); DB.AddInParameter(DBCommand, "@Su

我想根据从下拉列表中选择的值填充文本框

DAL:

public static string GetTicket(collection b)
{
    try
    {

        string returnValue = string.Empty;
        DB = Connect();
        DBCommand = connection.Procedure("getTicket");
        DB.AddInParameter(DBCommand, "@SupportRef", DbType.String, b.SupportRef1);

        var myReader = DBCommand.ExecuteReader();
        while (myReader.Read())
        {
            returnValue = myReader.GetString(0);
        }

        return returnValue;
    }

    catch (Exception ex)
    {
        throw ex;
    }
   public string returnTicket(collection b)
   {
       try
       {
           string ticket = DAL.data.GetTicket(b);
           return ticket;
       }
       catch (Exception ex)
       {
           throw ex;
       }
   }
protected void ddl_Customers_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedValue = ddl_Customers.SelectedValue.ToString();

    //populate the text boxes
    txtSupportRef.Text = bobj.returnTicket(selectedValue);
}
BLL:

public static string GetTicket(collection b)
{
    try
    {

        string returnValue = string.Empty;
        DB = Connect();
        DBCommand = connection.Procedure("getTicket");
        DB.AddInParameter(DBCommand, "@SupportRef", DbType.String, b.SupportRef1);

        var myReader = DBCommand.ExecuteReader();
        while (myReader.Read())
        {
            returnValue = myReader.GetString(0);
        }

        return returnValue;
    }

    catch (Exception ex)
    {
        throw ex;
    }
   public string returnTicket(collection b)
   {
       try
       {
           string ticket = DAL.data.GetTicket(b);
           return ticket;
       }
       catch (Exception ex)
       {
           throw ex;
       }
   }
protected void ddl_Customers_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedValue = ddl_Customers.SelectedValue.ToString();

    //populate the text boxes
    txtSupportRef.Text = bobj.returnTicket(selectedValue);
}
PL:

public static string GetTicket(collection b)
{
    try
    {

        string returnValue = string.Empty;
        DB = Connect();
        DBCommand = connection.Procedure("getTicket");
        DB.AddInParameter(DBCommand, "@SupportRef", DbType.String, b.SupportRef1);

        var myReader = DBCommand.ExecuteReader();
        while (myReader.Read())
        {
            returnValue = myReader.GetString(0);
        }

        return returnValue;
    }

    catch (Exception ex)
    {
        throw ex;
    }
   public string returnTicket(collection b)
   {
       try
       {
           string ticket = DAL.data.GetTicket(b);
           return ticket;
       }
       catch (Exception ex)
       {
           throw ex;
       }
   }
protected void ddl_Customers_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedValue = ddl_Customers.SelectedValue.ToString();

    //populate the text boxes
    txtSupportRef.Text = bobj.returnTicket(selectedValue);
}
我的存储过程有一个名为SupportRef的变量,它需要一个值才能返回结果

我得到以下错误:

The best overloaded method match for 'BLL.business.returnTicket(DAL.collection)' 
has some invalid arguments


是的,从您试图将字符串值传递给业务层方法returnTicket(集合b)的表单。但是在这个业务层方法returnTicket(collection b)中,签名具有要接受的collection type参数。 从下拉列表中选择值后,所选值将存储在字符串变量中。 请将BLL和DAL方法的集合类型更改为字符串类型。
此更改将解决上述错误。

您需要将
string
类型参数传递给BLL的
returnTicket
和DAL的
GetTicket
。这样更改您的方法

BLL

public string returnTicket(string supportRef)
{
   try
   {
       string ticket = DAL.data.GetTicket(supportRef);
       return ticket;
   }
   catch (Exception ex)
   {
       throw ex;
   }
}
public static string GetTicket(string supportRef)
{
 try
 {

    string returnValue = string.Empty;
    DB = Connect();
    DBCommand = connection.Procedure("getTicket");
    DB.AddInParameter(DBCommand, "@SupportRef", DbType.String, supportRef);

    var myReader = DBCommand.ExecuteReader();
    while (myReader.Read())
    {
        returnValue = myReader.GetString(0);
    }

    return returnValue;
 }


 catch (Exception ex)
 {
    throw ex;
 }

}
DAL

public string returnTicket(string supportRef)
{
   try
   {
       string ticket = DAL.data.GetTicket(supportRef);
       return ticket;
   }
   catch (Exception ex)
   {
       throw ex;
   }
}
public static string GetTicket(string supportRef)
{
 try
 {

    string returnValue = string.Empty;
    DB = Connect();
    DBCommand = connection.Procedure("getTicket");
    DB.AddInParameter(DBCommand, "@SupportRef", DbType.String, supportRef);

    var myReader = DBCommand.ExecuteReader();
    while (myReader.Read())
    {
        returnValue = myReader.GetString(0);
    }

    return returnValue;
 }


 catch (Exception ex)
 {
    throw ex;
 }

}

您正在从表示层传递字符串。尝试在演示层中传递
集合

简短回答 在表示层中,将
字符串
类型映射到
DAL.collection
类型

解释 这两个错误都是编译错误。你可以看到一个

错误1 与“BLL.business.returnTicket(DAL.collection)”匹配的最佳重载方法具有一些无效参数

编译器正在试图找到一个名为
BLL.business.returnTicket
的方法,该方法只接受一个参数。在找到的匹配中,该方法采用单个
DAL.collection
参数。而是将
字符串
传递给它,这是一个无效参数,因为
字符串
不是
DAL.collection

重载解析是一种编译时机制,用于在给定参数列表和一组候选函数成员的情况下选择要调用的最佳函数成员

错误2 参数1:无法从“字符串”转换为“DAL.collection”

由于
BLL.business.returnTicket
采用
DAL.collection
参数,编译器尝试将
字符串
转换为
DAL.collection
。它失败,因为没有从
string
类型到
DAL.collection
类型的隐式转换:

隐式转换:不需要特殊语法,因为转换是类型安全的,不会丢失任何数据

怎么办? 按照复杂性的顺序,您可以采取几种方法

  • 在表示层中,将
    字符串
    类型映射到
    DAL.collection
    类型。推荐的

  • 在业务层中,除了现有的方法重载之外,还创建一个新的
    returnTicket(string)
    方法重载,该方法重载将
    string
    映射到
    DAL.collection
    类。推荐的

  • returnTicket(DAL.collection)
    GetTicket(DAL.collection)
    更改为使用
    字符串而不是
    DAL.collection
    。这有两个缺点:它会破坏当前使用
    DAL.collection
    参数调用这些方法的其他代码,并且需要在两个不同的方法中更改四行代码

  • DAL.collection中创建一个from
    字符串
    缺点:这可能是过度使用

  • 建议做的事情 在表示层中,转换或将
    字符串
    类型映射到
    DAL.collection
    类型。这就是上面简短的答案所实现的

    或者,在业务层中,除了现有方法之外,还创建一个新的
    returnTicket(string)
    方法重载。看起来是这样的

    public string returnTicket(collection b)
    {
         // map the string to a DAL.collection
         var collection = new DAL.collection();
         collection.SupportRef1 = selectedValue;
    
         // call the existing method that takes a DAL.collection
         returnTicket(b);
    }
    

    您传递的是字符串而不是集合对象..?从我传递字符串值的表单中,请发布类的结构
    DAL.collection
    。通过这种方式,我们可以帮助您将类型
    string
    转换为类型
    DAL.collection
    。虽然这样做有效,但它将破坏其他已经调用
    GetTicket(集合b)
    returnTicket(集合b)
    的代码。当这样做有效时,它将破坏其他已经调用
    GetTicket(集合b)的代码
    退货单(收款b)