Get Column length of string in column

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

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);
}
 
Matt said:
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
 
Matt said:
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
 
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
 
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);
 
Matt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top