Filling text fields based on a drop-down list in a three-tier application

I would like to populate my text fields with values ​​based on the selection from my drop-down list.

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

My stored procedure has a variable called SupportRef that needs a value before it can return results.

I get the following errors:

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

and

 Argument 1: cannot convert from 'string' to 'DAL.collection' 
+7
c # 3-tier
source share
4 answers

Short answer

In your presentation layer, map the string type to the DAL.collection type. You can see it 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); } 

Explanation

Both errors are compilation errors. You can see their recreation as in this script .

Error 1

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

The compiler is trying to find a method called BLL.business.returnTicket that takes one argument. In the found match, the method takes one argument DAL.collection . You pass string instead, which is an invalid argument because string not DAL.collection . From MSDN :

Overload resolution is a compile-time mechanism for selecting the best member of a function to call a given list of arguments and a set of candidate elements.

Error 2

Argument 1: cannot be converted from 'string' to 'DAL.collection'

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

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

What to do?

There are several approaches that you could take in order of complexity.

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

  • At your business level, create a new returnTicket(string) method, in addition to the existing one, which maps the string class to the DAL.collection class. Recommended.

  • Change both returnTicket(DAL.collection) and GetTicket(DAL.collection) to take string instead of DAL.collection . This has two drawbacks: it will break other code that currently calls these methods with the DAL.collection argument, and requires changing four lines of code in two different ways.

  • Create a custom conversion from string to DAL.collection. Downside: This is probably a bust.

Recommended Actions

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

Alternatively, at your business level, create a new overload of the returnTicket(string) method in addition to the existing method. It will look like this.

 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); } 
+2
source share

Yes. From the form, you are trying to pass a String value to the returnTicket business layer method (collection b). But in this business-level method, returnTicket (collection b) has an argument of the type of collection that must be accepted. After selecting a value from the drop-down list, the selected value is stored in a string variable. Change the collection type to BLL and DAL to row type. This change will fix the above error.

+4
source share

You need to pass an argument of type string to returnTicket of BLL and GetTicket of DAL . Change your methods as follows

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; } } 
+3
source share

You are passing a string from the presentation level. Try passing collection to presentation level.

+1
source share

All Articles