Update et Insert grâce à un detailsview
Bonjour à tous,
Alors voilà mon problème : J'ai une application web en ASP.NET et du code en C#. Dans une de mes pages, j'ai un objet gridview dans lequel j'affiche les enregistrements d'une table grâce à une simple requete SELECT. A ce gridview j'ai activé la fonction de selection et, en suivant les instructions du site MSDN de microsoft, j'ai relié mon gridview à un objet detailsview de façon à ce que lorsqu'on sélectionne une ligne dans le gridview, tous les détails de cet enregistrement s'affichent dynamiquement dans le detailsview. Jusque là aucun problèmes, tout foncitonne à merveille.
Le problème réside dans le fait que j'aimerai utiliser ce detailsview pour faire des mise à jour, des suppressions et des insertions dans ma base. La suppresssion foncitonne parfaitement. En revanche l'insertion et la mise à jour plantent. Lorsque je clique sur "edit", je peux éditer tous les champs mais lorsque je clique sur "update" pour les mettre à jour dans ma base j'ai ce message d'erreur (de même pour l'insertion) :
Citation:
Cannot insert the value NULL into column 'name_mail', table 'AutoResponder.dbo.Mail_Template'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
J'ai chercher sur plusieurs site internet, dont celui de microsoft, des solutions à ce problème mais je n'ai rien trouvé que je n'ai pas déjà fait. Il semblerait que dans mon updatecommand il ne récupère pas les champs qui conviennent dans le détailsview. Voici donc mon updatecommand :
Code:
UpdateCommand="UPDATE Mail_Template SET [name_mail] = @name_mail WHERE [id_mail] = @id_mail"
Pour le moment je ne fais que la mise à jour du nom du mail mais même si je renseigne tous les champs dans ma requête ça ne fonctionne pas non plus...
Voici la requête d'insertion (on sait jamais) :
Code:
InsertCommand="INSERT INTO Mail_template (name_mail, path_mail_templateHTML, path_mail_templateTXT, subject_mail, priority, reply_address, from_address, reply_name, from_name, path_mail_attachment) VALUES (@name_mail, @path_mail_templateHTML, @path_mail_templateTXT, @subject_mail, @priority, @reply_address, @from_address, @reply_name, @from_name, @path_mail_attachment)"
Et voici le code source de mon objet gridview, detailsview et des deux SQLDateSource :
Code:
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
|
<table>
<tr>
<td valign="top">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id_mail" DataSourceID="Mails" PageSize="40">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="id_mail" HeaderText="id_mail" InsertVisible="False" ReadOnly="True" SortExpression="id_mail" />
<asp:BoundField DataField="name_mail" HeaderText="name_mail" SortExpression="name_mail" />
<asp:BoundField DataField="subject_mail" HeaderText="subject_mail" SortExpression="subject_mail" />
</Columns>
<HeaderStyle BackColor="SteelBlue" ForeColor="White" />
<SelectedRowStyle BackColor="#E0E0E0" />
</asp:GridView>
<asp:SqlDataSource ID="Mails" runat="server" ConnectionString="<%$ ConnectionStrings:AutoResponderConnectionString %>"
SelectCommand="SELECT [id_mail], [name_mail], [subject_mail] FROM [Mail_Template] ORDER BY [id_mail]">
</asp:SqlDataSource>
</td>
<td valign="top" style="padding-left:5px; width: 392px;">
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="id_mail" DataSourceID="mails_details" Height="50px" Width="227px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" OnItemUpdated="DetailsView1_ItemUpdated" OnItemDeleted="DetailsView1_ItemDeleted" OnItemUpdating="DetailsView1_ItemUpdating">
<Fields>
<asp:BoundField DataField="id_mail" HeaderText="id_mail" InsertVisible="False" ReadOnly="True" SortExpression="id_mail" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="name_mail" HeaderText="name_mail" SortExpression="name_mail" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="path_mail_templateHTML" HeaderText="path_mail_templateHTML" SortExpression="path_mail_templateHTML" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="path_mail_templateTXT" HeaderText="path_mail_templateTXT" SortExpression="path_mail_templateTXT" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="subject_mail" HeaderText="subject_mail" SortExpression="subject_mail" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:CheckBoxField DataField="priority" HeaderText="priority" SortExpression="priority">
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:CheckBoxField>
<asp:BoundField DataField="reply_address" HeaderText="reply_address" SortExpression="reply_address" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="from_address" HeaderText="from_address" SortExpression="from_address" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="reply_name" HeaderText="reply_name" SortExpression="reply_name" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="from_name" HeaderText="from_name" SortExpression="from_name" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="path_mail_attachment" HeaderText="path_mail_attachment" SortExpression="path_mail_attachment" >
<HeaderStyle BackColor="SlateGray" ForeColor="White" Font-Bold="True" />
</asp:BoundField>
</Fields>
<HeaderStyle BackColor="LightBlue" ForeColor="White" />
</asp:DetailsView>
<asp:SqlDataSource ID="mails_details" runat="server" ConnectionString="<%$ ConnectionStrings:AutoResponderConnectionString %>"
SelectCommand="SELECT * FROM Mail_template WHERE id_mail=@id_mail" UpdateCommand="UPDATE Mail_Template SET [name_mail] = @name_mail WHERE [id_mail] = @id_mail" DeleteCommand="DELETE FROM Composed_by WHERE id_mail=@id_mail; DELETE FROM Stats WHERE id_mail=@id_mail; DELETE FROM Mail_Template WHERE id_mail=@id_mail; " InsertCommand="INSERT INTO Mail_template (name_mail, path_mail_templateHTML, path_mail_templateTXT, subject_mail, priority, reply_address, from_address, reply_name, from_name, path_mail_attachment) VALUES (@name_mail, @path_mail_templateHTML, @path_mail_templateTXT, @subject_mail, @priority, @reply_address, @from_address, @reply_name, @from_name, @path_mail_attachment)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="id_mail" PropertyName="SelectedValue" Type="String" DefaultValue="1" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="name_mail" Type="String" />
<asp:Parameter Name="id_mail" Type="String"/>
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="id_mail" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="name_mail" />
<asp:Parameter Name="path_mail_templateHTML" />
<asp:Parameter Name="path_mail_templateTXT" />
<asp:Parameter Name="subject_mail" />
<asp:Parameter Name="priority" />
<asp:Parameter Name="reply_address" />
<asp:Parameter Name="from_address" />
<asp:Parameter Name="reply_name" />
<asp:Parameter Name="from_name" />
<asp:Parameter Name="path_mail_attachment" />
</InsertParameters>
</asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Height="132px" Text="Label" Width="337px"></asp:Label></td>
</tr>
</table> |
Si quelqu'un a une idée je suis preneur, j'avoue sécher complètement...