|
-
Feb 14th, 2011, 05:30 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] specified Method is not Supported
Hi All,
I have the following Code:
Code:
SqlDataReader locationBuildings = command.ExecuteReader();
LocalLocationBuildings = new List<LocationBuilding>();
while (locationBuildings.Read())
{
LocationBuilding locationBuilding = new LocationBuilding();
locationBuilding.PolicyLocationID = (Int32)locationBuildings.GetSqlInt32(0);
locationBuilding.LocationCode = locationBuildings.GetString(1);
locationBuilding.LocationName = locationBuildings.GetString(2);
locationBuilding.LocationPostCode = locationBuildings.GetString(3);
if (!Convert.IsDBNull(locationBuildings.GetValue(4))) { locationBuilding.PolicyLocationBuildingID = (Int32)locationBuildings.GetValue(4); }
if (!Convert.IsDBNull(locationBuildings.GetValue(5))) { locationBuilding.BuildingCode = (Char)locationBuildings.GetValue(5); }
if (!Convert.IsDBNull(locationBuildings.GetValue(6))) { locationBuilding.BuildingName = locationBuildings.GetValue(6).ToString(); }
if (!Convert.IsDBNull(locationBuildings.GetValue(7))) { locationBuilding.BuildingPostcode = locationBuildings.GetValue(7).ToString(); }
locationBuilding.MaintainBuildings = locationBuildings.GetChar(8);
LocalLocationBuildings.Add(locationBuilding);
}
locationBuildings.Close();
close();
}
My code fails with the above error on the highlighted line. The locationBuilding.MaintainBuildings has been declared as Char and the underlying table column for the value returned is declared as Char(1) not null.
there is no conversion in the sp returning the value it's a straight forward select from statement. Anybody any Ideas?
Thanks
-
Feb 14th, 2011, 07:07 AM
#2
Fanatic Member
Re: specified Method is not Supported
As far as I know GetChar 'is' a method of sqlDataReader so shouldn't be a problem.
If the problem was that the row or column didn't exist then I'd expect a different error. The field contains real data ?
Put it inside a try{} catch{} and see if the exception throws any more light on it.
-
Feb 14th, 2011, 07:23 AM
#3
Re: specified Method is not Supported
As I always say: read the documentation.
http://msdn.microsoft.com/en-us/libr...=VS.90%29.aspx
Remarks
Not supported for System.Data.SqlClient.
GetChar gets a Char value, which is a single character. There is not SQL Server data type that represents a single character so it doesn't make sense to support this method. The 'char' data type in SQL Server supports multiple characters, so you must use GetString to get data from such a column. If you have configured the column as 'char(1)' then that is a String containing a single character, which is different to a Char.
-
Feb 18th, 2011, 05:55 AM
#4
Thread Starter
Fanatic Member
Re: specified Method is not Supported
Strange, but this seemed to resolve the problem:
Code:
locationBuilding.MaintainBuildings = Convert.ToChar(locationBuildings.GetValue(8));
-
Feb 18th, 2011, 06:00 AM
#5
Re: [RESOLVED] specified Method is not Supported
It's not strange at all. GetChar doesn;t work because the value you're getting has to be a Char in the first place. The value isn't a Char, it's a String, so GetChar can't possibly work. What you're doing now is getting the value, which is a String, as an Object reference and passing that to Convert.ToChar. When you pass a String to ToChar you get the first character from the String back as a Char. What you're doing is equivalent to this:
Code:
myString = myDataReader.GetString(columnIndex)
myChar = myString(0)
-
Feb 18th, 2011, 07:03 AM
#6
Thread Starter
Fanatic Member
Re: [RESOLVED] specified Method is not Supported
Hmm... I can understand your reasoning, but The SP is essentially doing
select col1, col2 from table.
The Underlying column on the table is declared as Char.
-
Feb 18th, 2011, 08:30 AM
#7
Re: [RESOLVED] specified Method is not Supported
It's like post #3 never happened. The 'char' data type in SQL Server and the Char data type in VB.NET are not the same thing. A .NET Char is a type that contains a single character. A SQL Server 'char' is a type that contains text of a fixed number of characters. There is no data type in SQL Server that represents a single character, hence SqlClient doesn't support the GetChar method. In SQL Server, the 'text', 'ntext', 'char', 'nchar', 'varchar' and 'nvarchar' data types all map to the .NET String type because they are all capable of storing multi-character text. The fact that you declare your column as 'char(1)' doesn't make it map to Char. It still maps to String but the String you get will always contain one character. In .NET, a String containing one character and a Char are not the same thing, just like an array containing one Integer and an Integer are not the same thing. For GetChar to be supported by SqlClient, SQL Server would have to have a data type that represented a single character. It doesn't so it's not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|