Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does VB.NET trim trailing spaces of DataTable fields? 1

Status
Not open for further replies.

ZuperDuper

Programmer
Jun 4, 2004
39
US
I think I've asked a simmilar question, got no response, so I try againg.
If I try to open a database in my case I tried DBASE3 and MDB, and try to access the field that is empty, I get NULL value - thats fine.

When I replace those NULL fields with spaces, I still get a NULL value. If I try following, I get undesired result.
myString=RS.Fields("myfield").value
myString gets "" and not " "


When I replace a length 3 field that is NULL with "1 ", it gets value of "1" once opened through .NET.
myString=RS.Fields("myfield").value
myString gets "1" and not "1 "


Can anyone help me out on this one? Is it possible to set the recordset or the field to be fixedlength?

You should never underestimate the predictability of stupidity.
 
One, you are using Classic ADO, which isn't used often with .Net. So there could be some weird bug type problems that haven't had much written about them.

Two, how are are you replacing this values?

Three, there could be a better way to accomplish whatever you are wishing to do. If you are reading records and populating controls with their values, then you can do an if then else statement to check for nulls and put in empty spaces. Or, you can just construct your SQL statement to take care of the nulls. (See ISNULL() for SQL Server, NULLIF() for DB2, etc).

Four, why do you need space padding for Nulls?

Five, for your question in which your replace the value with a 1, is the the same field? If not, it could be numeric, therefore, "1 " doesn't make sense.
 
One, you are using Classic ADO, which isn't used often with .Net. So there could be some weird bug type problems that haven't had much written about them.


----I've tried OleDb, same result

Two, how are are you replacing this values?
----Well, in DBASE3, with a replace statement:
replace all <myfield> with "value", I know that DBASE3 does not support NULLs

Three, there could be a better way to accomplish whatever you are wishing to do. If you are reading records and populating controls with their values, then you can do an if then else statement to check for nulls and put in empty spaces. Or, you can just construct your SQL statement to take care of the nulls. (See ISNULL() for SQL Server, NULLIF() for DB2, etc).
----I know, that I can do this, but the thing is that I am working with many tables and many fields within each, I think that its easier to get fixed length field (if its possible at all)

Four, why do you need space padding for Nulls?
-----Not for Nulls, but I need to check substring of the resulting string of the field, and if the field is trimmed, I do get outbounds error.
Five, for your question in which your replace the value with a 1, is the the same field? If not, it could be numeric, therefore, "1 " doesn't make sense.
---Its text.

You should never underestimate the predictability of stupidity.
 
I think I get what you are trying to do. In a sense, the database is not normalized compared to how it is used. So for example, a field will contain some information at certain positions in the field. For example, in the values "ABC856OOP", you might only want the "856" in the middle?

Lets say you want each field to have a fixed-length of 9 characters, then instead of doing this: myString=RS.Fields("myfield").value, try this

Code:
myString = Trim(Cstr(RS.Fields("myfield").value)).PadRight(9, " ")


I still think most of the problem is that the database is not using fixed-length fields. I am confused by saying your database does not support nulls, but you are replacing them. Some databases will have a Padding type function, for example REPLICATE() in SQL Server. See if your's has one, so you can bring the fields over as fixed-length.
 
Yeah, it looks like I will have to do something like that- create bunch of temp strings from the fields and work with them. Its a pain, but looks like that is the only option I have right now. Thanks...

You should never underestimate the predictability of stupidity.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top