.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

casting to complex type

Posted By:      Posted Date: October 14, 2010    Points: 0   Category :.NET Framework

I'm trying to figure out how to use the combobox item object collection.  Given the code below, how can I put a term in the "Cast<?>" statement to get my anonymous types back?  Or, is there a completely different way to do it?  Can I explicitly cast 'a'?

var citiesbox = new ComboBox();
ComboBox.ObjectCollection objects_to_store = new ComboBox.ObjectCollection(citiesbox);
objects_to_store.Add(new { cityid = 1, cityname = "Eugene" });
objects_to_store.Add(new { cityid = 2, cityname = "Portland" });
objects_to_store.Add(new { cityid = 3, cityname = "Salem" });

var citycode_from_db = 2;

citiesbox.SelectedItem = from a in citiesbox.Items.Cast<?>
             where a.cityid = citycode_from_db
             select a;
-Ethan Nelson

View Complete Post

More Related Resource Links

IEnumerable Type Casting

Hello Everyone,  The following line is giving me an InvalidCastException when trying to convert to IEnumerable type, even though I have implemented IEnumerable interface.  Return New CountryInfo(CountryCodes(i), CountryNames(i)) (also in bold below)Imports System Imports Gaia.WebWidgets.HtmlFormatting Imports System.Collections.Generic Imports System.Globalization Public Class CountryInfo Implements IEnumerable Property Name As String Property Code As String Public Sub New(ByVal code As String, ByVal name As String) _Code = code _Name = name End Sub Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator Return CType(Me, IEnumerator) End Function Public Class SearchResult Private ReadOnly _countries As List(Of CountryInfo) Private ReadOnly _query As String Friend Sub New(ByVal countries As List(Of CountryInfo), ByVal query As String) _countries = countries _query = query End Sub Public ReadOnly Property IsEmpty As Boolean Get Return _countries.Count = 0 End Get End Property Public ReadOnly Property Countries As List(Of CountryInfo) Get Return _countries End Get End Property Public Function Bui

Circular Reference with Complex Type


I have created a webservice that returns a complex type.  This is the xml that is returned from the webservice:

<NinjaRouteEntity xmlns="http://tempuri.org/">
  <Entity NinjaRouteID="1" xmlns="">
    <EntityCollectionReference PropertyName="PointCollectionViaNinjaRouteVector">
        <Entities />

Error Casting to XML Data Type


I have an Ntext data type column, with 2000 XML Documents in it, I am trying to Cast(Col as Xml)

It is casting few of them and then failing.

Error: XML Parsing: Line10, character 6, well formed check: undeclared entry

I want to know if there is any way to write a query to find all Non Formed XML Documents?

Thanks loadsss in advance.

Complex Matrix column data with different data type

I have a data like this

drop table #table
select 34 as A,17948 as B,0.19 as C,19 as D,16145 as E,'Jan' as Period into #table
Insert into #table
select 26 as A,14206 as B,0.18 as C,19 as D,13411 as E,'Feb' as Period
union all
select 38 as A,13037 as B,0.21 as C,18 as D,14974 as E,'Mar' as Period
select * from #table

From this #table i want the view as below format and is doable thru matrix report option in SSRS,but the problem i am running is that i cannot get in this type of data type format. If you look into it,i have column with header 'Jan' and it has data in different data type e.g money,number,percentage. I need to maintain this data type in order to do the calculation after it generates the report on excel spreadsheet. If i use the varchar for the column, it is not usable to me in the final report and i cannot do the calcualtion. Any workaround will be highly appreciated.

    Jan               Feb          Mar
A $34               $26          $26
B 17,

Using Complex Type with Stored Procedure in EF4.0??


Hi All,

Playing with my new toy, VS2010.

  1. Created a .NET 4.0 Web App.
  2. Added Entity Data Model, SPTestModel.edmx, using local AdventureWork SQL Server database, included all tables and one stored procedure.
  3. From the Model Browser>Add Function Import>Choose Stored Proc>Get Column Info>Create New Complex Type>Return a Collection of New Complex Type>OK
  4. Add new WFC Data Service
    1. namespace SPTestDataService
          public class SPTestDataService : DataService<SPTest.AdventureWorksEntities>
              // This method is called only once to initialize service-wide policies.
              public static void InitializeService(DataServiceConfiguration config)
                  config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
                  config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

QUESTION: How do I use the stored procedure?? The complex type mapped to it is ju

DataAnnotation with .edmx complex type


I have a problem.

In edmx Contact entity PersonalInfo complex type is defined with property FullName and Title.

In view the code is

<div class="editor-label">
                <%: Html.LabelFor(model => model.PersonalInfo.FullName) %>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.PersonalInfo.FullName) %>
                <%: Html.ValidationMessageFor(model => model.PersonalInfo.FullName) %>

Got buddy class like
    public partial class Contact

    public class ContactValidation

        public string FullName { get; set; }

When I submit form get an error of mapping but if I take FullName out of complex type than form is working fine.
How I validate FullName if it is in Complex type?


Saving a complex custom type Variable to ModelItem


I am using a custom designer in a hosted workflow designer and want to use a complex custom type as a variable in a workflow.

I have been able to save the Variable, complete with values set in the designer Popup, in the ModelItem (to be able to save the workflow to a file) using:




SQLXML 4 - How to specify a relationship on repeating element of global complex type


I have tried to create a sample showing the issue.  Basically we have multiple elements that use a global complex type.  This complex type has a repeating element and, therefore, should have another relationship specified for it.  I've tried specifying two relationships on the "Phone" element that reference their respective parents, but that does not work.  Is this scenario supported in SQLXML using SchemaGen?

<?xml version="1.0" encoding="utf-16"?>
<xs:schema version="1.0" xmlns:xs

TreeSelector : playing with generics and type inference

create a generic way to define a selection starting from a tree of objects, the most simpler way as possible.

Let's start with the beginning...
Here is a very simple little interface defining a node of the tree, basically, a value and the references to the child nodes.

UnTyped DataSets and Strongly Type DataSets

We all are use datasets as a means of carrier of data from one layer to another. Most of the time we are using weakly typed datasets. In this article I will explain the differences between weakly typed datasets and strongly type datasets

Gmail style multiple Sign-in type combo...is it possible in asp.net?


I am developing a multi-company application and want my users to give the functionality like the following to switch companies.


Is this possible? then how?

Please help


MVC too complex?


I believe lot of people will disagree with me on this one. But the MVC model sucks. The reason:

1.       Over complicated;

2.       Cannot handle complex UI;

3.       Hard to debugging as code scattered all over the place.

Type or namespace error.


A couple of days ago I started getting an unusual behavior when compiling Website projects. I have referenced class libraries added to my bin folder that are also correctely referenced in using statements in my aspx.cs code behind. When I compile, I get the 

Error 4 The type or namespace name 'CommonClassLibrary' could not be found (are you missing a using directive or an assembly reference?) C:\Dev\Fiscal_Portal\Implementation\FMS Portal Site_new\App_Code\BLL\DataTables.cs 1 7 C:\...\FMS Portal Site_new\

The type or namespace name 'CommonClassLibrary' could not be found (are you missing a using directive or an assembly reference?)

error message that would typically be received when the dll has not been added as a reference or added in a using statement.

I can eliminate the error by adding an


Bug: Does not change column type from nvarchar to ntext


Scenario: If you create a table with a column of type nvarchar, populate it with some content and try to modify it using the WebMatrix editor, from nvarchar to ntext, it does not display any error message or warning that this is not possible. It silently saves and reverts the change, back to nvarchar. Wasted some hours on this issue as I thought my column was ntext, but in fact was still nvarchar.

Expected behavior: It should change to ntext, or if SQL CE doesn't support that, it should notify the user that no change was applied.

The number of members in the conceptual type ... does not match with the number of members on the ob



I've added a scalar property to an entity and now ASP.NET Dynamic Data scaffolding throws this error:

The number of members in the conceptual type 'TrainingModel.Users' does not match with the number of members on the object side type 'TrainingModel.Users'. Make sure the number of members are the same.

This happens at the following line in global.asax:
model.RegisterContext(typeof(TrainingModel.TrainingEntities), new ContextConfiguration() { ScaffoldAllTables = true });

Please help!

System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPerm


Good Day all,

Having an issue with an outside user accessing my IIS7 box. I do not have this problem when running the website from my host machine. I found this post: Http://forums.asp.net/t/1371394.aspx. I assure you that this is not a solution because I am not storing any of my files on a network share. 

What do you think my approach should be. 

I already have read rights to IIS user to my BIN folder. 

Thanks for the help. 

Need Syntax To Make Results of LINQ Union Return Non-Generic Type



I have the below SQL which works just fine:

SELECT     Message, CreateDate, AccountId, AlertTypeId
FROM       dbo.Alerts
SELECT     TOP (100) PERCENT Status, CreateDate, AccountId,
                          (SELECT     10 AS Expr1) AS AlertTypeId
FROM         dbo.StatusUpdates
WHERE AccountId = PassedInParameter

I have been going about trying to convert the above into a LINQ method and am having problems with return types.
As the above SQL illustrates, I am querying two distinct LINQ tables - Alerts and StatusUpdates -
and then attempting to perform a Union on them. In order to do the union in LINQ, I have to have agreeing types. So, I
am using "var" to make the results of "alerts" and "updates" generic before the union.  

public IList GetStatusUpdatesAsAlertsByAccountId<T>(Int32 accountId)
            using (WorkbookDataContext dc = _conn.GetCont

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend