我想根据我的下拉列表中的选择,使用值填充文本框。
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'
答案 0 :(得分:4)
是,从表单中,您尝试将String值传递给Business层方法returnTicket(集合b)。但是在此Business层方法中,returnTicket(集合b)签名具有要接受的集合类型参数。 从下拉列表中选择值后,所选值将存储在字符串变量中。 请将BLL和DAL方法的集合类型更改为字符串类型。 此更改将解决上述错误。
答案 1 :(得分:3)
您需要将string
类型参数传递给returnTicket of BLL
和GetTicket 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。
最佳重载方法匹配&#39; BLL.business.returnTicket(DAL.collection)&#39;有一些无效的论点
编译器正在尝试查找名为BLL.business.returnTicket
的方法,该方法只接受一个参数。在它找到的匹配中,该方法采用单个DAL.collection
参数。您将string
传递给string
,这是一个无效的参数,因为DAL.collection
不是BLL.business.returnTicket
。 From MSDN:
重载解析是一种编译时机制,用于在给定参数列表和一组候选函数成员的情况下选择要调用的最佳函数成员。
参数1:无法转换为&#39; string&#39;到&#39; DAL.collection&#39;
由于DAL.collection
采用string
参数,编译器会尝试将DAL.collection
转换为string
。它失败,因为从DAL.collection
类型到string
类型没有隐式转换。 From MSDN:
隐式转换:不需要特殊语法,因为转换类型安全且不会丢失任何数据。
根据复杂程度,您可以采取一些方法。
在您的表示层中,将DAL.collection
类型映射到returnTicket(string)
类型。推荐使用。
在您的业务层中,除了现有方法之外,还要创建一个新的string
方法重载,该方法会将DAL.collection
映射到returnTicket(DAL.collection)
类。推荐使用。
更改GetTicket(DAL.collection)
和string
以取DAL.collection
而不是DAL.collection
。这有两个缺点:它会破坏当前使用string
参数调用这些方法的其他代码,并且需要在两种不同的方法中更改四行代码。
从DAL.collection.
创建user-defined conversion到string
下行:这可能有点矫枉过正。
在您的演示文稿图层中,将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
。