1
我已经编写了将下拉列表绑定到数据集的以下方法。我需要在我的项目中的不同页面上调用此方法两次。所以我创建了一个类并将其放入其中,并且我试图通过创建一个对象来访问此方法。有无法做到的......将dropdownlist绑定到从函数返回的数据集
public void bind()
{
DataSet ds1 = new DataSet();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
string strQuery = "SELECT CountryName + '(+' + CountryCode + ')' As CountryName,CountryCode from ACountry";
SqlCommand cmd = new SqlCommand(strQuery, con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
da.Fill(ds1, "AUser");
ddlCountryCode.DataSource = ds1.Tables["AUser"];
ddlCountryCode.DataTextField = "CountryCode";
//ddlCountryCode.SelectedValue = "India(+91)";
ddlCountryCode.DataBind();
ddlCountryCode.SelectedIndex = ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByText("India(+91)"));
con.Close();
}
如果我写在新的类此完整方法,它不承认它使用&所以它抛出一个错误的控件(下拉列表)。所以我只包含以下部分:
public void bindddl()
{
DataSet ds1 = new DataSet();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
string strQuery = "SELECT CountryName + '(+' + CountryCode + ')' As CountryName,CountryCode from ACountry";
SqlCommand cmd = new SqlCommand(strQuery, con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
da.Fill(ds1, "AUser");
con.Close();
}
现在这返回一个数据集,我需要绑定到另一个窗体(.aspx)上的下拉列表。我怎么做?
protected void Page_Load(object sender, EventArgs e)
{
Bind objbind = new Bind();
ddlCountryCode.DataSource = objbind.---->?????????;
ddlCountryCode.DataTextField = "CountryCode";
//ddlCountryCode.SelectedValue = "India(+91)";
ddlCountryCode.DataBind();
ddlCountryCode.SelectedIndex = ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByText("India(+91)"));
}
另外,我还能做什么?这里有更好的选择吗?
无法找到类型或名称空间数据集。您是否缺少装配参考或指令...? ---> public Dataset bindddl() – adityawho 2013-03-21 13:27:14
@AdityaNandandar - 你使用System.Data添加了吗?在班级的顶部? – MuhammadHani 2013-03-21 13:31:00
是的......没关系,我重新输入“数据集”并完成了。谢谢...(标记为答案) – adityawho 2013-03-21 13:32:39