PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET White Space Problem

Reply

White Space Problem

 
Thread Tools Rate Thread
Old 25-08-2003, 06:05 PM   #1
Carlo Razzeto
Guest
 
Posts: n/a
Default White Space Problem


First of all, sorry about cross posting but I wasn't sure which group this
question was more appropriate for...

I am having a problem with Microsoft SQL Server or SqlDataReader (most
likely the former) apparently appending white space to the end of database
results... The code I have looks like:

Dim ConnectionString = "Data Source=(local); User=sa; Password=Oper64Hammer;
Initial Catalog=my_work"
Dim Query = "SELECT UserName, PassWord, UserLevel FROM UserData WHERE
UserName = '" + user + "'"
Dim SQLConnection As New SqlConnection(ConnectionString)
SQLConnection.Open()
Dim Command As New SqlCommand(Query, SQLConnection)
Dim Reader As SqlDataReader = Command.ExecuteReader()

For some reason when I read from the password column of the result set I get
the password with with one black space appended to the end of the string.
This forced me to write store my password in a string then use the
string.trim method to remove the white space so I could accurately compare
the database result to the password entered by the user. What causes this
problem? And is there any way to correct it? I would hate to have to store
all my database results as strings so I can trim them when ever I need to
compare results to user input. Thanks for any help,

Carlo


  Reply With Quote
Old 25-08-2003, 06:41 PM   #2
Marina
Guest
 
Posts: n/a
Default Re: White Space Problem

What data type is the column set to in sql server? Is it a char or varchar?

"Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
news:%23jkDnKyaDHA.2024@TK2MSFTNGP12.phx.gbl...
> First of all, sorry about cross posting but I wasn't sure which group this
> question was more appropriate for...
>
> I am having a problem with Microsoft SQL Server or SqlDataReader (most
> likely the former) apparently appending white space to the end of database
> results... The code I have looks like:
>
> Dim ConnectionString = "Data Source=(local); User=sa;

Password=Oper64Hammer;
> Initial Catalog=my_work"
> Dim Query = "SELECT UserName, PassWord, UserLevel FROM UserData WHERE
> UserName = '" + user + "'"
> Dim SQLConnection As New SqlConnection(ConnectionString)
> SQLConnection.Open()
> Dim Command As New SqlCommand(Query, SQLConnection)
> Dim Reader As SqlDataReader = Command.ExecuteReader()
>
> For some reason when I read from the password column of the result set I

get
> the password with with one black space appended to the end of the string.
> This forced me to write store my password in a string then use the
> string.trim method to remove the white space so I could accurately compare
> the database result to the password entered by the user. What causes this
> problem? And is there any way to correct it? I would hate to have to store
> all my database results as strings so I can trim them when ever I need to
> compare results to user input. Thanks for any help,
>
> Carlo
>
>



  Reply With Quote
Old 25-08-2003, 06:47 PM   #3
William \(Bill\) Vaughn
Guest
 
Posts: n/a
Default Re: White Space Problem

How are the columns defined in your UserData table?
Now that we know the SA password, all we need to know is ...
As a rule you should rarely (if ever) connect with the SA login. Keep the
password to yourself and create roles/permissions/logins to develop with and
use permissions to grant access.

--
____________________________________
Bill Vaughn
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
news:%23jkDnKyaDHA.2024@TK2MSFTNGP12.phx.gbl...
> First of all, sorry about cross posting but I wasn't sure which group this
> question was more appropriate for...
>
> I am having a problem with Microsoft SQL Server or SqlDataReader (most
> likely the former) apparently appending white space to the end of database
> results... The code I have looks like:
>
> Dim ConnectionString = "Data Source=(local); User=sa;

Password=Oper64Hammer;
> Initial Catalog=my_work"
> Dim Query = "SELECT UserName, PassWord, UserLevel FROM UserData WHERE
> UserName = '" + user + "'"
> Dim SQLConnection As New SqlConnection(ConnectionString)
> SQLConnection.Open()
> Dim Command As New SqlCommand(Query, SQLConnection)
> Dim Reader As SqlDataReader = Command.ExecuteReader()
>
> For some reason when I read from the password column of the result set I

get
> the password with with one black space appended to the end of the string.
> This forced me to write store my password in a string then use the
> string.trim method to remove the white space so I could accurately compare
> the database result to the password entered by the user. What causes this
> problem? And is there any way to correct it? I would hate to have to store
> all my database results as strings so I can trim them when ever I need to
> compare results to user input. Thanks for any help,
>
> Carlo
>
>



  Reply With Quote
Old 25-08-2003, 07:32 PM   #4
Carlo Razzeto
Guest
 
Posts: n/a
Default Re: White Space Problem

Thanks for the advice... This is actually not an internet accessable site
right now, it's behind my broadband routers firewall... I would be a little
more careful if this was going to be a "real" web site on the internet but
since I'm just getting familiar with .Net (in this case VB of course) I was
just being... well... lazy...

Carlo

"William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
news:OUKTRhyaDHA.1816@TK2MSFTNGP09.phx.gbl...
> How are the columns defined in your UserData table?
> Now that we know the SA password, all we need to know is ...
> As a rule you should rarely (if ever) connect with the SA login. Keep the
> password to yourself and create roles/permissions/logins to develop with

and
> use permissions to grant access.
>
> --
> ____________________________________
> Bill Vaughn
> MVP, hRD
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no

rights.
> __________________________________
>
> "Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
> news:%23jkDnKyaDHA.2024@TK2MSFTNGP12.phx.gbl...
> > First of all, sorry about cross posting but I wasn't sure which group

this
> > question was more appropriate for...
> >
> > I am having a problem with Microsoft SQL Server or SqlDataReader (most
> > likely the former) apparently appending white space to the end of

database
> > results... The code I have looks like:
> >
> > Dim ConnectionString = "Data Source=(local); User=sa;

> Password=Oper64Hammer;
> > Initial Catalog=my_work"
> > Dim Query = "SELECT UserName, PassWord, UserLevel FROM UserData WHERE
> > UserName = '" + user + "'"
> > Dim SQLConnection As New SqlConnection(ConnectionString)
> > SQLConnection.Open()
> > Dim Command As New SqlCommand(Query, SQLConnection)
> > Dim Reader As SqlDataReader = Command.ExecuteReader()
> >
> > For some reason when I read from the password column of the result set I

> get
> > the password with with one black space appended to the end of the

string.
> > This forced me to write store my password in a string then use the
> > string.trim method to remove the white space so I could accurately

compare
> > the database result to the password entered by the user. What causes

this
> > problem? And is there any way to correct it? I would hate to have to

store
> > all my database results as strings so I can trim them when ever I need

to
> > compare results to user input. Thanks for any help,
> >
> > Carlo
> >
> >

>
>



  Reply With Quote
Old 25-08-2003, 08:20 PM   #5
Marina
Guest
 
Posts: n/a
Default Re: White Space Problem

When you define a columns as char(15), that means there will always be 15
characters in that field. If you put anything smaller in, it will be padded
with spaces such so the field is always 15 in length. So if your string has
12 character, there will be 3 spaces afterwards.

If you do not want this behavior, use a varchar(15) type. This holds up to
15 characters, but does not pad with spaces.

"Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
news:OA6YO6yaDHA.2328@TK2MSFTNGP09.phx.gbl...
> Table is setup as follows
> UserData
> UserName char(15)
> Password char(15)
> UserLevel tinyint
>
> Carlo
>
> "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
> news:ei%23R6eyaDHA.4028@tk2msftngp13.phx.gbl...
> > What data type is the column set to in sql server? Is it a char or

> varchar?
> >
> > "Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
> > news:%23jkDnKyaDHA.2024@TK2MSFTNGP12.phx.gbl...
> > > First of all, sorry about cross posting but I wasn't sure which group

> this
> > > question was more appropriate for...
> > >
> > > I am having a problem with Microsoft SQL Server or SqlDataReader (most
> > > likely the former) apparently appending white space to the end of

> database
> > > results... The code I have looks like:
> > >
> > > Dim ConnectionString = "Data Source=(local); User=sa;

> > Password=Oper64Hammer;
> > > Initial Catalog=my_work"
> > > Dim Query = "SELECT UserName, PassWord, UserLevel FROM UserData WHERE
> > > UserName = '" + user + "'"
> > > Dim SQLConnection As New SqlConnection(ConnectionString)
> > > SQLConnection.Open()
> > > Dim Command As New SqlCommand(Query, SQLConnection)
> > > Dim Reader As SqlDataReader = Command.ExecuteReader()
> > >
> > > For some reason when I read from the password column of the result set

I
> > get
> > > the password with with one black space appended to the end of the

> string.
> > > This forced me to write store my password in a string then use the
> > > string.trim method to remove the white space so I could accurately

> compare
> > > the database result to the password entered by the user. What causes

> this
> > > problem? And is there any way to correct it? I would hate to have to

> store
> > > all my database results as strings so I can trim them when ever I need

> to
> > > compare results to user input. Thanks for any help,
> > >
> > > Carlo
> > >
> > >

> >
> >

>
>



  Reply With Quote
Old 25-08-2003, 11:22 PM   #6
William \(Bill\) Vaughn
Guest
 
Posts: n/a
Default Re: White Space Problem

Marina is right. We rarely use Char anymore for anything except those fields
where we know the length will ALWAYS be a fixed number of characters. This
is handy for part numbers or somesuch but anytime you're working with a
field that varies in length, use a VarChar. In the "olden days", we often
looked for small ways to help reduce the size of the DB and using CHAR made
more sense back then. Nowadays it's more trouble than it's worth to use
CHAR.

hth

--
____________________________________
Bill Vaughn
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Marina" <zlatkinam@nospam.hotmail.com> wrote in message
news:%231BsTWzaDHA.1816@TK2MSFTNGP09.phx.gbl...
> When you define a columns as char(15), that means there will always be 15
> characters in that field. If you put anything smaller in, it will be

padded
> with spaces such so the field is always 15 in length. So if your string

has
> 12 character, there will be 3 spaces afterwards.
>
> If you do not want this behavior, use a varchar(15) type. This holds up

to
> 15 characters, but does not pad with spaces.
>
> "Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
> news:OA6YO6yaDHA.2328@TK2MSFTNGP09.phx.gbl...
> > Table is setup as follows
> > UserData
> > UserName char(15)
> > Password char(15)
> > UserLevel tinyint
> >
> > Carlo
> >
> > "Marina" <zlatkinam@nospam.hotmail.com> wrote in message
> > news:ei%23R6eyaDHA.4028@tk2msftngp13.phx.gbl...
> > > What data type is the column set to in sql server? Is it a char or

> > varchar?
> > >
> > > "Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
> > > news:%23jkDnKyaDHA.2024@TK2MSFTNGP12.phx.gbl...
> > > > First of all, sorry about cross posting but I wasn't sure which

group
> > this
> > > > question was more appropriate for...
> > > >
> > > > I am having a problem with Microsoft SQL Server or SqlDataReader

(most
> > > > likely the former) apparently appending white space to the end of

> > database
> > > > results... The code I have looks like:
> > > >
> > > > Dim ConnectionString = "Data Source=(local); User=sa;
> > > Password=Oper64Hammer;
> > > > Initial Catalog=my_work"
> > > > Dim Query = "SELECT UserName, PassWord, UserLevel FROM UserData

WHERE
> > > > UserName = '" + user + "'"
> > > > Dim SQLConnection As New SqlConnection(ConnectionString)
> > > > SQLConnection.Open()
> > > > Dim Command As New SqlCommand(Query, SQLConnection)
> > > > Dim Reader As SqlDataReader = Command.ExecuteReader()
> > > >
> > > > For some reason when I read from the password column of the result

set
> I
> > > get
> > > > the password with with one black space appended to the end of the

> > string.
> > > > This forced me to write store my password in a string then use the
> > > > string.trim method to remove the white space so I could accurately

> > compare
> > > > the database result to the password entered by the user. What causes

> > this
> > > > problem? And is there any way to correct it? I would hate to have to

> > store
> > > > all my database results as strings so I can trim them when ever I

need
> > to
> > > > compare results to user input. Thanks for any help,
> > > >
> > > > Carlo
> > > >
> > > >
> > >
> > >

> >
> >

>
>



  Reply With Quote
Old 26-08-2003, 01:17 AM   #7
Carlo Razzeto
Guest
 
Posts: n/a
Default Re: White Space Problem

Thanks guys, that really helped!

Carlo

"William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
news:eIsko60aDHA.2672@tk2msftngp13.phx.gbl...
> Marina is right. We rarely use Char anymore for anything except those

fields
> where we know the length will ALWAYS be a fixed number of characters. This
> is handy for part numbers or somesuch but anytime you're working with a
> field that varies in length, use a VarChar. In the "olden days", we often
> looked for small ways to help reduce the size of the DB and using CHAR

made
> more sense back then. Nowadays it's more trouble than it's worth to use
> CHAR.
>
> hth
>
> --
> ____________________________________
> Bill Vaughn
> MVP, hRD
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no

rights.
> __________________________________



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off