Download - Ung dung web chuong 7
![Page 1: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/1.jpg)
Chương 7
ADO.NET - II
![Page 2: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/2.jpg)
Mục tiêu Hiểu kết buộc dữ liệu Repeater control DataList control Cập nhật dữ liệu qua form Làm việc với dữ liệu XML
![Page 3: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/3.jpg)
Data Binding (kết buộc dữ liệu) Kết buộc dữ liệu là tiến trình liên kết dữ liệu lấy
được vào điều khiển để hiển thị Dữ liệu có thể kết buộc đến tất cả các điều khiển
dùng biểu thức kết buộc dữ liệu được đặt giữa thể <%#......%>
Dữ liệu được kết buộc đến điều khiển bất cứ lúc nào phương thức DataBind() được gọi
Kết buộc dữ liệu có thể thực hiện trên các kiểu dữ liệu khác nhau như:
Các thuộc tính Collections Biểu thức Kết quả của lời gọi hàm
![Page 4: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/4.jpg)
Thuộc tínhASP.NET cho phép các nhà phát triển kết buộc dữ liệu đến các biến public, thuộc tính của trang hay thậm chí thuộc tính của các điều khiển khác<html><title>DataBinding </title>
<script language="C#" runat="server">void Page_Load(Object sender, EventArgs e) {
Page.DataBind();}</script><form runat=server> <center><b><u> DataBinding</center></b></u><br> Enter a string and press tab <br><br>
![Page 5: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/5.jpg)
Thuộc tính<asp:textbox id = "txtControl" AutoPostback="true" runat= "server" /><br> <br>
<asp:label id = "lblControl" text = <%#txtControl.Text%> runat = "server" /><br><br>
</form>
</html>
![Page 6: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/6.jpg)
Biểu thức và phương thức<%@ Import Namespace="System.Data" %>
<html><title>DataBinding Expressions</title>
<script language="C#" runat="server">void Page_Load(Object Sender, EventArgs E) { Response.Write("<center><b><u>DataBinding
Expressions</center></b></u><br>"); if(!IsPostBack) {
DataTable mydt = new DataTable();DataRow mydr; mydt.Columns.Add(new DataColumn ("Numbers",
typeof(Int32)));
![Page 7: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/7.jpg)
Biểu thức và phương thứcfor (int i=0;i<=5;i++){
mydr = mydt.NewRow();mydr[0] = i;mydt.Rows.Add(mydr);
} dlMyList.DataSource = mydt; dlMyList.DataBind(); } } int Square(int num) {
int ans = num* num;return ans;
}
![Page 8: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/8.jpg)
Biểu thức và phương thức int Cube(int num) {
int ans = num*num*num;return ans;
} </script> <form runat=server> <asp:DataList id = "dlMyList" runat = "server"> <ItemTemplate>
Number : <%# ((DataRowView)Container.DataItem)["Numbers"] %>
Square : <%# Square ((int) ((DataRowView) Container.DataItem) ["Numbers"]) %>
Cube : <%# Cube ((int) ((DataRowView) Container.DataItem) ["Numbers"]) %>
![Page 9: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/9.jpg)
Biểu thức và phương thức
</ItemTemplate></asp:DataList></form>
</html>
![Page 10: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/10.jpg)
Phương thức DataBinder.Eval()
<%# DataBinder.Eval(Container.DataItem,"max_lvl","{0:c}") %>
Tên chứa mục dữ liệu
Tên trường
Định dạng chuỗi
Arguments
Phương thức này dùng để đánh giá biểu thức kết buộc dữ liệu ở thời gian thực thi và định dạng kết xuất được hiển thị trên trình duyệt.
Example:
![Page 11: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/11.jpg)
Repeater
Templates
ItemTemplate
AlternatingItemTemplate
HeaderTemplate
FooterTemplate
SeparatorTemplate
Là một container control, dùng để hiển thị danh sách các dữ liệu theo một mẫu định dạng nào đó cho từng mục dữ liệu.
Mẫu định dạng (template) là một tập các phần tử HTML hay các điều khiển dùng để định dạng hiển thị của điều khiển
![Page 12: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/12.jpg)
Repeater Ví dụ<%@ Import Namespace="System.Data" %><html> <title>Repeater Control</title> <head>
<script language="C#" runat="server">void Page_Load(Object Sender, EventArgs e) {
Response.Write("<center><b><u>Repeater</center> </b></u><br>");
if (!IsPostBack) {
DataTable mydt = new DataTable();DataRow mydr; mydt.Columns.Add(new DataColumn ("Numbers",
typeof(Int32)));
![Page 13: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/13.jpg)
Repeater Ví dụmydt.Columns.Add(new DataColumn ("Squares",
typeof(Int32)));mydt.Columns.Add(new DataColumn ("Cubes",
typeof(Int32)));for (int i=0;i<=2;i++){ mydr = mydt.NewRow();
mydr[0] = i;mydr[1] = i*i;mydr[2] = i*i*i;mydt.Rows.Add(mydr);
} Repeater1.DataSource = mydt; Repeater1.DataBind();
Repeater2.DataSource = mydt;Repeater2.DataBind();
![Page 14: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/14.jpg)
Repeater Ví dụ}
} </script> </head> <body> <form runat=server> <b>Repeater1:</b> <p> <asp:Repeater id=Repeater1 runat="server"> <HeaderTemplate> <table border=1> <tr> <td><b>Number</b></td>
<td><b>Square</b></td> <td><b>Cube</b></td>
</tr>
![Page 15: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/15.jpg)
Repeater Ví dụ </HeaderTemplate> <ItemTemplate> <tr>
<td> <%# DataBinder.Eval (Container.DataItem, "Numbers") %> </td>
<td> <%# DataBinder.Eval (Container.DataItem, "Squares") %> </td>
<td> <%# DataBinder.Eval(Container.DataItem, "Cubes") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <p>
![Page 16: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/16.jpg)
Repeater Ví dụ<b>Repeater2:</b><p><asp:Repeater id=Repeater2 runat="server"><HeaderTemplate> Number (Square) [Cube] :</HeaderTemplate><ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Numbers")
%> (<%# DataBinder.Eval(Container.DataItem,
"Squares") %>) [<%# DataBinder.Eval(Container.DataItem, "Cubes")
%>]</ItemTemplate><SeparatorTemplate>, </SeparatorTemplate></asp:Repeater>
![Page 17: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/17.jpg)
Repeater Output
</form> </body></html>
![Page 18: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/18.jpg)
DataList
Templates
ItemTemplate
AlternatingItemTemplate
SelectedItemTemplate
EditItemTemplate
HeaderTemplate
FooterTemplate
SeparatorTemplate
Cho phép người dùng chỉ ra luồng dữ liệu
![Page 19: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/19.jpg)
DataList – Ví dụ<%@ Import Namespace="System.Data" %><html> <title>DataList Control</title> <head> <script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e) { Response.Write("<center><b><u>Data List with
Alternating Columns</center></b></u><br>"); if (!IsPostBack)
{DataTable mydt = new DataTable();DataRow mydr; mydt.Columns.Add(new DataColumn ("Numbers",
typeof(Int32)));
![Page 20: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/20.jpg)
DataList Ví dụ mydt.Columns.Add(new DataColumn("Squares", typeof(Int32))); mydt.Columns.Add(new DataColumn("Cubes",typeof(Int32))); for (int i=0;i<30;i++) { mydr = mydt.NewRow();
mydr[0] = i;mydr[1] = i*i;mydr[2] = i*i*i;mydt.Rows.Add(mydr);
} dlMyList.DataSource = mydt; dlMyList.DataBind(); } } </script>
![Page 21: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/21.jpg)
DataList Ví dụ </head> <body> <form runat=server> <asp:DataList id="dlMyList" RepeatDirection="Horizontal" RepeatColumns="10" runat="server"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Numbers") %><br> <%# DataBinder.Eval(Container.DataItem, "Squares") %><br> <%# DataBinder.Eval(Container.DataItem, "Cubes") %><br> </ItemTemplate> <AlternatingItemTemplate> <i><b><%# DataBinder.Eval(Container.DataItem, "Numbers") %></b> <i><br> <i><b><%# DataBinder.Eval(Container.DataItem, "Squares") %><b><i><br><i><b><%# DataBinder.Eval(Container.DataItem, "Cubes") %><b><i><br>
![Page 22: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/22.jpg)
DataList Kết xuất
</AlternatingItemTemplate> </asp:DataList> </form> </body></html>
![Page 23: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/23.jpg)
Quản lý dữ liệu trong ASP.NET
DATADATA
InsertInsert
SelectSelectDeleteDelete
UpdateUpdate
![Page 24: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/24.jpg)
Chèn dữ liệuĐể chèn dữ liệu :
SqlCom.Parameters.Add(new SqlParameter("@pubid", SqlDbType.SmallInt, 2));
1. Nối kết CSDL
2. Tạo lệnh chèn trong CSDL
Tạo một tham số
Thiết lập nối kết CSDL
Chèn dữ liệu qua đối tượng command
mySqlCon = new SqlConnection ("server=SQLDB; uid= sa; pwd =password; database=pubs");
string myinsertCmd = "insert into publishers ( pub_id, pub_name, city, state, country ) values (@pubid, @pubname, @city, @state, @country)";SqlCommand mySqlCom = new SqlCommand(myinsertCmd, mySqlCon);
![Page 25: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/25.jpg)
Chèn dữ liệu
<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %><html> <title>Inserting Data in a Database</title>
<script language="C#" runat="server" Debug="true"> SqlConnection mySqlCon; protected void Page_Load(Object Src, EventArgs e) { mySqlCon = new SqlConnection("server=SQLDB;uid
=sa;pwd=password; database=pubs"); if(!IsPostBack)
BindGrid(); }
![Page 26: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/26.jpg)
Chèn dữ liệu public void AddPublisher(Object sender, EventArgs e) {
string strInsert = "insert into publishers ( pub_id, pub_name, city, state, country ) values (@pubid, @pubname, @city, @state, @country)"; SqlCommand mySqlCom = new SqlCommand(strInsert, mySqlCon); mySqlCom.Parameters.Add(new SqlParameter ("@pubid", SqlDbType.Char, 4)); mySqlCom.Parameters ["@pubid"]. Value = txtPub_Id.Text; mySqlCom.Parameters.Add(new SqlParameter ("@pubname", SqlDbType.VarChar, 40)); mySqlCom.Parameters ["@pubname"]. Value = txtPub_Name. Text; mySqlCom.Parameters.Add(new SqlParameter ("@city", SqlDbType.VarChar, 20)); mySqlCom.Parameters["@city"].Value = txtCity.Text;
![Page 27: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/27.jpg)
Chèn dữ liệu
mySqlCom.Parameters.Add(new SqlParameter("@state", SqlDbType.Char, 2)); mySqlCom.Parameters ["@state"]. Value = txtState.Text; mySqlCom.Parameters.Add(new SqlParameter ("@country", SqlDbType.VarChar, 30)); mySqlCom.Parameters ["@country"].Value = txtCountry.Text; mySqlCom.Connection.Open(); mySqlCom.ExecuteNonQuery(); Message.InnerHtml = "<b>Record Added</b><br>"; mySqlCom.Connection.Close(); txtPub_Id.Text = ""; txtPub_Name.Text = ""; txtCity.Text = ""; txtState.Text = ""; txtCountry.Text = "";
![Page 28: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/28.jpg)
Chèn dữ liệuBindGrid();
} public void BindGrid() { SqlDataAdapter mySqlda = new SqlDataAdapter ("select * from publishers where pub_id like '99%'", mySqlCon); DataSet myds = new DataSet(); mySqlda.Fill (myds, "publishers"); dbgMyGrid.DataSource = myds.Tables ["publishers"]. DefaultView; dbgMyGrid.DataBind(); } </script> <form runat="server">
<h2>Add a New Publisher:</h2><br>
![Page 29: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/29.jpg)
Chèn dữ liệu Publisher Id should start with 99 and contain 4 digits<br><br> Pubisher ID:          <asp:textbox id="txtPub_Id" runat="server"/> Name:    <asp:textbox id="txtPub_Name" runat="server"/> City:   <asp:textbox id="txtCity" runat="server"/> <br><br> State:   <asp:textbox id="txtState" runat="server"/> Country:   <asp:textbox id="txtCountry" runat="server"/> <br> <br>             <asp:button id="btnSubmit" Text="Submit" OnClick="AddPublisher" runat="server"/><br>
![Page 30: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/30.jpg)
Chèn dữ liệu<span id="Message" MaintainState="false" style="font:
arial 11pt;" runat="server"/><br><asp:DataGrid id="dbgMyGrid" runat= "server"/>
</form> </body></html>
![Page 31: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/31.jpg)
Cập nhật dữ liệu Một cách để trình bày giao tiếp cho cập nhật dữ
liệu là cung cấp một tập dữ liệu đến người dùng và cho phép người dùng chọn hàng để cập nhật
DataGrid có thể được dùng để hiển thị tất cả dữ liệu cho người dùng, và người dùng chọn hàng để cập nhật
Người dùng có thể chọn hàng để cập nhật dùng EditCommandColumn trong DataGrid
EditCommandColumn cung cấp liên kết cho 3 sự kiện:
Edit Command Update Command Cancel Command
![Page 32: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/32.jpg)
Cập nhật dữ liệu EditCommandColumn có thể thêm vào DataGrid như sau:
<asp:datagrid id="dbgMyGrid" runat="server" DataKeyField="pub_id" OnUpdateCommand="dbgMyGrid_Update" OnCancelCommand="dbgMyGrid_Cancel" OnEditCommand="dbgMyGrid_Edit"><Columns> <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" /></Columns></asp:datagrid>
Sự kiện có thể được thực hiện khi người dùng chọn lệnh trong EditCommandColumn
![Page 33: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/33.jpg)
Cập nhật dữ liệu EditItemIndex dùng để xác định vị trí của
hàng được cập nh Sau khi giá trị hàng được gán cho EditItemIndex, hàng có thể được biên tập
Ví dụ
public void dbgMyGrid_Edit(Object sender, DataGridCommandEventArgs e){
dbgMyGrid.EditItemIndex = (int)e.Item.ItemIndex;}
![Page 34: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/34.jpg)
Cập nhật dữ liệu
mySqlCmd.Parameters ["@pubid"].Value = dbgMyGrid.DataKeys[(int)e.Item.ItemIndex];
Lấy khóa chính của hàng được chọn cập nhật
mySqlCmd.Parameters ["@pubname"].Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
Gán các giá trị thay đổi đến hàng qua các tham số
![Page 35: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/35.jpg)
Cập nhật dữ liệu<%@ Import Namespace="System.Data.SqlClient" %><%@ Import Namespace="System.Data" %><HTML> <title>Updating Data in a Database</title> <script language="C#" runat="server"> SqlConnection mySqlCon; protected void Page_Load(Object Src, EventArgs e) {
Response.Write("<center><b><u>Updating Data</center></b></u><br>");
mySqlCon = new SqlConnection ("server=SQLDB; uid=sa; pwd=password;database=pubs"); if(!IsPostBack)
BindGrid(); }
![Page 36: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/36.jpg)
Cập nhật dữ liệupublic void dbgMyGrid_Edit(Object sender, DataGridCommandEventArgs e) {
dbgMyGrid.Columns[0].HeaderText="Edit"; dbgMyGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid(); } public void dbgMyGrid_Cancel(Object sender, DataGridCommandEventArgs e) { dbgMyGrid.Columns[0].HeaderText="Cancel";
dbgMyGrid.EditItemIndex = -1;BindGrid();
} public void dbgMyGrid_Update(Object sender, DataGridCommandEventArgs e
![Page 37: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/37.jpg)
Cập nhật dữ liệu{
dbgMyGrid.Columns[0].HeaderText="Update";string strUpdate = "UPDATE Publishers SET pub_id = @pubid,
pub_name = @pubname, city = @city, state = @state, country = @country WHERE pub_id = @pubid"; SqlCommand mySqlCmd = new SqlCommand(strUpdate, mySqlCon); mySqlCmd.Parameters.Add(new SqlParameter("@pubid", SqlDbType.Char, 4)); mySqlCmd.Parameters.Add(new SqlParameter ("@pubname",SqlDbType.VarChar, 40)); mySqlCmd.Parameters.Add(new SqlParameter ("@city",SqlDbType.VarChar, 20)); mySqlCmd.Parameters.Add(new SqlParameter("@state",SqlDbType.Char, 2)); mySqlCmd.Parameters.Add(new SqlParameter("@country",SqlDbType.VarChar, 30));
![Page 38: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/38.jpg)
Cập nhật dữ liệu mySqlCmd.Parameters ["@pubid"]. Value = dbgMyGrid.DataKeys[(int)e.Item.ItemIndex]; mySqlCmd.Parameters ["@pubname"].Value=((TextBox)e.Item.Cells[2].Controls[0]).Text; mySqlCmd.Parameters ["@city"]. Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text; mySqlCmd.Parameters ["@state"].Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text; mySqlCmd.Parameters ["@country"].Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text; mySqlCon.Open();
![Page 39: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/39.jpg)
Cập nhật dữ liệu try { mySqlCmd.ExecuteNonQuery(); lblMessage.Text = "<b>Record Updated</b><br>" ; dbgMyGrid.EditItemIndex = -1; } catch(SqlException exc) {
if (exc.Number == 2627)lblMessage.Text = "ERROR: A record already
exists with the same primary key";else
lblMessage.Text = exc.ToString() + "ERROR: Could not update record, please ensure the fields are correctly filled out"; }
![Page 40: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/40.jpg)
Cập nhật dữ liệu mySqlCon.Close(); BindGrid(); } public void BindGrid() { SqlDataAdapter mySqlda = new SqlDataAdapter ("select * from publishers", mySqlCon); DataSet myds = new DataSet(); mySqlda.Fill(myds, "publishers"); dbgMyGrid.DataSource = myds.Tables ["publishers"]. DefaultView; dbgMyGrid.DataBind(); } </script> <body> <form id="Form1" runat="server">
![Page 41: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/41.jpg)
Cập nhật dữ liệu <h4><asp:label id="lblMessage" runat="server"> </asp:label> </h4><br> <ASP:Datagrid id="dbgMyGrid" runat="server" DataKeyField="pub_id" OnUpdateCommand="dbgMyGrid_Update" OnCancelCommand ="dbgMyGrid_Cancel" OnEditCommand= "dbgMyGrid_Edit"> <Columns> <asp:EditCommandColumn EditText="Edit" CancelText ="Cancel" UpdateText="Update" /> </Columns> </ASP:Datagrid></form> </body></HTML>
![Page 42: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/42.jpg)
Cập nhật dữ liệu
![Page 43: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/43.jpg)
Xóa dữ liệu OnDeleteCommand thực hiện nhiệm vụ xóa
hàng dữ liệu được chọn
<asp:DataGrid id="dbgMyGrid" runat="server" DataKeyField="pub_id" OnDeleteCommand=" dbgMyGrid_Delete">
<Columns><asp:ButtonColumn Text="Delete Publisher"
CommandName="Delete" />
</Columns></asp:DataGrid>
![Page 44: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/44.jpg)
Xóa dự liệu
<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %><html> <title>Deleting Data in a Database</title> <script language="C#" runat="server" Debug="true"> SqlConnection mySqlCon; protected void Page_Load(Object sender, EventArgs e) { Response.Write("<center><b><u>Deleting Data</center></b></u><br>");
mySqlCon = new SqlConnection ("server=SQLDB; uid =sa; pwd =password; database=pubs"); if (!IsPostBack)
BindGrid(); }
![Page 45: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/45.jpg)
Deleting Data Example Contd… public void dbgMyGrid_Delete(Object sender,
DataGridCommandEventArgs e) { string strDelete = "DELETE from publishers where pub_id = @pubid"; SqlCommand mySqlCmd = new SqlCommand(strDelete, mySqlCon);
mySqlCmd.Parameters.Add(new SqlParameter ("@pubid", SqlDbType.Char, 4)); mySqlCmd.Parameters["@pubid"].Value = dbgMyGrid.DataKeys [(int)e.Item.ItemIndex]; mySqlCon.Open();
try { mySqlCmd.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Deleted</b><br>"; }
![Page 46: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/46.jpg)
Cập nhật dữ liệu catch (SqlException) { Message.InnerHtml = "ERROR: Could not delete record"; Message.Style ["color"] = "red"; } mySqlCon.Close(); BindGrid(); } public void BindGrid() { SqlDataAdapter mySqlda = new SqlDataAdapter("select * from publishers", mySqlCon); DataSet myds = new DataSet(); mySqlda.Fill(myds, "publishers"); dbgMyGrid.DataSource =myds.Tables ["publishers"]. DefaultView;
![Page 47: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/47.jpg)
Cập nhật dữ liệu dbgMyGrid.DataBind(); } </script> <body> <form runat="server" ID="Form1"> <span id="Message" EnableViewState="false" runat="server" /><p> <asp:DataGrid id="dbgMyGrid" runat="server" DataKeyField="pub_id" OnDeleteCommand=" dbgMyGrid_Delete"> <Columns>
<asp:ButtonColumn Text="Delete Publisher" CommandName="Delete" /> </Columns> </asp:DataGrid> </form>
![Page 48: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/48.jpg)
Cập nhật dữ liệu </body></html>
![Page 49: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/49.jpg)
Quản lý dữ liệu XML
<rootelement xmlns = "x-schema:scheduledSchema.xsl">FileStream myfs = new FileStream (Server.MapPath ("xmldatagrid.xml") ,FileMode.Open,FileAccess.Read);
StreamReader myreader = new StreamReader(myfs);
DataSet myds = new DataSet();myds.ReadXml(myreader);
<rootelement xmlns=” x-schema:scheduledSchema.xsl”>
Schema
1
2
3
Mở tập tin
Gán streamreader
Đọc dữ liệu từ một streamreader
![Page 50: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/50.jpg)
Quản lý dữ liệu XML
DataView mySource = new DataView(myds.Tables[0]);DataGrid1.DataSource = mySource;DataGrid1.DataBind();
4 Kết buộc dự liệu
Ví dụ:
<%@ Import Namespace="System.IO" %><%@ Import Namespace="System.Data" %><html><title>XML Data</title>
<script language="C#" runat="server">void Page_Load(Object Src, EventArgs e) {
DataSet myds = new DataSet();
![Page 51: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/51.jpg)
XML Data in a DataGrid FileStream myfs = new FileStream(Server.MapPath("xmldatagrid.xml"),FileMode.Open, FileAccess.Read); StreamReader myreader = new StreamReader(myfs); myds.ReadXml(myreader); myfs.Close(); DataView mySource = new DataView(myds.Tables[0]); lblTableName.Text = mySource.Table.TableName; dbgMyGrid.DataSource = mySource; dbgMyGrid.DataBind(); } </script> <body> <h3><font face="Arial">XML Data for Table: <asp:label id="lblTableName" runat= "server"/></font> </h3>
![Page 52: Ung dung web chuong 7](https://reader033.vdocuments.pub/reader033/viewer/2022061111/545528fab1af9fc0638b476a/html5/thumbnails/52.jpg)
Hiển thị dữ liệu trong DataGrid </h3> <ASP:DataGrid id="dbgMyGrid" runat="server" /> </body></html>