Bonjour,
Je possède 3 fichiers.
- Web.config : Ou dedans j'ai rajouté ceci :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 <appSettings> <add key="SqlServer" value="Data Source=chemindudatasource" /> </appSettings>
Le chemin du DataSource est ok (ici c'est juste réduit ^^)
-Global.asax :
- Et enfin le fichier : Register.aspx.cs Avec un bouton Save qui me permet de faire un INSERT
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 void Session_Start(object sender, EventArgs e) { SqlConnection oConnection = new SqlConnection(); oConnection.ConnectionString = ConfigurationSettings.AppSettings["SqlServer"]; oConnection.Open(); Session["oConnection"] = oConnection; } void Session_End(object sender, EventArgs e) { SqlConnection oConnection = (SqlConnection)Session["oConnection"]; oConnection.Close(); }
Mon problème est dans ce dernier fichier. Je n'arrive pas à acceder à ma connection "Oconnection" créée dans le Global.asax.
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 protected void btnSave_Click(object sender, EventArgs e) { // Déclaration de l'objet SqlCommand et de la requête SQL System.Data.SqlClient.SqlCommand oCommand; string sSQL; // Ecriture de la requête SQL sSQL = "INSERT INTO [dbo.table](name)"; sSQL = sSQL + " VALUES('" + txtName.Text + "')"; // Création de l'objet SqlCommand oCommand = new System.Data.SqlClient.SqlCommand(sSQL, OConnection); // On appelle la méthode ExecuteNonQuery pour éxécuter notre commande oCommand.ExecuteNonQuery(); }
Si quelqu'un peut m'aider je débute en ASP.NET.
Merci par avance
Partager