Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My combo box does not load and keeps showing this error

Column 'Item_GenName' does not belong to a table. 


Here's the code:

C#
void loadgeneric()
        {
            string constring = "Data Source=D-HOS-MIS2;Initial Catalog=Consignment_db;Persist Security Info=True;User ID=sa;Password=t.july.01";
            string query = "select distinct convert(varchar,Item_GenName) from ItemMasterlistTable where datalength(Item_GenName) != 0";
            SqlConnection con_db = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand(query, con_db);
            SqlDataReader reader;

            try
            {
                DataTable gencombo = new DataTable();
                con_db.Open();
                reader = cmd.ExecuteReader();
                gencombo.Load(reader);
                foreach (DataRow row in gencombo.Rows)
                {
                 Item_GenName.Items.Add(row["Item_GenName"]);
                }
                Item_GenName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                Item_GenName.AutoCompleteSource = AutoCompleteSource.ListItems;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


What I have tried:

Before it prompted this error, my sql was simply just

select Item_GenName from ItemMasterlist


I wanted it to retrieve non-empty and non-duplicate items that's why I resulted to that query.
Posted
Updated 17-Jun-18 20:57pm

1 solution

Take a look at this line:
string query = "select distinct convert(varchar,Item_GenName) from ItemMasterlistTable where datalength(Item_GenName) != 0";

This returns something like: (No column name). So, change it as follow:
string query = "select distinct convert(varchar,Item_GenName) AS Item_GenName from ItemMasterlistTable where datalength(Item_GenName) != 0";

then you'll be able to refer to column Item_GenName
 
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