如何在Pageload上绑定Combobox [英] How to bind Combobox on Pageload

查看:69
本文介绍了如何在Pageload上绑定Combobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面加载页面时绑定我的组合框,数据将从数据库中获取,
但无法绑定.每当我在第一行运行代码并使用断点时,它都会错误地读取连接字符串或写入连接字符串,这会显示我的表单页面和组合框中的数据未加载.我尝试了很多.名称空间是否存在任何问题?有人可以建议我还是可以向我提供代码以及更多其他内容.如果我能够在app.config文件中这样做会更好.

由于多次尝试...我面临许多您可能理解的问题.谢谢你
我的代码如下
应用代码
----------

i want to bind my combobox on page load of a form Data will fetch from the database,
but unable to bind. Whenever i am running code and using break point at my first line, it read the connection string either wrong or write connection string it show my form page and data in combo box not loaded. I try a lot. Is there any problem in the name space.Can any body suggest me or can provide me code and one more thing.It will be better if i will able to do in app.config file.

Due to multiple try...I face many problem might u can understand. Thank u
My code is below
App Code
----------

<configuration>
  <appSettings>
    <add key="ConnectionString" value="Server=NAMIT-KB\SQLEXPRESS;Database=Interview_UNM_DB;UserID=sa;Pwd=manager"/>
  </appSettings>
</configuration>



-------------------------------------------------- -------------------------------
页面加载代码
-------------------------------------------------- -------------------------------



---------------------------------------------------------------------------------
Code on page load
---------------------------------------------------------------------------------

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

namespace Window_MyShop_2110
{
    public partial class frmHome : Form
    {
        //string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //DataTable tmp;
        //SqlDataAdapter dA;
        //string strData = string.Empty;
        public frmHome()
        {
            InitializeComponent();
        }


private void frmHome_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Server=NAMIT-KB\SQLEXPRESS;Database=Interview_UNM_DB;UserID=sa;Pwd=manager");
            //SqlCommand cmd = new SqlCommand("Select * from table_CreateFirm Where Category='Cloth'",strConn);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select * from table_CreateFirm Where Category='Cloth";
            DataSet objDs = new DataSet();
            SqlDataAdapter dAdapter = new SqlDataAdapter();
            dAdapter.SelectCommand = cmd;
            con.Open();
            dAdapter.Fill(objDs);
            con.Close();
            cBoxFirmName.ValueMember = "FirmID";
            cBoxFirmName.DisplayMember = "FirmName";
            cBoxFirmName.DataSource = objDs.Tables[0];
        }
}

推荐答案

报价:

comboBox2.Items.Clear( );
SqlConnection con =新的SqlConnection(数据源= .;初始目录= Dbname; uid = sa; pwd =密码");
试试
{
con.Open();
SqlCommand cmd =新的SqlCommand(按名称asc从Addgodown顺序中选择与众不同(名称)",con);
dr = cmd.ExecuteReader();
while(dr.Read())
{
comboBox2.Items.Add(dr [0] .ToString());
}
dr.Close();
}
catch(ex ex例外)
{
MessageBox.Show(ex.Message);
}
终于
{
con.Close();
}

comboBox2.Items.Clear();
SqlConnection con=new SqlConnection("Data Source=.; Initial Catalog=Dbname;uid=sa;pwd=password");
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select distinct(Name) from Addgodown order by name asc", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox2.Items.Add(dr[0].ToString());
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}


这篇关于如何在Pageload上绑定Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆