[英]Populate Combobox from Database Using the WebService in C# Winforms
Here Is My Code : 这是我的代码:
[WebMethod]
public SqlDataReader Cmb_BranchMaster() {
SqlCommand ad1 = new SqlCommand("select * from BranchMaster", conn);
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Open();
SqlDataReader rdr2 = ad1.ExecuteReader();
if (rdr2.HasRows)
{
while (rdr2.Read())
{
// here cmbranchname is my combobox of winforms .. so here in webservice it gievs error
cmbBranchName.Items.Add(rdr2[1].ToString());
}
}
conn.Close();
}
now what to do to return the data and use in my winform 现在该怎么做才能返回数据并在我的winform中使用
Here is the Solution i Got 这是我得到的解决方案
static SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Connection"].ToString());
[WebMethod]
public DataSet Cmb_BranchMaster()
{
conn.Open();
SqlCommand ad1 = new SqlCommand("select * from BranchMaster", conn);
SqlDataAdapter adapt = new SqlDataAdapter(ad1);
DataSet ds = new DataSet();
adapt.Fill(ds);
conn.Close();
return ds;
}
private void ComboBox_Load(object sender, EventArgs e)
{
myservice.Service test = new myservice.Service();
DataSet dd = new DataSet();
dd = test.Cmb_BranchMaster();
comboBox1.DataSource = dd.Tables[0];
comboBox1.DisplayMember = "BranchName";
comboBox1.ValueMember = "BranchID";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.