Professional Documents
Culture Documents
Buoi4 HuongDan
Buoi4 HuongDan
Bi Tp 1: Code hng dn
CREATE PROCEDURE [dbo].[sp_CatSelect] @CatID as integer AS if @CatID=0 Select * from categories else select * from categories where CatID=@CatID
CREATE PROCEDURE [dbo].[sp_ProSelectByID] @CatID as integer, @An as integer, @hien as integer as Select * from products where catid=@Catid and (ProStatus=@An or ProStatus=@hien) GO
CREATE PROCEDURE [dbo].[sp_ProDelete] @chuoiProduct as varchar(100) as Declare @ChuoiThucThi as varchar(200) Set @ChuoiThucThi='Delete from Products where ProID in(' +@chuoiProduct+')' exec(@chuoithucthi) select errcode=1, errmsg=N'Xa thnh cng' GO
Trang 1/17
Trang 2/17
Trang 3/17
Function GetAn() As Integer If chkAn.Checked Then Return 0 Else Return -1 End If End Function Function GetHien() As Integer If chkHien.Checked Then Return 1 Else Return -1 End If End Function Protected Sub chkAn_CheckedChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAn.CheckedChanged LoadDuLieu(cboCat.SelectedValue, GetAn, GetHien) End Sub Protected Sub chkHien_CheckedChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles chkHien.CheckedChanged LoadDuLieu(cboCat.SelectedValue, GetAn, GetHien) End Sub Protected Sub cmdXoa_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdXoa.Click Dim mangchon As String mangchon = Request( "chkXoa") Dim BangkEtnoi1 As New ds.sp_ProDeleteDataTable Dim bien1 As New dsTableAdapters.sp_ProDeleteTableAdapter bien1.Fill(BangkEtnoi1, mangchon) LoadDuLieu(cboCat.SelectedValue, GetAn, GetHien) End Sub End Class
Trang 4/17
Bi tp 2: CREATE PROCEDURE [dbo].[sp_ProInsert] @ProName as nvarchar(50), @ProImage as nvarchar(50), @ProPrice as float, @ProDescript as nvarchar(50), @ProInform as nvarchar(50), @CatID as integer as Insert Products(ProName,ProImage,ProPrice,ProDescript,ProInform,CatID) values(@ProName,@ProImage,@ProPrice,@ProDescript,@ProInform,@ CatID) Select ErrCode=1,ErrMsg=N'T o thnh cng sn phm' GO
<%@ Page Language="VB" MasterPageFile="~/QuanLy/MasterPage.master" AutoEventWireup="false" CodeFile="AddSanPham.aspx.vb" Inherits="QuanLy_AddSanPham" title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID ="ContentPlaceHolder1" Runat="Server"> <h1 align="center"> TO MI SN PHM</h1><br /> <table width="500" align="center" class="DataTable"> <tr> <td colspan="5" class="Table_Header"> Add user</td> </tr> <tr> <td class="ContentCell">Proname</td> <td class="InputCell"><asp:TextBox ID="txtProname" runat="server"></asp:TextBox></td> </tr> <tr> <td class="ContentCell">ProImage</td> <td class="InputCell"><asp:TextBox ID="txtProImage" runat="server"></asp:TextBox></td> </tr> <tr> <td class="ContentCell">ProPrice</td> <td class="InputCell"><asp:TextBox ID="txtProPrice" runat="server"></asp:TextBox></td> </tr> <tr> <td class="ContentCell">ProDescript</td> <td class="InputCell"><asp:TextBox ID="txtProDescript" runat="server"></asp:TextBox></td> </tr> <tr> <td class="ContentCell">ProInform</td> <td class="InputCell"><asp:TextBox ID="txtProInform" runat="server"></asp:TextBox></td> Trang 5/17 </tr> <tr >
<tr> <td class="ContentCell">ProInform</td> <td class="InputCell"><asp:TextBox ID="txtProInform" runat="server"></asp:TextBox></td> </tr> <tr > <td colspan="5" align="right"> <asp:Label ID="lblLoi" runat="server"></asp:Label> <asp:Button ID="cmdCancel" runat="server" Text="Cancel" CssClass="Button" /> <asp:Button ID="cmdAdd" runat="server" Text="To sn phm" CssClass="Button" /> </td> </tr>
</table> </asp:Content>
Trang 6/17
Bi tp 3: CREATE PROCEDURE [dbo].[sp_ProUpdate] @ProID as integer, @ProName as nvarchar(50), @ProStatus as integer, @ProImage as nvarchar(50), @ProPrice as float, @ProDescript as nvarchar(50), @ProInform as nvarchar(50) as Update Products set ProName=@ProName,ProImage=@ProImage,ProPrice=@ProPrice, ProDescript=@ProDescript,ProInform=@ProInform,ProStatus=@ProStatus where ProID=@ProID Select ErrCode=1, ErrMsg=N'Hi u chnh sn phm thnh cng' GO
<tr> <td class="ContentCell">ProInform</td> <td class="InputCell"><asp:TextBox ID="txtProInform" runat="server"></asp:TextBox></td> </tr> <tr> <td class="ContentCell">ProStatus</td> <td class="InputCell"> <asp:DropDownList ID="cboStatus" runat="server"></asp:DropDownList></td> </tr> <tr > <td colspan="2" align="right" style="height: 23px"> <asp:Label ID="lblLoi" runat="server"></asp:Label> <asp:Button ID="cmdCancel" runat="server" Text="Cancel" CssClass="Button" /> <asp:Button ID="cmdEdit" runat="server" Text="Hiu chnh sn phm" CssClass="Button" /> </td> </tr>
</table> </asp:Content>
Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click LoadDuLieu(MaSP) End Sub Protected Sub cmdEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdEdit.Click Dim bangEdit As New ds.sp_ProUpdateDataTab le Dim bienEdit As New dsTableAdapters.sp_ProUpdateTableAdapter bangEdit.Reset() bienEdit.Fill(bangEdit, MaSP, Trim(txtProname.Text), cboStatus.SelectedIndex, Trim(txtProImage.Text), CInt(txtProPrice.Text), Trim(txtProDescript.Text) , Trim(txtProInform.Text)) lblLoi.Text = bangEdit.Rows(0).Item( "errmsg") End Sub End Class
Cu 4: CREATE PROCEDURE [dbo].[sp_CatSelect] @CatID as integer, @hien as integer, @an as integer AS if @CatID=0 Select * From Categories where CatStatus=@an or CatStatus=@hien else Select * From Categories where CatID=@CatID
CREATE PROCEDURE [dbo].[sp_CatDelete] @chuoiCatID as varchar(100) as Declare @ChuoiThucThi as varchar(200) Set @ChuoiThucThi='Delete from Categories where Catid in(' +@chuoiCatID+')' exec(@chuoithucthi) select errcode=1, errmsg=N'Xoa thanh cong'
Trang 9/17
<ItemTemplate> <tr> <td><%#Eval("Catid")%></td> <td><%#Eval("CatName")%></td> <td><img src="Uploads\<%#Eval("CatImage")%>" width="100px" height="100px" /></td> <td><%#IIf(Container.DataItem( "CatStatus") = 1, "<img src=""image/on.gif"">" , "<img src=""image/off.gif"">" )%></td> <td><%#Eval("CatDescript")%></td> <td><input type="checkbox" value="<%#Eval("Catid")%>" name="chkXoa" /></td> <td><a href="EditCat.aspx?CatID= <%#Eval("Catid")%>">Edit</a></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </td> </tr> </table> <br /> </asp:Content>
Trang 11/17
Protected Sub chkAn_CheckedChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAn.CheckedChanged
Protected Sub chkHien_CheckedChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles chkHien.CheckedChanged LayDuLieu(KiemTraAn, KiemTraHien) End Sub Protected Sub chkAn_CheckedChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAn.CheckedChanged LayDuLieu(KiemTraAn, KiemTraHien) End Sub Function KiemTraHien() As Integer If chkHien.checked = True Then Return 1 Else Return -1 End If End Function Function KiemTraAn() As Integer If chkAn.Checked = True Then Return 0 Else Return -1 End If End Function End Class
Bi tp 5 CREATE PROCEDURE [dbo].[sp_CatInsert] @Catname as nvarchar(50), @CatImage as nvarchar(250), @CatDescript as nvarchar(50) as if (exists(Select * from Categories where CatName = @Catname)) begin Select ErrCode=1, ErrMsg=N'Danh m c tn ti' return end Insert Into Categories(catname,catimage,catstatus,catdescript) values(@Catname,@CatImage,1,@CatDescript ) Select ErrCode=0, ErrMsg=N'T o thnh cng'
Trang 12/17
Trang 13/17
Imports System.IO Partial Class QuanLy_AddCat Inherits System.Web.UI.Page Protected Sub cmdAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAdd.Click Dim BangKetNoi As New ds.sp_CatInsertDataTable Dim Bien As New dsTableAdapters.sp_CatInsertTableAdapter BangKetNoi.Reset() Bien.Fill(BangKetNoi, Trim(txtCatname.Text), lblTenFile.Text, Trim(txtDes.Text)) lblLoi.Text = BangKetNo i.Rows(0).Item("errmsg") txtCatname.Text = "" txtDes.Text = "" txtCatname.Focus() End Sub Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click txtCatname.Text = "" txtDes.Text = "" txtCatname.Focus() End Sub Protected Sub btnFileUpload_Click( ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFileUpload.Click Dim strStatusMessage As String Dim strSaveFileAs As String strSaveFileAs = Server.MapPath( "uploads/" & myFileUpload.FileName) Try If myFileUpload.HasFile Then myFileUpload.SaveAs(strSaveFileAs) strStatusMessage = "File saved as: " + strSaveFileAs Else strStatusMessage = "No file was uploaded." End If Catch Ex As Exception strStatusMessage = "Unable to save the uploaded file. The error was: " & Ex.Message Finally lblSaveR esults.Text = strStatusMessage lblTenFile.Text = myFileUpload.FileName End Try End Sub End Class
Trang 14/17
Bi 6: CREATE PROCEDURE [dbo].[sp_CatUpdate] @Catid as integer, @Catname as nvarchar(50), @CatImage as nvarchar(250), @CatStatus as integer, @CatDescript as nvarcha r(50) AS update Categories set CatName=@Catname, CatImage=@CatImage, CatStatus=@CatStatus, CatDescript=@CatDescript where Catid=@CatiD select Errcode=1, ErrMsg=N'Hi u chnh danh mc th nh cng'
<tr> <td class="ContentCell" style="width: 145px; height: 28px"> Status </td> <td class="InputCell" style="height: 28px"> <asp:DropDownList ID="cboStatus" runat="server"> </asp:DropDownList></td> </tr> <tr> <td class="ContentCell" style="width: 145px"> Cat Description </td> <td class="InputCell"> <asp:TextBox ID="txtDes" runat="server"></asp:TextBox></td> </tr> <tr> <td align="right" colspan="2" style="height: 23px"> <asp:Label ID="lblLoi" runat="server"></asp:Label> <asp:Button ID="cmdCancel" runat="server" CssClass="Button" Text="Cancel" /> <asp:Button ID="cmdEdit" runat="server" CssClass="Button" Text="Hiu chnh danh mc" /> </td> </tr> </table> </asp:Content>
Protected Sub cmdEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdEdit.Click Dim BangKetNoi As New ds.sp_CatUpdateDataTable Dim Bien As New dsTableAdapters.sp_CatUp dateTableAdapter BangKetNoi.Reset() Bien.Fill(BangKetNoi, CatID, Trim(txtCatname.Text), lblTenFile.Text, cboStatus.SelectedIndex, Trim(txtDes.Text)) lblLoi.Text = BangKetNoi.Rows(0).Item( "errmsg") txtCatname.Text = "" txtDes.Text = "" txtCatname.Focus() End Sub Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click LoadDuLieu(CatID) End Sub Sub LoadDuLieu(ByVal catid As Integer) Dim BangkEtnoi As New ds.sp_CatSelectDataTable Dim bien As New dsTableAdapters.sp_CatSelectTableAdapter BangkEtnoi.Reset() bien.Fill(BangkEtnoi, catid, -1, -1) txtCatname.Text = BangkEtnoi.Rows(0).Item( "Catname") lblTenFile.Text = BangkEtnoi.Rows(0).Item( "CatImage") cboStatus.SelectedIndex = BangkEtnoi.Rows(0).Item( "CatStatus") txtDes.Text = BangkEtnoi.Rows(0).Item( "CatDescript") End Sub Sub InputDatainCombo() cboStatus.Items .Insert(0, "Khng s dng") cboStatus.Items.Insert(1, "c s dng") End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load CatID = Request( "CatID") If Not IsPostBack Then InputDatainCombo() LoadDuLieu(CatID) End If End Sub End Class
Trang 17/17