Click here to Skip to main content
12,452,015 members (24,616 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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:

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 3-Jan-13 3:43am
Comments
Turbo_23 3-Jan-13 9:58am
   
hi,

Check by debugging when u select an item from dropdown list can this method "loadlocationcombobox()", call again.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try converting char to varchar as follows

select CAST(RTRIM(location_name) AS VARCHAR(50)) from dbo.Location
  Permalink  
Comments
Sparky12488 3-Jan-13 11:29am
   
thank you so much for your help, that has done the job.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

If I understood your problem correctly, you want to restrict loading blank spaces from to the combo

select location_name from dbo.Location where isnull(location_name ,'')<>''
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100