根据3层应用程序中的下拉列表填充文本框

时间:2015-04-03 09:50:24

标签: 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, "@SupportRef", DbType.String, b.SupportRef1);

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

        return returnValue;
    }

    catch (Exception ex)
    {
        throw ex;
    }

BLL:

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

PL:

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

Argument 1: cannot convert from 'string' to 'DAL.collection'

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

您需要将string类型参数传递给returnTicket of BLLGetTicket of DAL。更改您的方法

<强> BLL

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

<强> DAL

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

}

答案 2 :(得分:2)

简答

在您的表示层中,将string类型映射到DAL.collection类型。 You can see this here.

protected void ddl_Customers_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedValue = ddl_Customers.SelectedValue.ToString();

    // map the string to a DAL.collection
    var collection = new DAL.collection();
    collection.SupportRef1 = selectedValue;

    //populate the text boxes
    txtSupportRef.Text = bobj.returnTicket(collection);
}

解释

这两个错误都是编译错误。您可以看到recreation of them both in this fiddle

错误1

  

最佳重载方法匹配&#39; BLL.business.returnTicket(DAL.collection)&#39;有一些无效的论点

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

  

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

错误2

  

参数1:无法转换为&#39; string&#39;到&#39; DAL.collection&#39;

由于DAL.collection采用string参数,编译器会尝试将DAL.collection转换为string。它失败,因为从DAL.collection类型到string类型没有隐式转换From MSDN

  

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

怎么办?

根据复杂程度,您可以采取一些方法。

  1. 在您的表示层中,将DAL.collection类型映射到returnTicket(string)类型。推荐使用。

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

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

  4. DAL.collection.创建user-defined conversionstring下行:这可能有点矫枉过正。

  5. 推荐的事情

    在您的演示文稿图层中,将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); } 方法重载,以及。它看起来像这样。

    x7: function  
    function () { return "hello this is form function"; } 
    

答案 3 :(得分:1)

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