Presentation is loading. Please wait.

Presentation is loading. Please wait.

E-commerce Applications Development

Similar presentations


Presentation on theme: "E-commerce Applications Development"— Presentation transcript:

1 E-commerce Applications Development 0731465
Done by Raneem Qaddoura

2 Lecture 1 Ado.net

3 Accessing a Database from an ASP Page
Create an ADO connection to a database Open the database connection Create an ADO recordset Open the recordset Extract the data you need from the recordset Close the recordset Close the connection

4 Configuring Your Database
SQL Server Express Free data engine Download from Browsing and Modifying Databases in Visual Studio Choose View ➤ Server Explorer Use the Data Connections node in the Server Explorer to connect to existing databases or create new ones Server name: localhost\SQLEXPRESS ADO.Net Storing the Connection String Making the Connection Manipulate data using recordset

5 Storing the Connection String
Web.config: <configuration> <connectionStrings> <add name="Pubs" connectionString="Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;"/> </connectionStrings> </configuration>

6 Making the Connection Asp.cs: using System.Web.Configuration;
using System.Data.SqlClient; // Define the ADO.NET Connection object. string connectionString = WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString; SqlConnection myConnection = new SqlConnection(connectionString); try { // Try to open the connection. myConnection.Open(); lblStatus.Text = "Connected Succssfully"; } catch (Exception err) { // Handle an error by displaying the information. lblStatus.Text = err.Message; } finally { // Either way, make sure the connection is properly closed. myConnection.Close(); }

7 Example - Select <form id="form1" runat="server"> <div>
<asp:DropDownList ID="lstStudents" runat="server" /> <asp:Label ID="lblResults" runat="server" /> </div> </form>

8 Example - Select protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { lstStudents.Items.Clear(); string selectSQL = "SELECT ID, sname, phone, hours FROM Student"; string connectionString = WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(selectSQL, con); SqlDataReader reader; try { con.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { ListItem newItem = new ListItem(); newItem.Text = reader["sname"].ToString(); newItem.Value = reader["ID"].ToString(); lstStudents.Items.Add(newItem); } reader.Close(); catch (Exception err) {lblResults.Text = err.Message;} finally { con.Close(); }

9 Example - Insert <form id="form1" runat="server"> <div>
<asp:Label ID="lblID" runat="server" Text="ID "></asp:Label> <asp:TextBox ID="txtID" runat="server"></asp:TextBox><br /> <asp:Label ID="ibiName" runat="server" Text="Name "></asp:Label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /> <asp:Label ID="lblPhone" runat="server" Text="Phone "></asp:Label> <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox><br /> <asp:Label ID="lblHours" runat="server" Text="Hours "></asp:Label> <asp:TextBox ID="txtHours" runat="server"></asp:TextBox><br /> <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /><br /> <asp:Label ID="lblStatus" runat="server" Text=""></asp:Label> </div> </form>

10 Example - Insert protected void btnAdd_Click(object sender, EventArgs e){ string insertSQL; insertSQL = "INSERT INTO Student "; insertSQL += "VALUES ('"; insertSQL += txtID.Text + "', '"; insertSQL += txtName.Text + "', '"; insertSQL += txtPhone.Text + "', '"; insertSQL += txtHours.Text + "')"; string connectionString = WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(insertSQL, con); // Try to open the database and execute the update. int added = 0; try { con.Open(); added = cmd.ExecuteNonQuery(); lblStatus.Text = added.ToString() + " records inserted."; } catch (Exception err) {lblStatus.Text = err.Message;} finally {con.Close();}

11 Example-Update How Can we update a record???

12 Example-Delete How Can we delete a record???

13 References Beginning, A. S. P. (2007). .NET 3.5 in C# 2008: From Novice to Professional , M. MacDonald, Apress.

14 The End


Download ppt "E-commerce Applications Development"

Similar presentations


Ads by Google