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

Pri*_*ton 7 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;
    }
Run Code Online (Sandbox Code Playgroud)

BLL:

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

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);
}
Run Code Online (Sandbox Code Playgroud)

我的存储过程有一个名为SupportRef的变量,它需要一个值才能返回结果.

我收到以下错误:

The best overloaded method match for 'BLL.business.returnTicket(DAL.collection)' 
has some invalid arguments
Run Code Online (Sandbox Code Playgroud)

Argument 1: cannot convert from 'string' to 'DAL.collection'
Run Code Online (Sandbox Code Playgroud)

Sha*_*tin 2

Short Answer

In your presentation layer, map the string type into the DAL.collection type. 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);
}
Run Code Online (Sandbox Code Playgroud)

Explanation

Both of the errors are compilation errors. You can see a recreation of them both in this fiddle.

Error 1

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

The compiler is trying to find a method called BLL.business.returnTicket that takes a single argument. In the match that it finds, the method takes a single DAL.collection argument. You're passing it a string instead, which is an invalid argument, because a string is not a 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.

Error 2

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

Since BLL.business.returnTicket takes a DAL.collection argument, the compiler tries to convert the string into a DAL.collection. It fails because there is no implicit conversion from a string type to a DAL.collection type. From MSDN:

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

What to do?

There are a few approaches you could take, in order of complexity.

  1. In your presentation layer, map the string type to the DAL.collection type. Recommended.

  2. In your business layer, create a new returnTicket(string) method overload, in addition to the existing one, which maps the string to the DAL.collection class. Recommended.

  3. Change both returnTicket(DAL.collection) and GetTicket(DAL.collection) to take a string instead of a DAL.collection. This has two downsides: it will break other code that currently calls those methods with a DAL.collection argument and it requires changing four lines of code in two different methods.

  4. Create a user-defined conversion from string into DAL.collection. Downside: this is likely overkill.

Recommended things to do

In your presentation layer, convert or to map the string type into the DAL.collection type. That is what the short answer above accomplishes.

或者,在业务层中,除了现有方法之外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);
}
Run Code Online (Sandbox Code Playgroud)