Je voudrais faire un enregistrement dans la table vol(nomvol, villedep, datedep, villedest, datearr........... numavion) avec numavion qui est la clé étrangére venant de la table avion.
Mon probléme est qu'à l'exécution quand je clique sur le bouton d'ajout il m'affiche le message suivante: "navion n'est pas valide car il n'existe pas dans la liste des éléments, nom du paramétre: value".
Voici mes codes
formulaire qui fait l'ajout: addvol.asp
addvol.aspx.csCode:
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 <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AddVol.aspx.cs" Inherits="ASI.Formulaire.AddVol" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" runat="server" contentplaceholderid="MainContent"> <br /> <br /> <br /> <br /> <div style="height: 429px"> Nom Vol:<asp:TextBox ID="nomvolV" runat="server" Width="144px" style="margin-left: 36px"></asp:TextBox> N°Avion/ <asp:DropDownList ID="navion" runat="server" DataSourceID="numeroavion" DataTextField="NumAvion" DataValueField="NumAvion"> </asp:DropDownList> <asp:SqlDataSource ID="numeroavion" runat="server" ConnectionString="<%$ ConnectionStrings:GESTIONCAConnectionString2 %>" SelectCommand="SELECT [NumAvion] FROM [AVION]"></asp:SqlDataSource> <asp:SqlDataSource ID="numavion" runat="server" ConnectionString="<%$ ConnectionStrings:GESTIONCAConnectionString2 %>" SelectCommand="SELECT [NumAvion], [NomAvion] FROM [AVION]"> </asp:SqlDataSource> NbreEscale(s): <asp:TextBox ID="nbescale" runat="server" style="margin-left: 9px" Width="150px"></asp:TextBox> <br /> <br /> Prix <asp:TextBox ID="prixbillet" runat="server" style="margin-left: 5px" Width="148px"></asp:TextBox> <br /> <br /> <br /> Date Départ:; <asp:TextBox ID="datedepV" runat="server" style="margin-left: 0px" Width="145px"></asp:TextBox> Date Arrivée: <asp:TextBox ID="datearrV" runat="server" Width="144px" style="margin-left: 9px"></asp:TextBox> <br /> <br /> Ville Départ <asp:DropDownList ID="villedepV" runat="server" DataSourceID="nomville" DataTextField="NomVille" DataValueField="NomVille" onselectedindexchanged="villedepV_SelectedIndexChanged" Height="26px" style="margin-left: 0px" Width="149px"> </asp:DropDownList> <asp:TextBox ID="valvilldep" runat="server" Visible="False" Width="25px"></asp:TextBox> Ville Arrivée: <asp:DropDownList ID="villearrV" runat="server" DataSourceID="nomville" DataTextField="NomVille" DataValueField="NomVille" Height="26px" Width="145px"> </asp:DropDownList> <asp:Button ID="btn_addvol" runat="server" Text="Valider" onclick="btn_addvol_Click" Width="122px" /> <asp:Button ID="Fermer" runat="server" Text="Fermer" style="margin-left: 0px" Width="113px" /> <asp:SqlDataSource ID="nomville" runat="server" ConnectionString="<%$ ConnectionStrings:GESTIONCAConnectionString2 %>" SelectCommand="SELECT [NomVille] FROM [VILLE]"></asp:SqlDataSource> <asp:SqlDataSource ID="NomAvion" runat="server" ConnectionString="<%$ ConnectionStrings:GESTIONCAConnectionString2 %>" SelectCommand="SELECT [NomAvion] FROM [AVION]"></asp:SqlDataSource> <br /> </div> </asp:Content>
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 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ASI.Classe; using GESTIONCA.Modele; namespace ASI.Formulaire { public partial class AddVol : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btn_addvol_Click(object sender, EventArgs e) { try { u.MInsertVol(nomvolV.Text, int.Parse(nbescale.Text),villdepV.Text, DateTime.Parse(datedepV.Text), valvilledest.Text, DateTime.Parse(datearrV.Text), float.Parse(prixbillet.Text), int.Parse(this.navion.SelectedValue)); nomvolV.Text = ""; navion.SelectedValue = ""; nbescale.Text = ""; villedepV.Text = ""; datedepV.Text = ""; villearrV.SelectedValue = ""; datearrV.Text = ""; prixbillet.Text = ""; TEST.Text = "VOL AJOUTE AVEC SUCCES"; } catch (Exception ex) { TEST.Text = ex.Message; } }} }
La méthode qui attaque la base de données
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 public void MInsertVol(string nomvol, int nbescale, string nomvilledep, DateTime datedep, string villearr, DateTime datearr, float prixbillet, int navion) { string insertReqVol = "insert into VOL (NomVol, DateDepartVol, VilleDepartVol, DateArriveeVol, VilleArriveeVol, NbreEscaleVol, PrixBillet, NumAvion) values('{0}','{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')"; SqlConnection conn = null; vol v = new vol(); try { conn = new SqlConnection(); conn.ConnectionString = sconn; conn.Open(); v.nbescale = nbescale; v.prixbillet = prixbillet; v.vdatearriv = datearr; v.vdatedep = datedep; v.vvilledep = nomvilledep; v.vvilledest = villearr; v.prixbillet = prixbillet; v.nbescale = nbescale; v.nomvol = nomvol; v.numavion = navion; string req = string.Format(insertReqVol, v.nomvol, v.vdatedep, v.vvilledep, v.vdatearriv, v.vvilledest, v.nbescale, v.prixbillet, v.numavion); // string req = string.Format(insertReqVol, v.nomvol, v.vnomavion, v.nbescale, v.vvilledep, v.vdatedep, v.vvilledest, v.vdatearriv, v.prixbillet); SqlCommand cmd = new SqlCommand(req, conn); cmd.ExecuteNonQuery(); } catch (Exception) { //this.TEST.Text = ex.Message; } finally { if (conn != null) conn.Close(); } }