473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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(gconSqlConnection)
cnFalcon.Open()

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

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

Dim dr As SqlDataReader =
cmGetUser.ExecuteReaderCommandBehavior.SingleRow)

Do While dr.Read()
MessageBox.Show(Len(dr.Item("fname")).ToString)
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 3232
Try this:

Do While dr.Read()
MsgBox(dr("fname").ToString.Length, MsgBoxStyle.OkOnly)
Loop

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

Do While dr.Read()
MsgBox(dr(0).ToString.Length, MsgBoxStyle.OkOnly)
Loop

dc*********@gmail.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(gconSqlConnection)
cnFalcon.Open()

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

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

Dim dr As SqlDataReader =
cmGetUser.ExecuteReaderCommandBehavior.SingleRow)

Do While dr.Read()
MessageBox.Show(Len(dr.Item("fname")).ToString)
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("fname").ToString.Length, MsgBoxStyle.OkOnly)
Loop

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

Do While dr.Read()
MsgBox(dr(0).ToString.Length, MsgBoxStyle.OkOnly)
Loop

dc*********@gmail.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(gconSqlConnection)
cnFalcon.Open()

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

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

Dim dr As SqlDataReader =
cmGetUser.ExecuteReaderCommandBehavior.SingleRow)

Do While dr.Read()
MessageBox.Show(Len(dr.Item("fname")).ToString)
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*********@gmail.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("fname").ToString.Length, MsgBoxStyle.OkOnly)
Loop

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

Do While dr.Read()
MsgBox(dr(0).ToString.Length, MsgBoxStyle.OkOnly)
Loop

dc*********@gmail.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(gconSqlConnection)
cnFalcon.Open()
>
' Define query, assign to strSql.
Dim strSql As String = "Select fname, len(fname) as flen,lname from
tblUsers where username like '" & txtLastName.Text & "'"
>
' Create new action command.
Dim cmGetUser As New SqlCommand(strSql, cnFalcon)
>
Dim dr As SqlDataReader =
cmGetUser.ExecuteReaderCommandBehavior.SingleRow)
>
Do While dr.Read()
MessageBox.Show(Len(dr.Item("fname")).ToString)
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
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...
5
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...
6
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...
7
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...
22
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...
2
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...
13
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...
5
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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,...

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.