我正在开发一个 asp.net 应用程序,因为我必须使用 jquery 将下拉列表与 country_names 绑定。我使用了以下代码:-
[WebMethod]
public static CountryDetails[] BindDatatoDropdown()
{
SqlConnection conn = new SqlConnection(@"Data Source=RIZ-PC\SQLEXPRESS;Initial Catalog=Sandeep;Integrated Security=True;");
conn.Open();
DataTable dt = new DataTable();
List<CountryDetails> details = new List<CountryDetails>();
// SqlCommand cmd = new SqlCommand("SELECT CountryID,CountryName FROM Country", con);
SqlDataAdapter da = new SqlDataAdapter("SELECT CountryID,CountryName FROM Country", conn);
da.Fill(dt);
foreach (DataRow dtrow in dt.Rows)
{
CountryDetails country = new CountryDetails();
country.CountryId = Convert.ToInt32(dtrow["CountryId"].ToString());
country.CountryName = dtrow["CountryName"].ToString();
details.Add(country);
}
return details.ToArray();
}
public class CountryDetails
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
但它不起作用,当控件转到 conn.open() 时,它在窗口中显示错误,错误只显示“本地主机上的页面说:错误”请帮助我。谢谢