5

How can i get the value of company_name from Comp table and store it on a comboBox?

here is my initial code on getting the values from Database and store it on a combobox:

string Sql = "select company_name from JO.dbo.Comp";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());

it point out to da.fill(ds) and says "Could not locate entry in sysdatabases for database 'select company_name from JO'. No entry found with that name. Make sure that the name is entered correctly."

hope for your reply thanks!

4
  • 1
    What is JO? Have you tried without it, i.e. just select company_name from dbo.Comp? Commented Jul 31, 2013 at 1:49
  • JO.dbo.Comp purpose is JO(databasename) dbo(for 'master') comp(tablename) Commented Jul 31, 2013 at 1:56
  • In that case just do databasename.tablename. Commented Jul 31, 2013 at 2:00
  • If you have the initial catalog in the connection string and logging in as a DBO, you can use the table name no need of dbo and database name. Commented Jul 31, 2013 at 2:14

10 Answers 10

14

Use datareader it is much simpler \

   string Sql = "select company_name from JO.dbo.Comp";
   SqlConnection conn = new SqlConnection(connString);
   conn.Open();
   SqlCommand cmd = new SqlCommand(Sql, conn);
   SqlDataReader DR = cmd.ExecuteReader();

            while (DR.Read())
            {
                combobox1.Items.Add(DR[0]);

            }
Sign up to request clarification or add additional context in comments.

Comments

2

If you set up your connection string to be something of this sort:

string SqlConnectionString = "Data Source=[SERVER];Initial Catalog=[DATABASE];"

Then using that set up, you can set your string 'Sql' as:

string Sql = "select company_name from dbo.Comp";

This could be a possible set up you could use to read out the values.

using (SqlConnection saConn = new SqlConnection(this.ConnectionString))
{
       saConn.Open();

       string query = "select DBName from dbo.Company";
       SqlCommand cmd = new SqlCommand(query, saConn);

       using (SqlDataReader saReader = cmd.ExecuteReader())
       {
            while (saReader.Read())
            {
                   string name = saReader.GetString(0);
                   combobox1.Add(name);
             }
        }
        saConn.Close();
}

Comments

2

I would like to introduce you a very simple way to SQL data into a combobox as:

  • first you have a create a SQL table,
  • in C# platform drop a combobox and go to its property,
  • in the property menu click on "DataSource"
  • specify the database and table to load into combobox, Note, the combobox name and table's row should be the same.

Comments

1
{ 

    SqlConnection con =new SqlConnection("Data Source=Server_Name;Initial Catalog=Database_Name;integrated security=true");

    SqlCommand cmd;
    SqlDataReader dr;


    private void CashMemoForm_Load(object sender, EventArgs e)
    {
        con.Open();

        cmd = new SqlCommand("Select Column_Name From Table_Name", con);


        dr = cmd.ExecuteReader();


       while (dr.Read())

        {
            comboBox1.Items.Add(dr[0]).ToString();
        }
    }
}

Comments

0

Have you ever tried Entity Framework for database access and dto creation?

Comments

0

Change your line to cmd.CommandType = CommandType.Text; instead of cmd.CommandType = CommandType.StoredProcedure;

Comments

0

Try this

string Sql = "select Company_ID,company_name from JO.dbo.Comp";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{

     comboBox1.DataSource = ds.Tables[0];
     comboBox1.DataTextField = "company_name";
     comboBox1.DataValueField = "Company_ID";
     comboBox1.DataBind();
     comboBox1.Items.Insert(0, new ListItem("--Select--", "0"));
}

Comments

0

There is no use of for loop. you just need to check that whether the dataset contains rows or not.

    string Sql = "select Company_ID,company_name from JO.dbo.Comp";
    SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(Sql, conn);
    cmd.CommandType = CommandType.Text;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
         comboBox1.DataSource = ds.Tables[0];
         comboBox1.DataTextField = "company_name";
         comboBox1.DataValueField = "Company_ID";
         comboBox1.DataBind();
         comboBox1.Items.Insert(0, new ListItem("Select", "0"));
    }

Comments

0
string Sql = "select company_name from JO.dbo.Comp";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());

2 Comments

You should format your answers with Markdown: stackoverflow.com/editing-help
While the code could solve the problem, it is better to add a description of what you have changed and why you have made the change for the answer to be more useful to others.
0
public System.Data.DataTable EmployeeViewAll()
  {
  DataTable dtbl = new DataTable();
  try
  {
  // Here it shuld be your database Connection String
  string connectionString = "Server = .; database = HKS; Integrated Security = true";

using (SqlConnection sqlCon = new System.Data.SqlClient.SqlConnection(connectionString))
  {
  SqlDataAdapter SqlDa = new SqlDataAdapter("employeeViewAll", sqlCon);
  SqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
  SqlDa.Fill(dtbl);
  }
  return dtbl;
  }
  catch (Exception)
  {
  throw;
  }
  }


  public void ComboFill()
  {
  DataTable dt = new DataTable();
   eSP SP = new eSP();
  d = SP.EmployeeViewAll();
  comboBox1.DataSource = dt;
  comboBox1.DisplayMember = "department";
  comboBox1.ValueMember = "empName";
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.