Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

im have a problem with some code to load data from my MSSQL server in to a combo box. the code works and it loads the data but when i select the item from the drop down it loads all the blank space from the SQL table aswell.

this is my code:

C#
private void loadlocationcombobox()
        {
            string constr = ConfigurationManager.ConnectionStrings  ["SQLServer"].ConnectionString;
            string query = "select location_name from dbo.Location";

            SqlConnection con = new SqlConnection(constr);
            SqlCommand command = new SqlCommand(query, con);
            con.Open();
            SqlDataReader sqlreader = command.ExecuteReader();



            try
            {
                while (sqlreader.Read())
                {
                    comboLocation.Items.Add(sqlreader[0]);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("A error has occurred: " + ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                sqlreader.Close();
                con.Close();
            }
        }



any help would be great as i have been looking at the code but just cant see how to fix it.
Posted
Comments
[no name] 3-Jan-13 9:58am    
hi,

Check by debugging when u select an item from dropdown list can this method "loadlocationcombobox()", call again.

try converting char to varchar as follows

SQL
select CAST(RTRIM(location_name) AS VARCHAR(50)) from dbo.Location
 
Share this answer
 
Comments
Sparky12488 3-Jan-13 11:29am    
thank you so much for your help, that has done the job.
[no name] 3-Jan-13 11:29am    
Improve your answer ... Don't add the answers in the solutions...
If I understood your problem correctly, you want to restrict loading blank spaces from to the combo

SQL
select location_name from dbo.Location where isnull(location_name ,'')<>''
 
Share this answer
 
v3
Comments
Sparky12488 3-Jan-13 11:00am    
Hi Tharaka MTR,

thank you for your help, i have tried the code modification, but sadly the combo box does the same thing. when i select the drop down there in the list is the locations name in the list but when i click on it the box goes blank, but if i scroll to the left there is the name. if i fill the relevent box in my sql table with data (the limet is 50 on a char) then select the long name from the drop down it will show the last letters entered. it seems that the combo box is displaying the spare space in the sql table.
Tharaka MTR 3-Jan-13 11:17am    
yes, that because the data type of the column is char(50) and char is a char data type is a fixed-length data type. (varchar is a variable length type)

try converting char to varchar as follows

select CAST(RTRIM(location_name) AS VARCHAR(50)) from dbo.Location

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