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

ASP.NET Discussion :

Récupération ID après INSERT retourne NULL


Sujet :

ASP.NET

  1. #1
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    249
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2007
    Messages : 249
    Par défaut Récupération ID après INSERT retourne NULL
    Salut à toutes et à tous,

    Je cherche depuis un bon moment ce qui ne va pas dans mon code, mais je ne trouve pas, même après avoir lu plusieurs 10aines de sites/posts parlant du sujet, aussi je fais appel à vous.

    J'ai une base de donnée PostgreSQL dans laquelle j'ai créé une fonction qui insère une ligne dans 2 tables et me retourne l'ID d'un des éléments créés.
    Losque j'exécute la fonction sous postgreSQL, elle me retourne bien le nouvel ID et les éléments sont bien créés.
    Tout va donc bien du côté de PostgreSQL.

    Dans ma page ASP j'ai un SQLDataSource avec un paramètre ReturnValue pour récupérer le fameux ID retourné par la fonction.
    Sur l'event Inserted j'ai un code qui devrait alimenter un champs texte avec le nouvel ID, or e.Command.Parameters["New_ID"].Value me retourne une valeur nulle

    Ce qui me chipotte un peu, c'est qu'en PostgreSQL, pour appeler une fonction il faut utiliser un select, et je me demande si le pbl ne viendrait pas de là. Directement dans PostgreSQL j'appelle "SELECT reno_modifsl3_insert_modif_et_lot_lie("TexteAIntroduire", IntegerAIntroduire, NumericAIntroduire)" et il me retourne l'ID.

    J'ai donc écris ceci, mais le retour de l'ID a l'air null:
    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
     
    <asp:SqlDataSource ID="SqlDataSource_Reno_ModifsL3_ModifsParNouvLot"
                                        runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString_ODBC_Topocom %>"
                                        InsertCommandType ="StoredProcedure" 
                                        InsertCommand="SELECT reno_modifsl3_insert_modif_et_lot_lie(?, ?, ?)"
                                       ProviderName="<%$ ConnectionStrings:ConnectionString_ODBC_Topocom.ProviderName %>"
                                        SelectCommand='SELECT topocom."public".reno_modifs_apres_l3.modifl3_id, topocom."public".reno_modifs_apres_l3.modification FROM topocom."public".reno_modifs_apres_l3, topocom."public".reno_nouv_lots_modifs_apres_l3 WHERE topocom."public".reno_modifs_apres_l3.modifl3_id = topocom."public".reno_nouv_lots_modifs_apres_l3.modifl3_id AND (topocom."public".reno_nouv_lots_modifs_apres_l3.num_nouv_lot = ?) ORDER BY topocom."public".reno_modifs_apres_l3.modifl3_id' OnInserted="SqlDataSource_Reno_ModifsL3_ModifsParNouvLot_Inserted">
                                         <SelectParameters>
                                            <asp:ControlParameter ControlID="ListBox_Reno_ModifsL3_Nouv_Lots" Name="?" PropertyName="SelectedValue" />
                                        </SelectParameters>
                                        <InsertParameters>
                                            <asp:ControlParameter ControlID="TextBox_Reno_ModifL3_Description" Name="?" PropertyName="Text" />
                                            <asp:ControlParameter ControlID="TextBox_UserID" Name="?" PropertyName="Text" />
                                            <asp:ControlParameter ControlID="ListBox_Reno_ModifsL3_Nouv_Lots" Name="?" PropertyName="SelectedValue" />
                                            <asp:Parameter Direction="ReturnValue" Name="id_modif" Type="Int32" />
                                        </InsertParameters>
                                    </asp:SqlDataSource>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
        protected void SqlDataSource_Reno_ModifsL3_ModifsParNouvLot_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            TextBox_Reno_ModifsL3_modif_id.Text = Server.HtmlEncode(e.Command.Parameters["New_ID"].Value.ToString());
        }
    Quelqu'un a-t-il une idée de ce que je dois faire pour récupérer ce de ID ?

    Merci de vos z'avis z'avisés,
    Jean-Marc

  2. #2
    Membre Expert
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Réunion

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 277
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <asp:Parameter Direction="ReturnValue" Name="id_modif" Type="Int32" />
    donc,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    e.Command.Parameters["id_modif"]
    au lieu de e.Command.Parameters["New_ID"] ?

  3. #3
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    249
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2007
    Messages : 249
    Par défaut
    Cela est dû au fait qu'il ne me retournait rien. J'avais essayé en utilisant le même nom que dans la fonction (Je sais, c'est un peu ridicule, mais quand on ne trouve pas on essaye tout ...).

    J'ai effectivement changé cette info mais le résultat donné par l'espion sur e.Command.Parameters["id_modif"].Value est toujours {}.

    Une idée du pourquoi ?

    Merci de ton aide Kaidan,
    Jean-Marc

  4. #4
    Membre Expert
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Réunion

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 277
    Par défaut
    Il faudrait peut-être regarder du côté du code de la procédure stockée.

  5. #5
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    249
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2007
    Messages : 249
    Par défaut La procédure stockée
    La voici :

    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
     
    CREATE FUNCTION reno_modifsl3_insert_modif_et_lot_lie(IN desciption_modif text, IN id_employe integer, IN num_lot numeric) RETURNS integer AS
    $BODY$
     
    DECLARE 
    	id_modif integer;
     
    BEGIN
     
    	id_modif = NEXTVAL('reno_modifs_apres_l3_modifsl3_id_seq');
     
    	INSERT INTO reno_modifs_apres_l3(modifl3_id, modification, employe_id)
    		VALUES (id_modif, desciption_modif, id_employe);
     
    	INSERT INTO reno_nouv_lots_modifs_apres_l3(modifl3_id, num_nouv_lot)
    		VALUES (id_modif, num_lot);
     
    	RETURN id_modif;
     
    END;
     
    $BODY$ 
    LANGUAGE 'plpgsql' VOLATILE;
    ALTER FUNCTION reno_modifsl3_insert_modif_et_lot_lie(IN text, IN integer, IN numeric) OWNER TO jm;
    Et lorsque je la fait tourner à partir de PostgreSQL elle me retourne bien le nouvel id.
    Je pense donc qu'elle fonctionne correctement.

  6. #6
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    249
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2007
    Messages : 249
    Par défaut Le problème ne semble pas venir de PostgreSQL
    J'ai modifié mon SQLDataSource et j'ai refait l'insert en texte dans une seule table pour voir si c'est au niveau de la procédure stockée ou de son appel que ça coince.
    L'ajout se fait bien au serveur, mais
    e.Command.Parameters["New_ID"].Value me retourne toujours {}, or ici je n'appelle même plus une procédure stockée.
    C'est normal qu'il ne me retourne pas l'ID puisque c'est un simple insert et non une procédure stockée qui retourne l'id, mais il me semble qu'il devrait retourner 1 ou -1 selon que l'insert à réussi ou pas.

    Voici ce qui me retourne ce vide :
    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
    <asp:SqlDataSourceID="SqlDataSource_Reno_ModifsL3_ModifsParNouvLot"
    runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString_ODBC_Topocom %>"
    SelectCommand='SELECT topocom."public".reno_modifs_apres_l3.modifl3_id, topocom."public".reno_modifs_apres_l3.modification FROM topocom."public".reno_modifs_apres_l3, topocom."public".reno_nouv_lots_modifs_apres_l3 WHERE topocom."public".reno_modifs_apres_l3.modifl3_id = topocom."public".reno_nouv_lots_modifs_apres_l3.modifl3_id AND (topocom."public".reno_nouv_lots_modifs_apres_l3.num_nouv_lot = ?) ORDER BY topocom."public".reno_modifs_apres_l3.modifl3_id'
    InsertCommand='INSERT INTO "public".reno_modifs_apres_l3(modification, employe_id) VALUES (?, ?)'
    UpdateCommand='UPDATE "public".reno_modifs_apres_l3 SET modification = ? WHERE (modifl3_id = ?)'
    ProviderName="System.Data.Odbc"
    OnInserted="SqlDataSource_Reno_ModifsL3_ModifsParNouvLot_Inserted">
    <SelectParameters>
    <asp:ControlParameterControlID="ListBox_Reno_ModifsL3_Nouv_Lots"Name="?"PropertyName="SelectedValue"/>
    </SelectParameters>
    <InsertParameters>
    <asp:ControlParameterControlID="TextBox_Reno_ModifL3_Description"Name="?"PropertyName="Text"/>
    <asp:ControlParameterControlID="TextBox_UserID"Name="?"PropertyName="Text"/>
    <asp:ParameterDirection="ReturnValue"Name="New_ID"Type="Int32"/>
    </InsertParameters>
    <UpdateParameters>
    <asp:ControlParameterControlID="TextBox_Reno_ModifL3_Description"Name="?"PropertyName="Text"/>
    <asp:ControlParameterControlID="TextBox_Reno_ModifsL3_modif_id"Name="?"PropertyName="Text"/>
    </UpdateParameters>
    </asp:SqlDataSource>
    
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    protectedvoid SqlDataSource_Reno_ModifsL3_ModifsParNouvLot_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
    TextBox_Reno_ModifsL3_modif_id.Text = Server.HtmlEncode(e.Command.Parameters["New_ID"].Value.ToString());
    }
    Je n'y comprend rien.
    Qu'est ce qui ne va pas ?

    Jean-Marc

  7. #7
    Rédacteur
    Avatar de SaumonAgile
    Homme Profil pro
    Team leader
    Inscrit en
    Avril 2007
    Messages
    4 028
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Team leader
    Secteur : Conseil

    Informations forums :
    Inscription : Avril 2007
    Messages : 4 028
    Par défaut
    Reprend ta procédure stockée, déclare un paramètre OUT et affecte lui la valeur à retourner, déclare aussi le paramètre out dans ton code, et tu devrais pouvoir récupérer cet ID.
    Besoin d'un MessageBox amélioré ? InformationBox pour .NET 1.1, 2.0, 3.0, 3.5, 4.0 sous license Apache 2.0.

    Bonnes pratiques pour les accès aux données
    Débogage efficace en .NET
    LINQ to Objects : l'envers du décor

    Mon profil LinkedIn - MCT - MCPD WinForms - MCTS Applications Distribuées - MCTS WCF - MCTS WCF 4.0 - MCTS SQL Server 2008, Database Development - Mon blog - Twitter

  8. #8
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    249
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2007
    Messages : 249
    Par défaut Toujours rien ...
    Suivant tes conseils, j'ai modifié mon code comme ceci :

    Procédure stockée (elle tourne et me retourne toujours le bon id):
    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
    CREATE FUNCTION reno_modifsl3_insert_modif_et_lot_lie(IN desciption_modif text, IN id_employe integer, IN num_lot numeric, OUT new_id integer) RETURNS integer AS
    $BODY$
    DECLARE 
     id integer;
    BEGIN
     id = NEXTVAL('reno_modifs_apres_l3_modifsl3_id_seq');
     
     INSERT INTO reno_modifs_apres_l3(modifl3_id, modification, employe_id)
      VALUES (id, desciption_modif, id_employe);
     INSERT INTO reno_nouv_lots_modifs_apres_l3(modifl3_id, num_nouv_lot)
      VALUES (id, num_lot);
     new_id = id;
    END;
    $BODY$ 
    LANGUAGE 'plpgsql' VOLATILE;
    ALTER FUNCTION reno_modifsl3_insert_modif_et_lot_lie(IN text, IN integer, IN numeric) OWNER TO jm;
    La SQLDataSource :
    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
    <asp:SqlDataSourceID="SqlDataSource_Reno_ModifsL3_ModifsParNouvLot"
    runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString_ODBC_Topocom %>"
    SelectCommand='SELECT topocom."public".reno_modifs_apres_l3.modifl3_id, topocom."public".reno_modifs_apres_l3.modification FROM topocom."public".reno_modifs_apres_l3, topocom."public".reno_nouv_lots_modifs_apres_l3 WHERE topocom."public".reno_modifs_apres_l3.modifl3_id = topocom."public".reno_nouv_lots_modifs_apres_l3.modifl3_id AND (topocom."public".reno_nouv_lots_modifs_apres_l3.num_nouv_lot = ?) ORDER BY topocom."public".reno_modifs_apres_l3.modifl3_id'
    InsertCommand='SELECT new_id FROM reno_modifsl3_insert_modif_et_lot_lie(?,?,?)'
    UpdateCommand='UPDATE "public".reno_modifs_apres_l3 SET modification = ? WHERE (modifl3_id = ?)'
    ProviderName="System.Data.Odbc"
    OnInserted="SqlDataSource_Reno_ModifsL3_ModifsParNouvLot_Inserted"InsertCommandType="StoredProcedure">
    <SelectParameters>
    <asp:ControlParameterControlID="ListBox_Reno_ModifsL3_Nouv_Lots"Name="?"PropertyName="SelectedValue"/>
    </SelectParameters>
    <InsertParameters>
    <asp:ControlParameterControlID="TextBox_Reno_ModifL3_Description"Name="?"PropertyName="Text"/>
    <asp:ControlParameterControlID="TextBox_UserID"Name="?"PropertyName="Text"/>
    <asp:ControlParameterControlID="ListBox_Reno_ModifsL3_Nouv_Lots"Name="?"PropertyName="SelectedValue"/>
    <asp:ParameterDirection="Output"Name="new_id"Type="Int32"/>
    </InsertParameters>
    <UpdateParameters>
    <asp:ControlParameterControlID="TextBox_Reno_ModifL3_Description"Name="?"PropertyName="Text"/>
    <asp:ControlParameterControlID="TextBox_Reno_ModifsL3_modif_id"Name="?"PropertyName="Text"/>
    </UpdateParameters>
    </asp:SqlDataSource>
    Le code sur l'event inserted :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    protectedvoid SqlDataSource_Reno_ModifsL3_ModifsParNouvLot_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
    TextBox_Reno_ModifsL3_modif_id.Text = Server.HtmlEncode(e.Command.Parameters["new_id"].Value.ToString());
    }
    Le Server.HtmlEncode(e.Command.Parameters["new_id"].Value.ToString()) me retourne toujours "" puisque le e.Command.Parameters["new_id"].Value retourne toujours {}.

    J'ai aussi essayé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    InsertCommand='SELECT * FROM reno_modifsl3_insert_modif_et_lot_lie(?,?,?)'
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    InsertCommand='SELECT reno_modifsl3_insert_modif_et_lot_lie(?,?,?)'
    et même
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    InsertCommand='SELECT reno_modifsl3_insert_modif_et_lot_lie FROM reno_modifsl3_insert_modif_et_lot_lie(?,?,?)'
    
    Mais ça ne change rien.

    Pourquoi tant de haîne de ce PC envers moi ???

    Merci de votre aide,
    Jean-Marc

Discussions similaires

  1. [MySQL] Récupération de donnée après INSERT
    Par benjamin002 dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 24/11/2010, 14h21
  2. Réponses: 3
    Dernier message: 29/07/2009, 11h18
  3. Réponses: 3
    Dernier message: 19/04/2007, 21h55
  4. fclose apres un fopen null?
    Par poca10 dans le forum MFC
    Réponses: 4
    Dernier message: 04/02/2004, 11h39
  5. récupérer valeur d'un ID après insertion
    Par rikidi dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 26/08/2003, 22h21

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