Bonjour;
Je cherche à ajouter une fonctionnalité attacher pièce jointe à mon formulaire asp.net sans succès.
J'ai le message d'erreur suivant:
Le code frContact.aspx
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 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 55: Line 56: String StrFileName = ""; Line 57: StrFileName = Request.Files["fileAttachment"].FileName; Line 58: if (StrFileName != "") Line 59: { Source File: c:\Documents and Settings\TBSoltane\Mes documents\Visual Studio 2008\WebSites\Prestige\Fra\frContact.aspx.cs Line: 57
Le code de frContact.aspx.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 <%@ Page Language="C#" MasterPageFile="~/Fra/frMasterPage.master" AutoEventWireup="true" CodeFile="frContact.aspx.cs" Inherits="frContact" Title="Prestige Concepts Inc: Contact" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div id="content-container-three-column"> <!-- CONTENT SIDE 1 COLUMN --> <div id="content-side1-three-column"> <ul class="list-of-links"> <li class="current"><a href="frContact.aspx">Contact</a></li> <li><a href="frDefault.aspx">Accueil</a></li> <li><a href="frProducts.aspx">Produits</a></li> <li><a href="frPartners.aspx">Partenaires</a></li> <li><a href="frReferences.aspx">Réferences</a></li> <li><a href="frAbout.aspx">A propos</a></li> <li><a href="frTestimonials.aspx">Témoignages</a></li> </ul> </div> <!-- CENTER COLUMN --> <div id="content-main-three-column"> <h2><span id="heading">Contactez-nous</span></h2> <hr /> <h3> Siège social </h3> <p> Prestige concepts<br /> LL1/9-Kortoba Riadh El Andalous<br /> 2058 Ariana Tunis Tunisie </p> <p> tel: (+216)71 82 07 47 / (+216)71 82 07 46 <br /> fax: (+216)71 82 07 48 </p> <a href="mailto:info@prestigeconcepts.com">info@prestigeconcepts.com</a><br /> <p> </p> <br /> <asp:Panel ID="Panel1" runat="server" Height="526px" BorderStyle="Dotted" BorderColor="gray" Width="350px" > <table> <caption> <br /> <tr> <td style="width: 100px"> Entreprise</td> <td style="width: 100px"> <asp:TextBox ID="txtCompany" runat="server" Width="175px"></asp:TextBox> </td> <td style="width: 100px"> <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtCompany" ErrorMessage="Prière de communiquer le nom de votre entreprise">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td style="width: 100px"> Nom</td> <td style="width: 100px"> <asp:TextBox ID="txtName" runat="server" Width="175px"></asp:TextBox> </td> <td style="width: 100px"> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="Prière de rentrer votre nom">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td style="width: 100px"> Email</td> <td style="width: 100px"> <asp:TextBox ID="txtEmail" runat="server" Width="175px"></asp:TextBox> </td> <td style="width: 100px"> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail" ErrorMessage="Prière de fournir votre adrese e-mail">*</asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" ErrorMessage="Prière de fournir une adresse e-mail valide (xxx@yyy.zzz)" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator> </td> </tr> <tr> <td style="width: 100px"> Tél. </td> <td style="width: 100px"> <asp:TextBox ID="txtPhone" runat="server" Width="175px"></asp:TextBox> </td> <td style="width: 100px"> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtPhone" ErrorMessage="Prière de fournir votre numéro de téléphone">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td style="width: 100px; height: 116px;"> Message<span style="font-size: 10pt">*</span> <br /> </td> <td style="width: 100px; height: 116px;"> <asp:TextBox ID="txtMessage" runat="server" Height="106px" TextMode="MultiLine"></asp:TextBox> </td> <td style="width: 100px; height: 116px;"> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtMessage" ErrorMessage="Prière de saisir un message">*</asp:RequiredFieldValidator> <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtMessage" ErrorMessage="Pas plus de 1000 caractères, ni moins de 20" OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator> </td> </tr> <tr> <td> Attachment </td> <td> <input type="file" runat="server" id="fileAttachment" name="fileAttachment" /> </td> </tr> </caption> </table> <hr /> <asp:Button ID="btnValidate" runat="server" Text="Valider" Width="61px" style="left: 156px; position: relative; top: 10px" OnClick="btnValidate_Click" /> <br /> <br /> <br /> <span style="font-size: 7pt">Tous les champs sont obligatoires <br /> * = Maximum 1000 caractères, minimum 20.<br /> <br /> <asp:Label ID="lblError" runat="server"></asp:Label> <br /> <br /> <br /> <asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="56px" Width="342px" /> </span></asp:Panel> </div> <!-- END MAIN COLUMN --> <!-- CONTENT SIDE 2 COLUMN --> <div id="content-side2-three-column"> <span id="subheading">Prestige Concepts à votre écoute.</span> <p> <br /> <br /> Si vous avez une quetion, n'hésitez pas à nous contacter. Une équipe réactive et dynamique vous répondra rapidement. </div> <div class="clear"> </div> </div> </asp:Content>
Je pense que je doit créer un objet et l'instancier pour résoudre le problème. Mais, j'ai beau chercher, je ne sais pas comment faire.
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 using System; using System.Web.Mail; using System.IO; public partial class frContact : System.Web.UI.Page { protected System.Web.UI.HtmlControls.HtmlInputFile file; protected void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args) { if ((txtMessage.Text.Length < 20) || (txtMessage.Text.Length > 1000)) { args.IsValid = false; } else { args.IsValid = true; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { txtName.Text = ""; txtEmail.Text = ""; txtCompany.Text = ""; txtPhone.Text = ""; txtMessage.Text = ""; } } protected void btnValidate_Click(object sender, EventArgs e) { MailMessage email = new MailMessage(); email.From = "site@prestigeconcepts.fr"; email.To = "info@prestigeconcepts.fr"; email.Body = "Name :" + txtName + "\r\n" + "Email :" + txtEmail + "\r\n" + "Company: " + txtCompany.Text + "\r\n" + "Phone : " + txtPhone.Text + "\r\n" + "Message : " + txtMessage.Text; String StrFileName = ""; StrFileName = Request.Files["fileAttachment"].FileName; if (StrFileName != "") { //extraction fichier avec chemin StrFileName = StrFileName.Substring(StrFileName.LastIndexOf("\\") + 1); //enregistrement en temp Request.Files["fileAttachment"].SaveAs(Server.MapPath(StrFileName)); //attacher le fichier MailAttachment MyAttachment = new MailAttachment(Server.MapPath(StrFileName)); email.Attachments.Add(MyAttachment); } email.Priority = MailPriority.High; SmtpMail.SmtpServer = "smtp.planet.tn"; try { SmtpMail.Send(email); } catch (Exception ex) { lblError.Text = ex.Message; } finally { lblError.Text = "Message envoyé avec succès"; } if (StrFileName != "") File.Delete(Server.MapPath(StrFileName)); } }
Merci de votre aide.
Partager