Imports Imports Partial Class Inherits Dim As New Dim As New Dim As New Dim As String Dim As Boolean Dim As New Protected Sub Byval As Object Byval As Handles Me If Not Then
Imports Imports Partial Class Inherits Dim As New Dim As New Dim As New Dim As String Dim As Boolean Dim As New Protected Sub Byval As Object Byval As Handles Me If Not Then
Page Dim cnn As New SqlConnection Dim sqladap As New SqlDataAdapter Dim dt As New DataTable Dim sql As String = "select * from Nha_san_xuat" Dim trangthai As Boolean Dim sqlcom As New SqlCommand Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then hang_san_xuat() ''hien thi thong tin nha xuat ban dau tien 'If (dt.Rows.Count > 0) Then ' txtmahangsx.Text = dt.Rows(0)("Mnsx") ' txttenhangsx.Text = dt.Rows(0)("Ten_nha_san_xuat") ' txtdiachi.Text = dt.Rows(0)("Dia_chi") ' txtdienthoai.Text = dt.Rows(0)("Dien_thoai") 'End If Visiblebutton(True) End If End Sub Public Sub hang_san_xuat() Try cnn = New SqlConnection(Application("cnn")) cnn.Open() sqladap = New SqlDataAdapter(sql, cnn) sqladap.Fill(dt) If (dt.Rows.Count > 0) Then GridView1.DataSource = dt GridView1.DataBind() ' txtmahangsx.Text = dt.Rows(0)("Mnsx")
txttenhangsx.Text = dt.Rows(0)("Ten_nha_san_xuat") txtdiachi.Text = dt.Rows(0)("Dia_chi") txtdienthoai.Text = dt.Rows(0)("Dien_thoai") Else lblerror.Text = "Khng c d liu" End If Catch ex As Exception lblerror.Text = "Error:" & ex.Message Finally cnn.Close() sqladap.Dispose() dt.Dispose() cnn.Dispose() End Try End Sub
Protected Sub cmdthem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdthem.Click 'text rong txtmahangsx.Text = "" txttenhangsx.Text = "" txtdiachi.Text = "" txtdienthoai.Text = "" 'hien nut luu & khong luu Visiblebutton(False) 'tro chuot ve nha xuat ban txtmahangsx.Focus() Session("trangthai") = True End Sub Protected Sub txtsua_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdsua.Click 'hien nut luu & khong luu Visiblebutton(False) Session("trangthai") = False
End Sub Public Sub Visiblebutton(ByVal b As Boolean) cmdthem.Visible = b cmdluu.Visible = b cmdsua.Visible = b cmdxoa.Visible = b cmdluu.Visible = Not b cmdkhongluu.Visible = Not b End Sub 'phan trang Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging GridView1.PageIndex = e.NewPageIndex hang_san_xuat() End Sub Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged Dim i As Integer = GridView1.SelectedIndex txtmahangsx.Text = GridView1.Rows(i).Cells(0).Text txttenhangsx.Text = GridView1.Rows(i).Cells(1).Text txtdiachi.Text = GridView1.Rows(i).Cells(2).Text txtdienthoai.Text = GridView1.Rows(i).Cells(3).Text End Sub Protected Sub cmdluu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdluu.Click cnn = New SqlConnection(Application("cnn")) cnn.Open() Dim strsql As String = "" Try
sqlcom.Connection = cnn If Session("trangthai") = True Then strsql = "insert into Nha_san_xuat values('" & txtmahangsx.Text & "','" & txttenhangsx.Text & "','" & txtdiachi.Text & "','" & txtdienthoai.Text & "') " sqlcom.CommandText = strsql sqlcom.CommandType = CommandType.Text Else strsql = "update Nha_san_xuat set Ten_nha_san_xuat='" & txttenhangsx.Text & "',Dia_chi='" & txtdiachi.Text & "',Dien_thoai='" & txtdienthoai.Text & "'" & "where Mnsx='" & txtmahangsx.Text & "'" End If sqlcom.CommandText = strsql sqlcom.CommandType = CommandType.Text Dim i As Integer i = sqlcom.ExecuteNonQuery() If (i > 0) Then lblerror.Text = "Thc hin thnh cng" hang_san_xuat() Else lblerror.Text = "Khng thnh cng" End If Catch ex As Exception lblerror.Text = "Li: " & ex.Message Finally cnn.Close() sqlcom.Dispose() cnn.Dispose() End Try ' Visiblebutton(True) End Sub Protected Sub cmdxoa_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdxoa.Click cnn = New SqlConnection(Application("cnn"))
cnn.Open() Dim strsql As String = "" Try sqlcom.Connection = cnn sqlcom.CommandText = "delete from Nha_san_xuat where Mnsx=" & txtmahangsx.Text sqlcom.CommandType = CommandType.Text Dim i As Integer i = sqlcom.ExecuteNonQuery() If (i > 0) Then lblerror.Text = "Thc hin thnh cng" hang_san_xuat() Else lblerror.Text = "Khng thnh cng" End If Catch ex As Exception lblerror.Text = "Li: " & ex.Message Finally cnn.Close() sqlcom.Dispose() cnn.Dispose() End Try End Sub Protected Sub cmdkhongluu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdkhongluu.Click hang_san_xuat() Visiblebutton(True) End Sub End Class