Hi,
i am new to c#. i write a code as below.You see that i obtained columnname and dataType
but How can i get size of field in Table ? Pls help me.

foreach(DataColumn dc in ds.Tables[UpdateTableName].Columns )
{
lFound = false;
cNewName = dc.ColumnName;
cNewType = dc.DataType.ToString();

Thanks a lot.

MaxLength returns sizes for strings, most others are fixed sizes.

Your ds variable looks like it is a DataSet. DataSets normally don't have such things as columnwidths. Use a DataGridView instead.

First,
Thank you for your reply.

You are that MaxLength returns Size . But there is no record in dataset so it returns -1.
pls help me for the same.

MaxLength returns sizes for strings, most others are fixed sizes.

Thanks for your prompt reply.

I will try as you write. thanks again.

If you have a string say ColName, then its size is given by ColName.Length

ColName.Length property returns a length of column. i.e.length of column caption.
but i need a length of field used in column. for eg.

I have a field name city. and columnname is colcity. city length is varchar(20)

I need this length(20) from datacolumn object. pls guide me.

Now we are getting somewhere!
If you pose the right questions you will get the answers quicker!
I or someone else will let you know soon, see ya.

here is the sql code to get it

SELECT
sysobjects.name AS "TABLE_NAME", 
syscolumns.name AS "COLUMN_NAME", 
systypes.name AS "DATA_TYPE", 
syscolumns.LENGTH AS "LENGTH" 
FROM         
	sysobjects 
INNER JOIN 
	syscolumns ON sysobjects.id = syscolumns.id 
INNER JOIN                      
	systypes ON syscolumns.xtype = systypes.xtype 
WHERE     
(sysobjects.xtype = 'U') and
sysobjects.name = 'MyTableName'
ORDER BY sysobjects.name, syscolumns.colid

then in your your datacolumn, you will set maxlength to the value returned from the query for length

commented: whew cool! +1

Perfect. I got what i want. thanks a lot.

Hi

If we want to run a query then need lenght of columns result . may be column dont in any table but we need lenght of it . please answer to me

many thanks for you ddanbe

hi alikolahdoozan

I think that any column must be in a table!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.