[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Why I get all these extra blanks after I used Trim()?

The LastName in the database would get a lot of extra trailing blanks.  Why?  Attached file is the sql sp called from the code snipt.
SqlDataSource sdsDoctors = new SqlDataSource();
        sdsDoctors.ConnectionString = WebConfigurationManager.ConnectionStrings["AdaptSurveyConnectionString"].ToString();
        sdsDoctors.InsertCommand = "usp_InsertNewDoctor";
        sdsDoctors.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
        sdsDoctors.InsertParameters.Add("FirstName", txtFirstName.Text.Trim());
        sdsDoctors.InsertParameters.Add("LastName", txtLastName.Text.Trim());
        int success = sdsDoctors.Insert();

Open in new window

usp-InsertNewDoctor.sql
0
lapucca
Asked:
lapucca
  • 4
  • 4
  • 2
  • +1
2 Solutions
 
EyalCommented:
check that the type starts with n (nchar,nvarchar,ntext)
0
 
Dale BurrellDirectorCommented:
And if your column types are nchar/varchar/ntext then split out the trim statement and see if its working e.g.

String TempFirstName = txtFirstName.Text.Trim();
sdsDoctors.InsertParameters.Add("FirstName", TempFirstName);

Slap a breakpoint on the line and inspect TempFirstName, and if it hasn't worked look at the char code - it may not be a regular space.

But my guess will be the table definition as Eyal says...
0
 
lapuccaAuthor Commented:
Here is my table definition.  I can't remember why I used nchar type.  Does this type pad blanks at the end?
CREATE TABLE [dbo].[Doctors](
	[DoctorId] [int] IDENTITY(1,1) NOT NULL,
	[LastName] [nchar](30) NOT NULL,
	[FirstName] [nchar](30) NULL,
	[FullName] [nchar](60) NULL,
 CONSTRAINT [PK_Doctors] PRIMARY KEY CLUSTERED 
(
	[DoctorId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
EyalCommented:
in the table all seems ok

post the definition of usp_InsertNewDoctor
0
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
I believe you want 'varchar' instead of 'char' (unicode doesn't matter)
0
 
lapuccaAuthor Commented:
usp_InsertNewDoctor is the attached file when I open this question.  
0
 
Dale BurrellDirectorCommented:
Of course Slimfinger has the right of it...
0
 
lapuccaAuthor Commented:
I did check the LastName text in debug and it doesn't have any extra space.
        string lName = txtLastName.Text.Trim();
        sdsDoctors.InsertParameters.Add("LastName", lName);
0
 
EyalCommented:
you should change all fields to nvarchar

nvarchar

n=unicode
var=variable (trims the input)
0
 
lapuccaAuthor Commented:
Thank you.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now