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

前端之家收集整理的这篇文章主要介绍了c# – 根据3层应用程序中的下拉列表填充文本框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想根据我的下拉列表中的选择,使用值填充我的文本框.

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'

解决方法

简答

在表示层中,将字符串类型映射到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

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

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

Overload resolution is a compile-time mechanism for selecting the best function member to invoke given an argument list and a set of candidate function members.

错误2

Argument 1: cannot convert from ‘string’ to ‘DAL.collection’

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

Implicit conversions: No special Syntax is required because the conversion is type safe and no data will be lost.

该怎么办?

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

>在表示层中,将字符串类型映射到DAL.collection类型.推荐的.
>在业务层中,除了现有的方法之外,还要创建一个新的returnTicket(string)方法重载,该方法将字符串映射到DAL.collection类.推荐的.
>更改returnTicket(DAL.collection)和GetTicket(DAL.collection)以获取字符串而不是DAL.collection.这有两个缺点:它将破坏当前使用DAL.collection参数调用这些方法的其他代码,并且它需要在两种不同的方法中更改四行代码.
>从字符串创建user-defined conversion到DAL.collection.缺点:这可能是矫枉过正.

推荐的事情要做

在表示层中,将字符串类型转换或映射到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);
}
原文链接:https://www.f2er.com/csharp/100376.html

猜你在找的C#相关文章