<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="Hersh" Namespace="Generic_chap7" Assembly="GenEditAdd_Chap7" %>
<%@Page Language="VB"  %>
<html>
<script language="VB" runat="server">
Dim myConnection As OleDbConnection
Dim myCommand    As OleDbDataAdapter
Dim ds           As New DataSet
Dim ConnStr      As String
Dim SQL          As String
Sub Page_Load(Source As Object, E As EventArgs) 
	ConnStr = "Provider=SQLOLEDB; Data Source=(local); Initial Catalog=ASPNET;User ID=sa;"
	myConnection = New OleDbConnection(ConnStr)      
	if NOT (isPostBack)
		rebind
	end if	
End Sub
Sub ReBind()
	'设置DataSetCommand
	SQL = "select  m.*, g.code_display as category "
	SQL = SQL + "from masters m,  groups g "
	SQL = SQL + " where m.code_category = g.code_value"
	myCommand = New OleDbDataAdapter(SQL, myConnection)
	'使用 Fill方法填充dataset
	myCommand.Fill(ds, "masters")
	'绑定DataGrid	
	Grid1.DataSource=ds.Tables("masters").DefaultView
	Grid1.DataBind()
End Sub
Sub RunSql(sql as string)
	'捕获 验证控件的异常
	if not page.isvalid then
		response.write("Stored Procedure did not execute")
		rebind
		exit sub
	end if
	try
	
	Dim mycommand2 As New OleDbCommand(sql,myConnection)
	myconnection.Open()
	myCommand2.ExecuteNonQuery()
	myconnection.Close()
	'退出编辑
	Grid1.EditItemIndex = -1
	 Catch ex As OleDbException
	             ' SQL 错误
	             Dim errItem   As OleDbError
	             Dim errString As String
	             For Each errItem In ex.Errors
	                 errString += ex.Message + "<br/>"
	             Next
	             Response.write( "SQL 错误细节如下:<br/><br/>" & errString)
	 catch myException as Exception
	  Response.Write("出现异常: " + myException.ToString()) 
	end try
	rebind
End sub    	
 Sub Grid1_delete(sender As Object, e As DataGridCommandEventArgs)
   Dim code_value as string   = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
   Dim sql as string
   sql = "Delete from masters where code_value = " + cstr(code_value)
   RunSql(sql)
 end sub 
</script>
<head>
  <title>Masters DataGrid 1</title>
</head>
<body>
<br>
<form runat=server>
<b>Accounts表:</b><br>
<table width="95%">
<tr>
<td valign="top">
	<asp:DataGrid id="Grid1" runat="server"
	    AutoGenerateColumns="false"
	    BackColor="White"
	    BorderWidth="1px" BorderStyle="Solid" BorderColor="Tan"
	    CellPadding="2" CellSpacing="0"
	    Font-Name="Verdana" Font-Size="8pt"
	    OnDeleteCommand = "Grid1_delete"
            DataKeyField="code_value"
	    >
<Columns>
	    <asp:HyperLinkColumn Text="编辑" DataNavigateUrlField="code_value"
          DataNavigateUrlFormatString="config_masters.aspx?code_value={0}"/>
           <asp:HyperLinkColumn Text="添加" DataNavigateUrlField="code_value"
          DataNavigateUrlFormatString="config_masters.aspx?code_value=0"/>
          <asp:ButtonColumn Text="删除" CommandName="Delete" HeaderText="删除"/>
	    <asp:BoundColumn HeaderText="Account" DataField="code_display">
	      <HeaderStyle Width="150px"/>
		
	    </asp:BoundColumn>
	    <asp:BoundColumn HeaderText="Group" DataField="category">
		  <HeaderStyle Width="150px"/>
		
	    </asp:BoundColumn>
	    <asp:BoundColumn HeaderText="Type" DataField="type">
	      <HeaderStyle Width="150px"/>
		
	    </asp:BoundColumn>
	    <asp:BoundColumn HeaderText="Opening" DataField="opening">
		  <HeaderStyle Width="150px"/>
		  
	    </asp:BoundColumn>
	    <asp:BoundColumn HeaderText="Closing" DataField="closing">
		  <HeaderStyle Width="150px"/>
		 
	    </asp:BoundColumn>
 </Columns>
	  <HeaderStyle BackColor="DarkRed" ForeColor="White" Font-Bold="true"/>
	  
	  <ItemStyle ForeColor="DarkSlateBlue"/>
	  
	  <AlternatingItemStyle BackColor="Beige"/>
	   
	</asp:DataGrid>
</td>
<td valign="top">
</td>
 </tr>
 </table>
</form>
</body>
</html>
