Click here to Skip to main content
15,915,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
----------
XML
<configuration>
  <appSettings>
    <add key="ConnectionString" value="Server=NAMIT-KB\SQLEXPRESS;Database=Interview_UNM_DB;UserID=sa;Pwd=manager"/>
  </appSettings>
</configuration>


---------------------------------------------------------------------------------
Code on page load
---------------------------------------------------------------------------------
C#
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];
        }
}
Posted
Updated 28-Oct-12 3:01am
v3

1 solution

Quote:
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();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900