Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Varchar in sql creating extra whitespaces

Well, I am building a small hobby program in C#, and I am trying to write information from my program to Sql

The thing is I have some varchar fields in Sql with a max length of 50 when I send information to fill those columns they will be added with extra space until reaching 50 chars

I already tried to change varchar to nvarchar just to see if it works

What I have been researching is that nchar does this kind of thing but with varchar I shouldn't have problem but I am having them..

public PersonModel CreatePerson(PersonModel model)
{
    using (IDbConnection connection =
        new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
    {
        var p = new DynamicParameters();
        p.Add("@FirstName", model.FirstName);
        p.Add("@LastName", model.LastName);
        p.Add("@EmailAddress", model.EmailAddress);
        p.Add("@CellphoneNumber", model.CellphoneNumber);
        p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);

        connection.Execute("dbo.spPeople_Insert", p, commandType: CommandType.StoredProcedure);
        model.Id = p.Get<int>("@id");

        return model;
    }
}

and here is how i call it

    if (ValidateForm())
    {
        PersonModel p = new PersonModel();
        p.FirstName = firstNameValue.Text;
        p.LastName = lastNameValue.Text;
        p.EmailAddress = emailValue.Text;
        p.CellphoneNumber = cellphoneValue.Text;

those are textbox in my form btw

like image 370
Alejandro Sánchez Avatar asked Nov 19 '25 12:11

Alejandro Sánchez


1 Answers

According to Microsoft You need to set SET ANSI_PADDING OFF
please note that existing columns will not be be affected by changing the setting only new ones

like image 175
Cyber Progs Avatar answered Nov 21 '25 05:11

Cyber Progs