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
|
public partial class Ajout_Cat_2 : System.Web.UI.Page
{
DataSet ds = new DataSet();
Int32 nbr; // La variable qui me pose problème
protected void Page_Load(object sender, EventArgs e)
{
// Ici j'insère des données dans la BD
if (!Page.IsPostBack)
{
try
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Base_YConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("insert into categorie (nom_categorie,pere_id) values ('" + PreviousPage.Nom_Cat_1 + "',0)", myConnection);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
catch (Exception ex)
{
Label2.Text =ex.Message;
}
// Ici j'extrait l'ID de la donnée que je vient d' insérer
// je la récupère dans la variable " nbr "
try
{
String strSQL = "SELECT id ,pere_id FROM [categorie]where nom_categorie= '" + PreviousPage.Nom_Cat_1 + "' ";
SqlConnection myConnectio = new SqlConnection(ConfigurationManager.ConnectionStrings["Base_YConnectionString"].ConnectionString);
SqlCommand cm = new SqlCommand("SELECT id ,pere_id FROM [categorie]where nom_categorie= '" + PreviousPage.Nom_Cat_1 + "' ", myConnectio);
cm.Connection.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter(strSQL, myConnectio);
myAdapter.Fill(ds, "categorie");
DataRow[] choix = ds.Tables["categorie"].Select("pere_id is not null");
foreach (DataRow ch in choix)
{
nbr = Convert.ToInt32(ch.ItemArray[0]);
}
}
catch (Exception t)
{
Label7.Text = t.Message;
}
}
}
// Ici mon problème, je n'arrive pas à transférer la valeur de " nbr " dans
// la requête,dans la BD, au lieu de récupérer la valeur de " nbr ", je
// trouve tj "nbr" vaut 0
protected void Button3_Click(object sender, EventArgs e)
{
try
{
SqlConnection myConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["Base_YConnectionString"].ConnectionString);
SqlCommand ci = new SqlCommand("insert into categorie (nom_categorie,pere_id) values ('" + TextBox1.Text + "','" + nbr + "')", myConnect);
ci.Connection.Open();
ci.ExecuteNonQuery();
ci.Connection.Close();
ci.Connection.Dispose();
}
catch (Exception x)
{
Label2.Text =x.Message;
}
}
} |
Partager