views:

20

answers:

2

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).

+2  A: 

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.

Fatih
It doesn't but he should <g>.
Lieven
Ah, I'm only just starting to learn ASP.Net and SQL. I figured it had something to do with me using char(50) but was confused why it wouldn't happen to the username row. The username row is definitely char(50) though and it definitely is not padding out spaces.. strange, it also is a primary key though so maybe that's why? But yeah, making password varchar fixed the problem :)
meds
+2  A: 

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.

Richard