Option Strict Off
Imports System
Imports System.Collections
Imports System.Text
Imports System.Data
Imports System.Data.OleDb
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class BaseClass
Inherits System.Web.UI.Page
protected Grid1 as DataGrid
Protected Message as label, title as label
Protected aposted_display as dropdownlist, selection as dropdownlist
Protected AddPanel as Panel
Protected adate as TextBox, aref as TextBox, adr_amount as TextBox
Protected  acr_amount as TextBox , anarr as TextBox 
Protected addshow as button
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)
    dim code as string, display as string
    
    '在 RC 版的代码中
    'code = Request.Form("Selection")
    'title.text = code
    code = Session("TheSelectionValue")
    title.text = Session("TheSelectionText")
    if code = "" then
       response.redirect("selection.aspx")
    end if
    UpdateSelection(code)	
    rebind
    end if
End Sub
Sub ReBind()

	SQL = " select m.code_value,m.code_display,t.*, h.* ,"
	sql = sql + "(select code_display from masters where code_value = t.posted_to) as posted_display"
	sql = sql + " from tr_header h,transactions t, masters m "
	sql = sql + " where t.doc_no = h.doc_no "
	sql = sql + " and  m.code_value = t.code_value"
	sql = sql + " and m.code_value = (select selection from tblSelection)"
	myCommand = New OleDbDataAdapter(SQL, myConnection)
	'使用Fill方法填充dataset
	myCommand.Fill(ds, "transactions")
	'绑定DataGrid	
	Grid1.DataSource=ds.Tables("transactions").DefaultView
	Grid1.DataBind()
	'填充在添加模式下显现的drop down list
	
	SQL = "Select * from masters where code_value <> "
	SQL = SQL + " (select selection from tblSelection)" 
	myCommand = New OleDbDataAdapter(SQL, myConnection)
	myCommand.Fill(ds, "masters")
	aposted_display.DataSource=ds.Tables("masters").DefaultView
	aposted_display.DataBind()
	addshow.visible = true
End Sub
Sub Grid1_Edit(Sender As Object, E As DataGridCommandEventArgs)
	Grid1.EditItemIndex = E.Item.ItemIndex
	ReBind()
End Sub
Sub Grid1_Cancel(Sender As Object, E As DataGridCommandEventArgs)
	Grid1.EditItemIndex = -1
	ReBind()
End Sub
Sub RunSql(vsql as string)

	try
	'sql = "Execute p_test " + vkey
	Dim mycommand2 As New OleDbCommand(vsql,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
           	     Message.Style("color") = "red"
	 	     Response.write("数据库错误 :" + errString)	
	 catch myException as Exception
	  Response.Write("出现异常: " + myException.ToString()) 
	  Message.Style("color") = "red"
	Finally
		message.text = vsql
	end try
End sub
sub hidePanel()
	if AddPanel.visible = true then
	   AddPanel.visible = false
	   '重新设定值
	   adate.text = ""
	   aref.text = ""
	   adr_amount.text = ""
	   acr_amount.text = ""
	   anarr.text = ""
	end if
end sub
Sub UpdateSelection(vselection)
	sql = "delete from tblSelection "
	sql = sql + " insert into tblSelection(selection)"
	sql = sql + " values('" + vselection + "')"
	runSql(sql)
End Sub
End Class
