Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DataColumn.MaxLength property always returns -1 ?

Posted on 2009-04-08
11
Medium Priority
?
1,085 Views
Last Modified: 2012-05-06
The client application receives the DataSet via a web service call.

Here is the code inside the web service that populates the dataset which is sent back to the client.
Dim DA As SqlDataAdapter
                Dim DS As New DataSet
                Cmd.CommandText = strSelect
                DA = New SqlDataAdapter(Cmd)
                DA.Fill(DS, "RunSQL")
                DA.Dispose()
                Con.Close()
                Return DS

Open in new window

0
Comment
Question by:tfsln
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 6

Expert Comment

by:HarryNS
ID: 24104005
Are you sure that dataset is getting filled up in your web service?

If so, then in your client application you can see the count of columns, DS.Tables[0].Columns.Count
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24104007
What is your strSelect value ?
0
 
LVL 7

Expert Comment

by:Chandan_Gowda
ID: 24104088
check the dataset and make sure, the dataset is properly filled.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 15

Expert Comment

by:oobayly
ID: 24115263
What's the datatype of the column? According the MSDN, "The MaxLength property is ignored for non-text columns."
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.maxlength(VS.80).aspx
0
 

Author Comment

by:tfsln
ID: 24128751
HarryNS: I dont want a column count, i want the max lengths for the columns in the dataset (as specified in the database table design)

The select query is, lets say, "Select * from tablename"

I am not trying to get the MaxLength of any other columns other than varchar (the db is sql server 2005).
0
 
LVL 15

Accepted Solution

by:
oobayly earned 2000 total points
ID: 24128999
Add the following line after you create the DataAdapter, it will tell the DataAdapter to fetch the table schema as well as the data

More information at:
http://msdn.microsoft.com/en-us/library/system.data.missingschemaaction.aspx
DA.MissingSchemAction = MissingSchemaAction.AddWithKey

Open in new window

0
 

Author Comment

by:tfsln
ID: 24133165
This doesnt seem to work when i use a complex query... Its sensitive information so i cant paste it, but are you aware of any limitations in using this property?

I often batch queries together to return multiple datatable's inside the dataset - is this a problem? Or using joins/unions?
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24133312
To be quite honest, this isn't something I've ever used, so I can't say how multiple joins or returning multiple datasets will effect using the property.

However, just doing some simple testing, selecting multiple simple tables doesn't cause any issues and neither does a query that has a single inner join.
My only thought is that you may be returning composite fields, so the schema can't be returned.
0
 

Author Comment

by:tfsln
ID: 24133334
I do include composite fields... but i would have expected those fields to return the same value for MaxLength as any other field which doesnt have a max length (i.e. datetime)

If they do error, then thats a problem... Ill have to abandon this path
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24133405
Interesting, I ran the following code and in all 5 tables returned the columns had the expected MaxLength properties set.
For the composite fields, the MaxLength property set was the Length of the longest string returned, whilst for the standard fields, MaxLength = 64 for a VARCHAR(64) field etc.
      SqlDataAdapter adapter= new SqlDataAdapter("SELECT * FROM tblVehicles;SELECT * FROM tblBodyTypes;"+
        "SELECT * FROM tblVehicles INNER JOIN tblBodyTypes ON tblVehicles.fldBodyID = tblBodyTypes.fldBodyID;" +
        "SELECT 'Foo' + ' ' + 'bar' AS [Foo];" +
        "SELECT fldModelDescription + 'Foo' AS [Foo] FROM tblVehicles", conn);
      adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
      DataSet ds = new DataSet();
      adapter.Fill(ds);

Open in new window

0
 

Author Comment

by:tfsln
ID: 24133414
Interesting... Im going to need to run more tests. It logged in to our application but then failed on one of the most complex queries we have.

Its difficult, because when i test my web service it only allows me to enter text on one line - but i need to paste it a huge query over many lines just so that i can get the error message. At the moment we use the ASPAccelerator to compress web service output and a side effect is that all errors are returned as "Xml was not well formed"
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question