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
| public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
ChargerListe();
}
}
void ChargerListe()
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "Data Source=PC-LAURENCE\\SQLEXPRESS; Initial Catalog=Librarie; Integrated Security=True;";
myConnection.Open();
// Requete SQL
SqlCommand myCommande;
myCommande = new SqlCommand("SELECT Titre, ISBN from Ouvrages", myConnection);
SqlDataReader lecteur = myCommande.ExecuteReader();
List<Book> books = new List<Book>();
while (lecteur.Read())
{
books.Add(new Book(lecteur["Titre"].ToString(), lecteur["ISBN"].ToString()));
}
DdlBooks.DataSource = books;
DdlBooks.DataTextField = "BookTitle";
DdlBooks.DataValueField = "Isbn";
DdlBooks.DataBind();
lecteur.Close();
myConnection.Close();
}
protected void DdlBooks_SelectedIndexChanged(object sender, EventArgs e)
{
if (DdlBooks.SelectedIndex > -1)
{
LbBook.Text = DdlBooks.SelectedItem.Text;
LbIsbn.Text = DdlBooks.SelectedValue;
}
}
protected void Confirmer_Click(object sender, EventArgs e)
{
// Instatiation de l'objet Book
Book NewBook = new Book(TxtIsbn.Text, TxtTitre.Text, TxtAuteur.Text, int.Parse(TxtCategorie.Text), decimal.Parse(TxtPrix.Text), int.Parse(TxtCD.Text), DateTime.Parse(TxtParution.Text), int.Parse(TxtPage.Text));
if (Page.IsValid)
{
// Connexion à la DB
// 1° Declaration de l'Objet connexion et création de l'objet
SqlConnection myConnection = new SqlConnection();
// 2° Ecriture de la chaine de connexion
myConnection.ConnectionString = "Data Source=PC-LAURENCE\\SQLEXPRESS; Initial Catalog=Librarie; Integrated Security=True;";
myConnection.Open();
// Requete SQL
SqlCommand myCommande;
myCommande = new SqlCommand("Insert INTO Ouvrages (ISBN, Titre, Auteur, Catégorie, Prix, CD, DateParution, NbPages) VALUES('" + TxtIsbn.Text + "', '" + TxtTitre.Text + "', '" + TxtAuteur.Text + "','" + int.Parse(TxtCategorie.Text) +"','"+ decimal.Parse(TxtPrix.Text) +"', '"+ int.Parse(TxtCD.Text) +"', '"+ DateTime.Parse(TxtParution.Text) +"', '"+ int.Parse(TxtPage.Text) +"')", myConnection);
myCommande.ExecuteReader();
}
}
} |
Partager