IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Linq Discussion :

"StrongTypingException was unhandled by user code"


Sujet :

Linq

  1. #1
    Membre habitué
    Inscrit en
    Novembre 2004
    Messages
    415
    Détails du profil
    Informations forums :
    Inscription : Novembre 2004
    Messages : 415
    Points : 138
    Points
    138
    Par défaut "StrongTypingException was unhandled by user code"
    Bonjour,
    Ma requête linq me génère le type d'exception indiqué en titre.
    Je vous ai mis le contenu complet du dataset dont je me sers pour illustrer mon problème, afin que vous ayez tous les éléments pour essayer de regénérer éventuellement mon problème ou voir sinon directement ce qui ne va pas.
    Le code complet, avec les données de tests (équivalentes à ce que j'ai en base) qui font que ça plante, est également fourni ci-dessous.

    L'exception arrive à l'intérieur de mon foreach, au moment où il tente de gérer le contenu de ma 1ère requête(queryWithxxxNotNull). Dans mon callStack, l'eception est levée au niveau du b.xxx de
    equals new {
    b.xxx,
    b.zzz
    }

    Le detail de mon erreur est ceci:
    The value for column 'xxx' in table 'TestDetail' is DBNull.
    Quand je regarde en mode débug le contenu de "Results View" de queryWithxxxNotNull, j'ai déjà ce message en guise de Value.

    De plus, xxx est un champs int qui dans ma base peut être nul mais qui via le designer ne peut pas être setté à nul.

    ds.xsd:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="ds" targetNamespace="http://tempuri.org/ds.xsd" xmlns:mstns="http://tempuri.org/ds.xsd" xmlns="http://tempuri.org/ds.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
      <xs:annotation>
        <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
          <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
            <Connections />
            <Tables />
            <Sources />
          </DataSource>
        </xs:appinfo>
      </xs:annotation>
      <xs:element name="ds" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_UserDSName="ds" msprop:Generator_DataSetName="ds" msprop:EnableTableAdapterManager="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Test" msprop:Generator_UserTableName="Test" msprop:Generator_RowDeletedName="TestRowDeleted" msprop:Generator_RowChangedName="TestRowChanged" msprop:Generator_RowClassName="TestRow" msprop:Generator_RowChangingName="TestRowChanging" msprop:Generator_RowEvArgName="TestRowChangeEvent" msprop:Generator_RowEvHandlerName="TestRowChangeEventHandler" msprop:Generator_TableClassName="TestDataTable" msprop:Generator_TableVarName="tableTest" msprop:Generator_RowDeletingName="TestRowDeleting" msprop:Generator_TablePropName="Test">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="xxx" msprop:Generator_UserColumnName="xxx" msprop:nullValue="_throw" msprop:Generator_ColumnPropNameInRow="xxx" msprop:Generator_ColumnPropNameInTable="xxxColumn" msprop:Generator_ColumnVarNameInTable="columnxxx" type="xs:int" minOccurs="0" />
                  <xs:element name="yyy" msprop:Generator_UserColumnName="yyy" msprop:nullValue="_null" msprop:Generator_ColumnVarNameInTable="columnyyy" msprop:Generator_ColumnPropNameInTable="yyyColumn" msprop:Generator_ColumnPropNameInRow="yyy" type="xs:string" minOccurs="0" />
                  <xs:element name="zzz" msprop:Generator_UserColumnName="zzz" msprop:Generator_ColumnVarNameInTable="columnzzz" msprop:Generator_ColumnPropNameInRow="zzz" msprop:Generator_ColumnPropNameInTable="zzzColumn" type="xs:int" minOccurs="0" />
                  <xs:element name="aaa" msprop:Generator_UserColumnName="aaa" msprop:Generator_ColumnVarNameInTable="columnaaa" msprop:Generator_ColumnPropNameInRow="aaa" msprop:Generator_ColumnPropNameInTable="aaaColumn" type="xs:string" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="TestDetail" msprop:Generator_UserTableName="TestDetail" msprop:Generator_RowDeletedName="TestDetailRowDeleted" msprop:Generator_RowChangedName="TestDetailRowChanged" msprop:Generator_RowClassName="TestDetailRow" msprop:Generator_RowChangingName="TestDetailRowChanging" msprop:Generator_RowEvArgName="TestDetailRowChangeEvent" msprop:Generator_RowEvHandlerName="TestDetailRowChangeEventHandler" msprop:Generator_TableClassName="TestDetailDataTable" msprop:Generator_TableVarName="tableTestDetail" msprop:Generator_RowDeletingName="TestDetailRowDeleting" msprop:Generator_TablePropName="TestDetail">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="xxx" msprop:Generator_UserColumnName="xxx" msprop:nullValue="_throw" msprop:Generator_ColumnPropNameInRow="xxx" msprop:Generator_ColumnPropNameInTable="xxxColumn" msprop:Generator_ColumnVarNameInTable="columnxxx" type="xs:int" minOccurs="0" />
                  <xs:element name="yyy" msprop:Generator_UserColumnName="yyy" msprop:nullValue="_null" msprop:Generator_ColumnVarNameInTable="columnyyy" msprop:Generator_ColumnPropNameInTable="yyyColumn" msprop:Generator_ColumnPropNameInRow="yyy" type="xs:string" minOccurs="0" />
                  <xs:element name="zzz" msprop:Generator_UserColumnName="zzz" msprop:Generator_ColumnVarNameInTable="columnzzz" msprop:Generator_ColumnPropNameInRow="zzz" msprop:Generator_ColumnPropNameInTable="zzzColumn" type="xs:int" minOccurs="0" />
                  <xs:element name="bbb" msprop:Generator_UserColumnName="bbb" msprop:Generator_ColumnVarNameInTable="columnbbb" msprop:Generator_ColumnPropNameInRow="bbb" msprop:Generator_ColumnPropNameInTable="bbbColumn" type="xs:string" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="Final" msprop:Generator_UserTableName="Final" msprop:Generator_RowDeletedName="FinalRowDeleted" msprop:Generator_RowChangedName="FinalRowChanged" msprop:Generator_RowClassName="FinalRow" msprop:Generator_RowChangingName="FinalRowChanging" msprop:Generator_RowEvArgName="FinalRowChangeEvent" msprop:Generator_RowEvHandlerName="FinalRowChangeEventHandler" msprop:Generator_TableClassName="FinalDataTable" msprop:Generator_TableVarName="tableFinal" msprop:Generator_RowDeletingName="FinalRowDeleting" msprop:Generator_TablePropName="Final">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="aaa" msprop:Generator_UserColumnName="aaa" msprop:Generator_ColumnVarNameInTable="columnaaa" msprop:Generator_ColumnPropNameInRow="aaa" msprop:Generator_ColumnPropNameInTable="aaaColumn" type="xs:string" minOccurs="0" />
                  <xs:element name="bbb" msprop:Generator_UserColumnName="bbb" msprop:Generator_ColumnVarNameInTable="columnbbb" msprop:Generator_ColumnPropNameInRow="bbb" msprop:Generator_ColumnPropNameInTable="bbbColumn" type="xs:string" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    Remplissage de mes datatable et requêtage sur celles-ci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
                    DataRow dr;
     
                    ds.TestDataTable dt = new ds.TestDataTable();
                    dr = dt.NewRow();
                    dr["xxx"] = 1;
                    dr["yyy"] = DBNull.Value;
                    dr["zzz"] = 3;
                    dr["aaa"] = "val1";
                    dt.AddTestRow((ds.TestRow)dr);
     
                    dr = dt.NewRow();
                    dr["xxx"] = DBNull.Value;
                    dr["yyy"] = 2;
                    dr["zzz"] = 3;
                    dr["aaa"] = "val11";
                    dt.AddTestRow((ds.TestRow)dr);
     
                    ds.TestDataTable dtWithxxxNotNull = new ds.TestDataTable();
                    ds.TestRow[] tr1 = (ds.TestRow[])dt.Select("xxx is not null");
                    if (tr1.Count<ds.TestRow>() > 0)
                    {
                        DataTable looselyTypedDT1 = tr1.CopyToDataTable<ds.TestRow>();
                        dtWithxxxNotNull.Merge(looselyTypedDT1, true);
                    }
     
                    ds.TestDataTable dtWithxxxNull = new ds.TestDataTable();
                    ds.TestRow[] tr2 = (ds.TestRow[])dt.Select("xxx is null");
                    if (tr2.Count<ds.TestRow>() > 0)
                    {
                        DataTable looselyTypedDT1 = tr2.CopyToDataTable<ds.TestRow>();
                        dtWithxxxNull.Merge(looselyTypedDT1, true);
                    }
     
                    ds.TestDetailDataTable dtdetail = new ds.TestDetailDataTable();
                    dr = dtdetail.NewRow();
                    dr["xxx"] = 1;
                    dr["yyy"] = DBNull.Value;
                    dr["zzz"] = 3;
                    dr["bbb"] = "val101";
                    dtdetail.AddTestDetailRow((ds.TestDetailRow)dr);
                    dr = dtdetail.NewRow();
                    dr["xxx"] = DBNull.Value;
                    dr["yyy"] = 2;
                    dr["zzz"] = 3;
                    dr["bbb"] = "val999";
                    dtdetail.AddTestDetailRow((ds.TestDetailRow)dr);
     
     
                    var queryWithxxxNotNull =
                    from a in dtWithxxxNotNull
                    join b in dtdetail
                    on new
                    {
                        a.xxx,
                        a.zzz
                    }
                        equals new
                        {
                            b.xxx,
                            b.zzz
                        }
                    select new
                    {
                        aaa = a.aaa,
                        bbb = b.bbb
                    };
     
                    var queryWithxxxNull =
                    from a in dtWithxxxNull
                    join b in dtdetail
                    on new
                    {
                        a.yyy,
                        a.zzz
                    }
                        equals new
                        {
                            b.yyy,
                            b.zzz
                        }
                    select new
                    {
                        aaa = a.aaa,
                        bbb = b.bbb
                    };
     
                    var final_query = queryWithxxxNotNull.Union(queryWithxxxNull);
                    ds.FinalDataTable fdt = new ds.FinalDataTable();
     
                    foreach (var item in final_query)
                    {
                        fdt.AddFinalRow(item.aaa, item.bbb);
                    }
    Merci beaucoup si vous pouviez m'indiquer ce que je ne fais pas correctement. J'ai mis toutes les infos a priori afin que vous puissiez reproduire le problème. S'il manque quelque chose, n'hésitez pas à me le dire.
    Julien

  2. #2
    Membre émérite
    Avatar de laedit
    Homme Profil pro
    Consultant études et développement
    Inscrit en
    Décembre 2006
    Messages
    1 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Consultant études et développement
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 344
    Points : 2 265
    Points
    2 265
    Par défaut
    Si je comprends bien, c'est un problème de valeur DBNull avec Linq et des DataSet. D'après ce que je sais, c'est un problème connu et d'autres topic en parle déjà sur ce forum, fais une recherche pour avoir plus d'infos

  3. #3
    Membre habitué
    Inscrit en
    Novembre 2004
    Messages
    415
    Détails du profil
    Informations forums :
    Inscription : Novembre 2004
    Messages : 415
    Points : 138
    Points
    138
    Par défaut
    Bonjour,
    C'est ce que j'ai fait mais aucun ne répondait au problème justement car le problème n'existait soudainement plus pour la personne en question ou alors il s'agissait d'autre chose.
    J'ai pris soin de mettre le code complet nécessaire afin que chacun puisse reproduire l'erreur (un dataset complet et le code complet qui remplit mes datatables et qui les utilisent), ce qu'il manque parfois dans certaines demandes d'aide. L'erreur apparaîtra alors directmeent et on saura de quoi je parle exactement.
    D'avance merci à toute personne qui voudra bien jeter un coup d'oeil.
    Julien

Discussions similaires

  1. Réponses: 3
    Dernier message: 22/08/2011, 14h50
  2. Augmenter la hauteur du div/span de la balise &quot;code&quot;
    Par Djakisback dans le forum Evolutions du club
    Réponses: 10
    Dernier message: 24/01/2009, 14h31
  3. SecurityException was unhandled
    Par matrix_ceg dans le forum VB.NET
    Réponses: 2
    Dernier message: 23/10/2007, 14h45
  4. [VB.NET] Tableau "index was outside of bound array"
    Par guillaume1998 dans le forum VB.NET
    Réponses: 1
    Dernier message: 11/11/2005, 14h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo