473,327 Members | 2,025 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,327 software developers and data experts.

Get Column length of string in column

Have below code
AcctNbr give me result of 30.
That is the database column length, Column stores 10 why is giving me
30 ?

while(objRead.Read())
{
AcctNbr = (string)objRead[0].ToString().Length;
Console.WriteLine("Length is {0}",AcctNbr.Length);
}

Nov 17 '05 #1
7 2511
Matt wrote:
Have below code
AcctNbr give me result of 30.
That is the database column length, Column stores 10 why is giving me
30 ?

while(objRead.Read())
{
AcctNbr = (string)objRead[0].ToString().Length;
Console.WriteLine("Length is {0}",AcctNbr.Length);
}


I don't know that I have an answer to your question but it seems what
you are doing here isn't quite right.

This line returns the length of objRead after converting it to a string.
This value is an integer. You then convert that integer into a
string. So AcctNbr now hold something like 10 in string form.
AcctNbr = (string)objRead[0].ToString().Length;

Here you take your string value of 10 and get the length of that string
value. so the answer to AcctNbr.Length would be 2. I don't think this
is what you ment to do.
Console.WriteLine("Length is {0}",AcctNbr.Length);

Chris
Nov 17 '05 #2
I just like to get the string lenght of the stored value ?
so i can compare that length..

Nov 17 '05 #3
Matt wrote:
I just like to get the string lenght of the stored value ?
so i can compare that length..

What is the object type in the datareader? String?

int intLen

intLen = objRead[0].ToString().Length
or
intLen = objRead.GetString(0).Length

Console.WriteLine("Length is {0}",intLen.ToString())

That should give you the length.
Chris
Nov 17 '05 #4
Chris am still getting 30. I have tried those before thats where i
dont get it.
data column is char and stored 10 char value '5559878789'

int intlen;
while(objRead.Read())
{

intLen = objRead.GetString(0).Length;
Console.WriteLine("Length is {0}",intLen.ToString()) ;
}
Length is 30

Nov 17 '05 #5
If anyone interested, I just used trim to get rid of the space for
string.
It sucks. but what can u do Thats MS for you.
intLen = objRead.GetString(0).Length;
Console.WriteLine("Length is {0}",intLen.ToString());
Console.WriteLine(objRead[0].GetType());
string x = objRead[0].ToString();
Console.WriteLine("String x is {0}",x);
Console.WriteLine(x.Length);
string y = x.Trim();
Console.WriteLine(y.Length);

Nov 17 '05 #6
Matt wrote:
If anyone interested, I just used trim to get rid of the space for
string.
It sucks. but what can u do Thats MS for you.
intLen = objRead.GetString(0).Length;
Console.WriteLine("Length is {0}",intLen.ToString());
Console.WriteLine(objRead[0].GetType());
string x = objRead[0].ToString();
Console.WriteLine("String x is {0}",x);
Console.WriteLine(x.Length);
string y = x.Trim();
Console.WriteLine(y.Length);


You have the variable in the database declared as Char(30) I bet.
Change it to varchar(30) and it will work the way you were expecting.

Chris
Nov 17 '05 #7
Not Allowed to change database
Thanks though

Nov 17 '05 #8

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

Similar topics

4
by: Peter Scott | last post by:
I created a table that has a column in that needs to contain a full Unix file path. Since 2048 was too long for a VARCHAR, I made it TEXT. I since populated the table. Now I want to make the...
12
by: Philip Sherman | last post by:
I'm trying to copy production statistics to a test database and can't set the column statistics for a VARGRAPHIC column to match what RUNSTATS generated on production. The reason code and some...
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
3
by: PeterZ | last post by:
Hi, In a running C# app with a datagrid control I select all rows in the dataGrid using CTRL-A, I then paste into some other app like notepad or Word but the column headings get left off. Is...
1
by: venky | last post by:
HI guys, In one of my columns in the grid, i want to set the width of the grid to the widest entry in the column when i click on the column seperator. How can i calcualte the column width if i...
6
by: Aaron Smith | last post by:
Is there a way to put a limit on the text size of a datagrid column? Thanks, Aaron -- --- Aaron Smith Remove -1- to E-Mail me. Spam Sucks.
6
by: CindyH | last post by:
Hi Does anyone know how to create a multi column combo box from the same table? Thanks, Cindy
2
by: Javier | last post by:
Hi Everyone, I have a dynamic checkbox in a datagrid that uses the ITemplate interface and has the checkchanged event wired up. When the checkbox is checked, the event event handler that...
9
by: Mel | last post by:
I have 10 columns total. 3 of them are invisible. The rest are read- only BoundFields, 3 of which are editable fields using TemplateFields. Upon editing, I want to validate what the user enters...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.