AJ26
AJ26

Reputation: 501

Trimming of SQL Database values from my c# application

Am trying to achieve trimming my comboBox whch is getting data from my SQL Server. Am aware that the Trim function should be used. I have applied it in various ways but seem to still be faced with white spaces from my database. How do I go about including the Trim function effectively?

void fill()//fill combobox with values
{
    try
    {
        string connectionString = "Data Source=cmcentraldb;Initial Catalog=CMLTD;Integrated Security=True";
        SqlConnection con2 = new SqlConnection(connectionString);
        con2.Open();
        string query = "SELECT DISTINCT ITEMDESC  FROM dbo.IV00101"; 
        //select Convert(nvarchar(50),Item_Description)+ ':' +Convert(nvarchar(50),Item#) as Combined from Carimed
        SqlCommand cmd2 = new SqlCommand(query, con2);

        SqlDataReader dr2 = cmd2.ExecuteReader();
        while (dr2.Read())
        {
            string cari_des = dr2.GetString(dr2.GetOrdinal("ITEMDESC"));
            suggestComboBox1.Items.Add(cari_des);
            suggestComboBox1.Text.Trim();
        }
        //con2.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Upvotes: 1

Views: 1652

Answers (4)

Anup Shah
Anup Shah

Reputation: 1254

How do I go about including the Trim function effectively

One thing I notice is you have SQL query prepared in application side. For many reasons this is neither good practice nor advisable.

You should have created stored procedures which returns the needed data. implementing "Trim" function should be on database side (inside the db procedure) rather on the application side unless you are sure that data returned from the database is never going to be in large amount.

declare @name varchar(5)='a   '
select @name,RTRIM(LTRIM(@name))

Upvotes: 1

Deeksha Pandit
Deeksha Pandit

Reputation: 74

You need to trim value before binding it to combobox. Do it like below:

void fill()//fill combobox with values
{
    try
    {
        string connectionString = "Data Source=cmcentraldb;Initial Catalog=CMLTD;Integrated Security=True";
        SqlConnection con2 = new SqlConnection(connectionString);
        con2.Open();
        string query = "SELECT DISTINCT ITEMDESC  FROM dbo.IV00101"; 
        //select Convert(nvarchar(50),Item_Description)+ ':' +Convert(nvarchar(50),Item#) as Combined from Carimed
        SqlCommand cmd2 = new SqlCommand(query, con2);

        SqlDataReader dr2 = cmd2.ExecuteReader();
        while (dr2.Read())
        {
            string cari_des = dr2.GetString(dr2.GetOrdinal("ITEMDESC"));
            suggestComboBox1.Items.Add(cari_des.Trim());
        }
        //con2.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Upvotes: 1

Afnan Ahmad
Afnan Ahmad

Reputation: 2542

You can also use Replace

suggestComboBox1.Items.Add(cari_des.Replace(" ", ""));

Note: This will remove all the white spaces at start at end and in middle.

Upvotes: 0

David
David

Reputation: 1871

You need to trim the items that you Add to the combobox

suggestComboBox.Items.Add(cari_des.Trim());

you can delete the line, it does nothing:

suggestComboBox1.Text.Trim();     // delete this

Upvotes: 1

Related Questions