Option Strict Off
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic

Namespace Generic_Chap7
Public Class GenEditAdd_Chap7 : Inherits Control : Implements INamingContainer

Private ls_display as string
Private ls_where as string
Private ls_sql as string
Private ls_ConnStr as string
Private ls_keyField as string
Private ls_keyValue as string
Private ls_procedure as string
Private ls_exitpage as string  
Private lt_datatable as datatable
Private ls_mode as string
Protected mytbl as table
Public Property Mode as string
	Get
		Return Cstr(ViewState("ls_mode"))
	End Get
	Set
		ViewState("ls_mode") = value
	End Set
End Property
Public Property ExitPage as string
	Get
		Return Cstr(ViewState("ls_exitpage"))
	End Get
	Set
		ViewState("ls_exitpage") = value
	End Set
End Property
Public Property t as datatable 
	Get
		Return lt_datatable
	End Get
	Set
		lt_datatable = value
	End Set
End Property
Public Property KeyField as string 
	Get
		Return Cstr(ViewState("ls_keyfield"))
	End Get
	Set
		ViewState("ls_keyfield") = value
	End Set
End Property
Public Property KeyValue as string 
	Get
		Return Cstr(ViewState("ls_keyvalue"))
	End Get
	Set
		ViewState("ls_keyvalue") = value
	End Set
End Property

Public Property Procedure as string 
	Get
		Return Cstr(ViewState("ls_procedure"))
	End Get
	Set
		ViewState("ls_procedure") = value
	End Set
End Property
     
 Public Property display as string 
	Get
		Return Cstr(ViewState("ls_display"))
	End Get
	Set
		ViewState("ls_display") = value
	End Set
End Property
Public Property Where as string 
	Get
		Return Cstr(ViewState("ls_where"))
	End Get
	Set
		ViewState("ls_where") = value
	End Set
End Property
Public Property SQL as string 
	Get
		Return Cstr(ViewState("ls_sql"))
	End Get
	Set
		ViewState("ls_sql") = value
	End Set
End Property
Public Property ConnStr as string 
	Get
		Return Cstr(ViewState("ls_ConnStr") )
	End Get
	Set
		ViewState("ls_ConnStr")  = value
	End Set
End Property

Protected Overrides Sub CreateChildControls()
	Dim dv as DataView
	Dim myConnection As OleDbConnection
	Dim myCommand    As OleDbDataAdapter
	Dim ds           As New DataSet
	Dim vSql as string
	If len(Where) < 1 then
		vSql = SQL
		mode = "insert"
	Else
		vSql = SQL + Where
		mode = "update"
	End If
	myConnection = New OleDbConnection(ConnStr)    
	myCommand = New OleDbDataAdapter(vSql, myConnection)
	myCommand.Fill(ds, "vtable")
	dv = new DataView(ds.Tables("vtable"))
	   Dim Fields as Integer
	   'Dim t As DataTable
	   t = dv.Table
	   Dim r As DataRow
	   Dim c As DataColumn
	   Dim cell as TableCell
	   Dim row as DataRow
	   Dim Fieldscount as integer 
	   Dim s as string
	   Dim vdisplay as string
	   vDisplay = Display + "000000000000000000000000000000000000000000"
	   FieldsCount = 0
	   s = "<A HREF=" + ExitPage + ">Back</A>"
	   me.Controls.Add(new LiteralControl(s))
	   me.Controls.Add(new LiteralControl("<table bgcolor ='antiquewhite' style='font: 8pt verdana'>"))
	   me.Controls.Add(new LiteralControl("<tr>"))
	   If mode = "insert" then
		me.Controls.Add(new LiteralControl("<td colspan='2' bgcolor='#aaaadd' style='font:10pt verdana'>Add a New Record:</td>"))
	   Else
		me.Controls.Add(new LiteralControl("<td colspan='2' bgcolor='#aaaadd' style='font:10pt verdana'>Edit Record:</td>"))
	   End if
	   me.Controls.Add(new LiteralControl("</tr>"))
	   If mode = "update" then
	      For Each r in t.Rows
		      For Each c in t.Columns
		      'Don't show this field
		      IF  vdisplay.chars(FieldsCount) = "0" or c.ToString = KeyField then
		      Else
			      me.Controls.Add(new LiteralControl("<tr>"))
			      'label
			      me.Controls.Add(new LiteralControl("<td>"))
			      me.Controls.Add(new LiteralControl(c.ToString))
			      me.Controls.Add(new LiteralControl("</td>"))	
			     'value
			     me.Controls.Add(new LiteralControl("<td>"))
			     Dim Box As New TextBox
			     Box.Text = r(c).ToString
			     Box.ID = c.ToString
			     me.Controls.Add(box) 
			     me.Controls.Add(new LiteralControl("</td>"))	
		     end if	
		 FieldsCount = FieldsCount + 1
		Next c
	     Next r   
	  Else  ' Insert Mode
			For Each c in t.Columns
			  IF  vdisplay.chars(FieldsCount) = "0" or c.ToString = KeyField then
			  Else
			      me.Controls.Add(new LiteralControl("<tr>"))
			      'label
			      me.Controls.Add(new LiteralControl("<td>"))
			      me.Controls.Add(new LiteralControl(c.ToString))
			      me.Controls.Add(new LiteralControl("</td>"))	
			     'value
			     me.Controls.Add(new LiteralControl("<td>"))
			     Dim Box As New TextBox
			     Box.ID = c.ToString
			     me.Controls.Add(box) 
			     me.Controls.Add(new LiteralControl("</td>"))	
			 End if
			 FieldsCount = FieldsCount + 1
			Next c
	  End If
	 me.Controls.Add(new LiteralControl("</tr>"))
	 me.Controls.Add(new LiteralControl("</Table>"))
	'--------add button
   Dim AddButton As New Button
   if mode = "insert" then
	AddButton.Text = "Add"
   else
	AddButton.Text = "Update"
   end if
   Me.Controls.Add(AddButton)
End Sub
End Class
End Namespace
