473,664 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sqldatareader padding varchar fields with extra white space

I am currently developing a small windows application using Visual
Basic via Visual Studio 2005. My database resides on a SQL 2000 server.

I have a table with three fields:

id (int, Not Null)
lname (varchar(30), Not Null)
fname (varchar(30), Not Null)

I have one record in the table as follows:

1 Smith Joe

When I run the following query in query analyzer I see that the length
of the fname field is 3:

select *, len(fname) from tblmytable

I created a form in Visual Studio 2005 using Visual Basic with 1 button
on it and a text field for a last name. That button runs the query
using an SQL data reader and then popups up a few message boxes. The
code is as follows:

' Create, open connection to database.
Dim cnFalcon As New SqlConnection(g conSqlConnectio n)
cnFalcon.Open()

' Define query, assign to strSql.
Dim strSql As String = "Select fname, len(fname) as flen,lname from
tblUsers where username like '" & txtLastName.Tex t & "'"

' Create new action command.
Dim cmGetUser As New SqlCommand(strS ql, cnFalcon)

Dim dr As SqlDataReader =
cmGetUser.Execu teReaderCommand Behavior.Single Row)

Do While dr.Read()
MessageBox.Show (Len(dr.Item("f name")).ToStrin g)
MessageBox.Show (dr.Item("flen" ))
Loop

dr.Close()

cnFalcon.Close( )

When I run my application and provide a last name I see two message
boxes.

The first message box shows the length of the raw fname field as being
30, which is incorrect.

The second message box shows the length of the flen alias as being 3,
which is correct.

Does anyone have some insight as to why the sqldatareader is padding
the data that is returned from my varchar fields with extra spaces?

Best regards,

Locker

P.S. I know I can use RTRIM in the query or TrimEnd in the VB code to
work around this but I feel that I should not have to do this. The
Sqldatareader query should return the data exactly as it's returned if
I run the query directly via Query Analyzer.

Sep 20 '06 #1
3 3255
Try this:

Do While dr.Read()
MsgBox(dr("fnam e").ToString.Le ngth, MsgBoxStyle.OkO nly)
Loop

Also is more efficient if you reference the column by ordinal rather
then name, like this:

Do While dr.Read()
MsgBox(dr(0).To String.Length, MsgBoxStyle.OkO nly)
Loop

dc*********@gma il.com wrote:
I am currently developing a small windows application using Visual
Basic via Visual Studio 2005. My database resides on a SQL 2000 server.

I have a table with three fields:

id (int, Not Null)
lname (varchar(30), Not Null)
fname (varchar(30), Not Null)

I have one record in the table as follows:

1 Smith Joe

When I run the following query in query analyzer I see that the length
of the fname field is 3:

select *, len(fname) from tblmytable

I created a form in Visual Studio 2005 using Visual Basic with 1 button
on it and a text field for a last name. That button runs the query
using an SQL data reader and then popups up a few message boxes. The
code is as follows:

' Create, open connection to database.
Dim cnFalcon As New SqlConnection(g conSqlConnectio n)
cnFalcon.Open()

' Define query, assign to strSql.
Dim strSql As String = "Select fname, len(fname) as flen,lname from
tblUsers where username like '" & txtLastName.Tex t & "'"

' Create new action command.
Dim cmGetUser As New SqlCommand(strS ql, cnFalcon)

Dim dr As SqlDataReader =
cmGetUser.Execu teReaderCommand Behavior.Single Row)

Do While dr.Read()
MessageBox.Show (Len(dr.Item("f name")).ToStrin g)
MessageBox.Show (dr.Item("flen" ))
Loop

dr.Close()

cnFalcon.Close( )

When I run my application and provide a last name I see two message
boxes.

The first message box shows the length of the raw fname field as being
30, which is incorrect.

The second message box shows the length of the flen alias as being 3,
which is correct.

Does anyone have some insight as to why the sqldatareader is padding
the data that is returned from my varchar fields with extra spaces?

Best regards,

Locker

P.S. I know I can use RTRIM in the query or TrimEnd in the VB code to
work around this but I feel that I should not have to do this. The
Sqldatareader query should return the data exactly as it's returned if
I run the query directly via Query Analyzer.
Sep 20 '06 #2
I added a third message box using your format. This new message box
also returns 30 as the length.

Just to give a bit of additional information, it's not just that the
length function returns 30. There are actually 27 spaces being added to
the field so if I display the fname + lname from the query you can
physically see a ton of extra whitespaces on the end of the first name.

Thanks,

Dan
Izzy wrote:
Try this:

Do While dr.Read()
MsgBox(dr("fnam e").ToString.Le ngth, MsgBoxStyle.OkO nly)
Loop

Also is more efficient if you reference the column by ordinal rather
then name, like this:

Do While dr.Read()
MsgBox(dr(0).To String.Length, MsgBoxStyle.OkO nly)
Loop

dc*********@gma il.com wrote:
I am currently developing a small windows application using Visual
Basic via Visual Studio 2005. My database resides on a SQL 2000 server.

I have a table with three fields:

id (int, Not Null)
lname (varchar(30), Not Null)
fname (varchar(30), Not Null)

I have one record in the table as follows:

1 Smith Joe

When I run the following query in query analyzer I see that the length
of the fname field is 3:

select *, len(fname) from tblmytable

I created a form in Visual Studio 2005 using Visual Basic with 1 button
on it and a text field for a last name. That button runs the query
using an SQL data reader and then popups up a few message boxes. The
code is as follows:

' Create, open connection to database.
Dim cnFalcon As New SqlConnection(g conSqlConnectio n)
cnFalcon.Open()

' Define query, assign to strSql.
Dim strSql As String = "Select fname, len(fname) as flen,lname from
tblUsers where username like '" & txtLastName.Tex t & "'"

' Create new action command.
Dim cmGetUser As New SqlCommand(strS ql, cnFalcon)

Dim dr As SqlDataReader =
cmGetUser.Execu teReaderCommand Behavior.Single Row)

Do While dr.Read()
MessageBox.Show (Len(dr.Item("f name")).ToStrin g)
MessageBox.Show (dr.Item("flen" ))
Loop

dr.Close()

cnFalcon.Close( )

When I run my application and provide a last name I see two message
boxes.

The first message box shows the length of the raw fname field as being
30, which is incorrect.

The second message box shows the length of the flen alias as being 3,
which is correct.

Does anyone have some insight as to why the sqldatareader is padding
the data that is returned from my varchar fields with extra spaces?

Best regards,

Locker

P.S. I know I can use RTRIM in the query or TrimEnd in the VB code to
work around this but I feel that I should not have to do this. The
Sqldatareader query should return the data exactly as it's returned if
I run the query directly via Query Analyzer.
Sep 20 '06 #3
Oh wait I just reread your original post.

If you can use RTRIM in the SQL query and it resolves the problem then
the spaces are in the table itself.

I always use DataReaders and never experience this behavior.

dc*********@gma il.com wrote:
I added a third message box using your format. This new message box
also returns 30 as the length.

Just to give a bit of additional information, it's not just that the
length function returns 30. There are actually 27 spaces being added to
the field so if I display the fname + lname from the query you can
physically see a ton of extra whitespaces on the end of the first name.

Thanks,

Dan
Izzy wrote:
Try this:

Do While dr.Read()
MsgBox(dr("fnam e").ToString.Le ngth, MsgBoxStyle.OkO nly)
Loop

Also is more efficient if you reference the column by ordinal rather
then name, like this:

Do While dr.Read()
MsgBox(dr(0).To String.Length, MsgBoxStyle.OkO nly)
Loop

dc*********@gma il.com wrote:
I am currently developing a small windows application using Visual
Basic via Visual Studio 2005. My database resides on a SQL 2000 server.
>
I have a table with three fields:
>
id (int, Not Null)
lname (varchar(30), Not Null)
fname (varchar(30), Not Null)
>
I have one record in the table as follows:
>
1 Smith Joe
>
When I run the following query in query analyzer I see that the length
of the fname field is 3:
>
select *, len(fname) from tblmytable
>
I created a form in Visual Studio 2005 using Visual Basic with 1 button
on it and a text field for a last name. That button runs the query
using an SQL data reader and then popups up a few message boxes. The
code is as follows:
>
' Create, open connection to database.
Dim cnFalcon As New SqlConnection(g conSqlConnectio n)
cnFalcon.Open()
>
' Define query, assign to strSql.
Dim strSql As String = "Select fname, len(fname) as flen,lname from
tblUsers where username like '" & txtLastName.Tex t & "'"
>
' Create new action command.
Dim cmGetUser As New SqlCommand(strS ql, cnFalcon)
>
Dim dr As SqlDataReader =
cmGetUser.Execu teReaderCommand Behavior.Single Row)
>
Do While dr.Read()
MessageBox.Show (Len(dr.Item("f name")).ToStrin g)
MessageBox.Show (dr.Item("flen" ))
Loop
>
dr.Close()
>
cnFalcon.Close( )
>
When I run my application and provide a last name I see two message
boxes.
>
The first message box shows the length of the raw fname field as being
30, which is incorrect.
>
The second message box shows the length of the flen alias as being 3,
which is correct.
>
Does anyone have some insight as to why the sqldatareader is padding
the data that is returned from my varchar fields with extra spaces?
>
Best regards,
>
Locker
>
P.S. I know I can use RTRIM in the query or TrimEnd in the VB code to
work around this but I feel that I should not have to do this. The
Sqldatareader query should return the data exactly as it's returned if
I run the query directly via Query Analyzer.
Sep 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2157
by: Prithpal | last post by:
Please help!!! We had a sql server 7 DB, with a char 8 field, in which some of the data was only 7 characters in length. Via a type 4 JDBC driver, we got back a NON space padded String. This server got upgraded to sql server 2000. Now, via the type 4 JDBC driver, we get a String padded field!! I understand there is a ODBC setting for ANSI padding on/off, but the
5
13106
by: Jonathan Daggar | last post by:
Hello, I'm trying to put together a form with a very tight table formatting. However, every time I put an text-type input field in, the browser pads the area to the right of it with space. I've tried to eliminate it several ways, but it won't go away. It looks like it's forcing at least one space (line break?) in after each field, and then there's some mystery padding on the right that also refuses to go away. I've put up a demo of...
6
7549
by: Skip Hollowell | last post by:
I am working on a menu bar for a site, and am using buttons in the bar (because the customer wants to use accessKeys for each selection, apparently it is too much work to click on them with a mouse, but I digress) It seems that the longer the text is in a button, the more padding there is around that text in the button, thus making the button itself much to wide, and wasting too much space on the page. I have tried width:auto in the...
7
6236
by: jmm-list-gn | last post by:
Hello, <http://www.asaom.edu> There are some curious spacing issues with the top bar (gray) and the main navigation bar. The most significant is how the nav bar looks in Opera v6 (win2k): the text portion is has extra space at the top that I cannot remove. As a result the text portion is pushed below the background bar by a few pixels. This does not occur in Mozilla/Firefox, or IE6. (I have ordered Opera v7.) Is this a known issue? The...
22
11578
by: jdokos | last post by:
Hello, I have a question about VARCHAR fields. Our application groups here are starting to use VARCHARs much more frequently. Even VARCHAR (2) to (9) length fields. They say this is because some of the application programs, specifically Java Beans cannot handle the spaces after the value in CHAR fields. Is anyone else seeing this trend?
2
3151
by: JohnR | last post by:
When creating an msAccess db within the Access UI itself the fields that are text are NOT padded with blanks. For example, if I have a 10 char field and put in "HI" and then when I come back to the field and click my mouse on it the cursor is just after the "I" in "HI"... that is, no blanks were added to the field. However, when I create an MDB database programatically in VB.Net using ADOX and create the tables using SQL stmts (ie:...
13
8568
by: lossed | last post by:
Hi everyone, Please excuse me if this has been asked before or sounds a bit dim. This is a question asked on another forum but the solutions being offered are focussing on programming rather than letting the DB server do the work, which I'm not sure is the most efficient solution. However, my confession is I dont use SQL server so can't help them directly with the syntax. Hopefully you can help me help them and learn a little about SQL...
5
10518
by: Tom | last post by:
I am having a problem where an extra gap (whitespace, padding) appears in a table cell where I don't want it. The data fed in the table is via dynamic HTML, so the amount in the middle cell may vary. When the data in the middle table cell is little and that cell's height is small, an unwanted gap appears in the first table cell. I would prefer to have all additional blank space at the bottom of the table (last row) and not have any in...
0
27245
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications are constrained by the technology available at the time of their development and hence aren’t optimum as per current scenario. One of such cases is the extensive usage of CHAR fields, which aren’t optimum solution for space storage now. This paper...
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.