Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space character at the end of a string when saving to the database

I have a nvarchar(50) field in my table. When I save a string with 12 character Length for example, it saves the string with 50 character length. Actually it adds a space character at the end of the string. Should I select another data type?

tmp_Person.NameFamily = txt_NameFamily.Text.Trim();
PersonKBBSHDataContext.PersonInfos.InsertOnSubmit(tmp_Person);
PersonKBBSHDataContext.SubmitChanges();

When I save a string directly (in SQL Server) in the NameFamily field of the table, everything is fine.

like image 696
hamze Avatar asked Jun 22 '12 08:06

hamze


3 Answers

I had same problem. Remove .IsFixedLength() from Nvarchar properties in Mapping class and my problem solved.

Me.Property(Function(t) t.Description).IsFixedLength().HasMaxLength(500)

Replaced with

Me.Property(Function(t) t.Description).HasMaxLength(500)
like image 193
Manny Avatar answered Oct 15 '22 00:10

Manny


If using the Entity Framework, set the Fixed Length property to true on your NVarchar fields.

You can still use the Trim however that is just to 'fix' the textBox.Text value and has nothing to do with the database.

like image 22
Myrtle Avatar answered Oct 15 '22 01:10

Myrtle


I found the problem. I just added my table again in the .dbml file and everything went fine.

like image 38
hamze Avatar answered Oct 15 '22 00:10

hamze