C# 将dropdownlist绑定到从函数返回的数据集
我编写了以下方法将下拉列表绑定到数据集。我需要在我的项目中的不同页面上调用此方法两次。因此,我创建了一个类并将该方法放入其中,我试图通过创建一个对象来访问该方法。这样做有困难C# 将dropdownlist绑定到从函数返回的数据集,c#,asp.net,drop-down-menu,dataset,C#,Asp.net,Drop Down Menu,Dataset,我编写了以下方法将下拉列表绑定到数据集。我需要在我的项目中的不同页面上调用此方法两次。因此,我创建了一个类并将该方法放入其中,我试图通过创建一个对象来访问该方法。这样做有困难 public void bind() { DataSet ds1 = new DataSet(); SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings
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();
}
如果我在新类中编写这个完整的方法,它将无法识别其中使用的控件(dropdownlist),因此会抛出一个错误。因此,我只包括以下部分:
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()
{
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();
return ds1;
}
然后按如下方式分配它们
Bind objbind = new Bind();
ddlCountryCode.DataSource = objbind.bindddl().Tables["AUser"];
使您的函数返回数据集,然后将其分配给您想要的任何对象
public DataSet 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();
return ds1;
}
然后按如下方式分配它们
Bind objbind = new Bind();
ddlCountryCode.DataSource = objbind.bindddl().Tables["AUser"];
找不到类型或命名空间数据集。是否缺少程序集引用或指令…?-->公共数据集binddl()@AdityaNawandar-是否使用System.Data添加;在班上名列前茅?是的。。。没关系,我重新输入了“数据集”,就完成了。谢谢(标记为回答)@AdityaNawandar-很高兴能帮忙。不过我还有一个问题。。为什么它只从表中获取代码而不获取国家名称?找不到类型或命名空间数据集。是否缺少程序集引用或指令…?-->公共数据集binddl()@AdityaNawandar-是否使用System.Data添加;在班上名列前茅?是的。。。没关系,我重新输入了“数据集”,就完成了。谢谢(标记为回答)@AdityaNawandar-很高兴能帮忙。不过我还有一个问题。。为什么只从表中取代码而不取国家名称??