meds
meds

Reputation: 22916

Why are the white spaces added to a row in my sql table?

I'm not sure why this is, basically when I insert a row using the following function:

    public bool CreateUser(string username, string password, string email, int age = 0)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = "Data Source=localhost\\SQLEXPRESS;" +
        "Initial Catalog=Pubs;Integrated Security=SSPI;Trusted_Connection=True;";

        try
        {
            myConnection.Open();

            string query = "INSERT INTO SiteUser VALUES('" + username + "', '" +  password + "', '" + email + "', " + age + ")";
            SqlCommand cmd = new SqlCommand(query);
            cmd.Connection = myConnection;
            cmd.ExecuteNonQuery();

            myConnection.Close();
        }
        catch (Exception excep)
        {
            string error = excep.ToString();
            Response.Write(error);

            return false;
        }


        return true;
    }

The field 'Password' ends up having white spaces in it (as in "Password "). So whatever the password is it's trailed by white spaces.

The strange thing is this doesn't happen to the field 'UserName', both are of type char(50).

Upvotes: 1

Views: 366

Answers (2)

Richard
Richard

Reputation: 22016

Are you sure one is not varchar(50) and one is char(50). You will find that database columns with char specified will always be that length.

Upvotes: 3

Fatih
Fatih

Reputation: 158

I don't know if this answers your question but why don't you use varchar type instead of char. Or you can use TRIM() function in your select query.

Upvotes: 2

Related Questions