Jemo
Jemo

Reputation: 309

How to prevent from automatically adding whitespace character in SQL Server?

I have a problem about SQL Server in my visual studio 2012 web site project. For example; I have a table named "user" that has columns "name" ( varchar(16) ) and "surname" ( varchar(16) ). I add new data such as name: "john" surname: "holiday", but SQL Server automatically fills the rest of the variable with whitespace character. It seems like . How can I block this? I hope I explained my problem clearly.

protected void LoginButton_Click(object sender, EventArgs e)
{
    string connectionString = "Data Source=MYPC-PC\\SQLEXPRESS;Initial Catalog=fff;Integrated Security=True";
    SqlConnection connection = new SqlConnection(connectionString);
    try
    {
        connection.Open();
    }
    catch (Exception s)
    {
        FailureText.Text = s.ToString();
    }
    SqlCommand cmd = new SqlCommand("SELECT * FROM [person] WHERE user_name='" + UserName.Text + "' AND user_password='" + Password.Text + "' ", connection);

    SqlDataReader dr = null;
    dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        if (this.CompareStrings(dr["user_name"].ToString(), UserName.Text) &&
            this.CompareStrings(dr["user_password"].ToString(), Password.Text))
        {
            FailureText.Text = "ok";
        }
        else
        {
            FailureText.Text = "no";
        }


    }
    connection.Close();
    dr.Close();
}

For example when I am debugging "dr["user_name"]" variable is like that in watch :

Upvotes: 0

Views: 3312

Answers (2)

Sandip Bantawa
Sandip Bantawa

Reputation: 2880

this.CompareStrings(dr["user_name"].ToString(), UserName.Text)--> dr["user_name"].ToString().Trim()

might solve your problem if anyway there are some extra whitespaces

Upvotes: 1

Lock
Lock

Reputation: 5522

You must have used the ANSI_PADDING setting when you created the table. If so, you'll need to drop and create the table again.

Upvotes: 1

Related Questions