我有这个aspx
代码,我对asmx.cs
代码感到困惑。
ASPX:
<div class="col-md-6">
<asp:DropDownList ID="ddlMake" runat="server" CssClass="form-control"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="cdlMake" TargetControlID="ddlMake" PromptText="Select Make" PromptValue="" ServicePath="CascadingDropdown1.cs.asmx" ServiceMethod="GetMakes" runat="server" Category="MakeId" LoadingText="Loading Make..." />
</div>
<div class="col-md-6">
<asp:DropDownList ID="ddlModel" runat="server" CssClass="form-control"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="cdlModel" TargetControlID="ddlModel" PromptText="Select Model" PromptValue="" ServicePath="CascadingDropdown1.cs.asmx" ServiceMethod="GetModels" runat="server" Category="ModelId" ParentControlID="ddlMake" LoadingText="Loading Model..." />
</div>
我有Stored Procedure
名为Search_MAKE
和Search_MODEL
且模型SP需要参数。
有人可以帮我构建asmx.cs
代码。
更新
[ScriptService]
public class CascadingDropdown1 : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] GetMakes(string knownCategoryValues)
{
string query = "exec spIBCInventorySearch_MAKE @IDCustomer = 253433";
List<CascadingDropDownNameValue> Makes = GetData(query);
return Makes.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetModels(string knownCategoryValues)
{
string make = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["MakeId"];
string query = string.Format("exec spIBCInventorySearch_MODEL @VehicleMake = {0}, @IDCustomer = 253433 ", make);
List<CascadingDropDownNameValue> Models = GetData(query);
return Models.ToArray();
}
private List<CascadingDropDownNameValue> GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["UID=pal;Password=123;DATABASE=ATDBSQL;"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(new CascadingDropDownNameValue
{
name = reader[0].ToString(),
value = reader[1].ToString()
});
}
reader.Close();
con.Close();
return values;
}
}
}
}
我刚刚完成了asmx
代码,但它会在conString
部分中断。有人可以帮我调试这个问题吗?
答案 0 :(得分:1)
您正试图以奇怪的方式从配置文件中获取连接字符串。通常它是connectionStrings部分中的一个键。例如:
<connectionStrings>
<add name="YourConnectionString" connectionString="Server=WIN-9A0EHHSEC8L\SQLEXPRESS2014;Database=SenseAPIClient;Integrated Security = true;" providerName="System.Data.SqlClient" />
要检索数据库连接字符串,我需要使用&#34; YourConnectionString&#34;。如下:
var conString = ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
答案 1 :(得分:0)
[ScriptService]
public class CascadingDropdown1 : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] GetMakes(string knownCategoryValues)
{
string query = "exec spIBCInventorySearch_MAKE @IDCustomer = 253433";
List<CascadingDropDownNameValue> Makes = GetData(query);
return Makes.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetModels(string knownCategoryValues)
{
string make = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["MakeId"];
string query = string.Format("exec spIBCInventorySearch_MODEL @VehicleMake = {0}, @IDCustomer = 253433 ", make);
List<CascadingDropDownNameValue> Models = GetData(query);
return Models.ToArray();
}
private List<CascadingDropDownNameValue> GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["UID=pal;Password=123;DATABASE=ATDBSQL;"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(new CascadingDropDownNameValue
{
name = reader[0].ToString(),
value = reader[0].ToString()
});
}
reader.Close();
con.Close();
return values;
}
}
}
我只是将value=reader[0].ToString()
中的索引从1更改为0:)
因为表格中只有一列。