Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

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
Avatar of Eyal
Eyal
Flag of Israel image

check that the type starts with n (nchar,nvarchar,ntext)
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...
Avatar of lapucca
lapucca

ASKER

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

in the table all seems ok

post the definition of usp_InsertNewDoctor
ASKER CERTIFIED SOLUTION
Avatar of Bill Nolan
Bill Nolan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca

ASKER

usp_InsertNewDoctor is the attached file when I open this question.  
Of course Slimfinger has the right of it...
Avatar of lapucca

ASKER

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);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca

ASKER

Thank you.