Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft ASP .NET Professional Projects - Premier Press

.pdf
Скачиваний:
147
Добавлен:
24.05.2014
Размер:
4.63 Mб
Скачать

Dim VFieldName As string

vDisplay = Display + "000000000000000000000000000000000000000000"

vRequired = RequiredFields+"000000000000000000000000000000000000000000"

veditable = editable + "000000000000000000000000000000000000000000"

FieldsCount = 0

s = "<A HREF=" + ExitPage + ">Back</A>"

me.Controls.Add(new LiteralControl(s))

s= "<table bgcolor ='antiquewhite' style='font: 8pt verdana'>" me.Controls.Add(new LiteralControl(s)) me.Controls.Add(new LiteralControl("<tr>"))

If mode = "insert" then

s="<td colspan='2' bgcolor='#aaaadd'"

s= s + " style='font:10pt verdana'>Add a New Record:</td>" me.Controls.Add(new LiteralControl(s))

Else

s="<td colspan='2' bgcolor='#aaaadd' "

s= s + " style='font:10pt verdana'>Edit Record:</td> " me.Controls.Add(new LiteralControl(s))

End If

me.Controls.Add(new LiteralControl("</tr>")) '**************************************

'UPDATE MODE '************************************* If mode = "update" then

For Each r in t.Rows

For Each c in t.Columns 'Get the field name

vFieldName = FieldNamesArray(FieldsCount)

If len(vFieldName) < 1 then

vFieldName = c.ToString

End If

'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(vFieldName)) me.Controls.Add(new LiteralControl("</td>")) 'value

'------Read only field hence label

If veditable.chars(FieldsCount) = "0" me.Controls.Add(new LiteralControl("<td>")) me.Controls.Add(new LiteralControl(r(c).ToString)) me.Controls.Add(new LiteralControl("</td>")) '------Editable Fields------

Else

'------Drop Down List------

If isDropDown(c.ToString) then 'get the index & other parameters idx = GetDDLindex((c.ToString)) vDisplayField =DisplayField(idx) vCodeField =CodeField(idx) vDDLSql =DDLSql(idx)

me.Controls.Add(new LiteralControl("<td>")) Dim DDL As New DropDownList

DDL.ID = c.ToString DDL.DataTextField = vDisplayField DDL.DataValueField = vCodeField me.Controls.Add(DDL)

'---Populate the drop down---

Dim mSql As String

msql = vDDLSQL

myConnection = New OleDbConnection(ConnStr) myCommand = New OleDbDataAdapter(mSql, myConnection) myCommand.Fill(ds, c.ToString)

DDL.DataSource = ds.Tables(c.ToString).DefaultView DDL.DataBind

'set the display field Dim dv2 As dataview

dv2 = new DataView(ds.Tables(c.ToString))

If c.DataType.ToString = "System.String" Then msql = vCodeField + " = '" + r(c).ToString +"'"

Else

msql = vCodeField + " = " + r(c).ToString End If

dv2.RowFilter = msql

DDL.Selecteditem.text = dv2(0)(vDisplayField).ToString DDL.Selecteditem.value = dv2(0)(vCodeField).ToString me.Controls.Add(new LiteralControl("</td>"))

Else

'--------Text Boxes--------

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 LiteralCont rol("</td>")) End If

'------Required field ------

If vRequired.chars(FieldsCount) = "1"

Dim vReq As New RequiredFieldValidator vReq.controltovalidate = c.ToString vReq.errormessage= "Please enter " + c.ToString me.Controls.Add(vReq)

End If

End If

End If

FieldsCount = FieldsCount + 1

Next c

Next r '**************************************

'INSERT MODE '************************************* Else

For Each c in t.Columns 'Get the field name

vFieldName = FieldNamesArray(FieldsCount)

If len(vFieldName) < 1 then

vFieldName = c.ToString End If

'Don't show this field

If vdisplay.chars(FieldsCount) = "0" or c.ToString = KeyField _ or veditable.chars(FieldsCount) = "0" then

Else

me.Controls.Add(new LiteralControl("<tr>")) 'label

me.Controls.Add(new LiteralControl("<td>")) me.Controls.Add(new LiteralControl(vFieldName)) me.Controls.Add(new LiteralControl("</td>"))

If isDropDown(c.ToString) then 'get the index & other parameters idx = GetDDLindex((c.ToString)) vDisplayField =DisplayField(idx) vCodeField =CodeField(idx) vDDLSql =DDLSql(idx)

me.Controls.Add(new LiteralControl("<td>")) Dim DDL As New DropDownList

DDL.ID = c.ToString DDL.DataTextField = vDisplayField DDL.DataValueField = vCodeField me.Controls.Add(DDL)

'---Populate the drop down---

Dim mSql As String msql = vDDLSQL

myCommand = New OleDbDataAdapter(mSql, myConnection) myCommand.Fill(ds, c.ToString)

DDL.DataSource = ds.Tables(c.ToString).DefaultView DDL.DataBind

me.Controls.Add(new LiteralControl("</td>")) Else

'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

'------Required field ------

If vRequired.chars(FieldsCount) = "1"

Dim vReq As New RequiredFieldValidator vReq.controltovalidate = c.ToString vReq.errormessage= "Please enter " + c.ToString me.Controls.Add(vReq)

End If

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

AddHandler AddButton.Click, AddressOf AddBtn_Click

Me.Controls.Add(AddButton)

'------------Validation Summary

me.Controls.Add(new LiteralControl("<br><br>"))

Dim vSummary As New ValidationSummary

vSummary.headertext="There were errors on the page:" me.Controls.Add(vSummary)

End Sub

Private Sub AddBtn_Click(Sender As Object, E As EventArgs) 'Build the procedure call

Dim s As String

Dim r As DataRow

Dim c As DataColumn

Dim cell As TableCell

Dim row As DataRow

Dim column As string

Dim Value As string

Dim Fieldscount As integer

Dim vdisplay As string

Dim veditable As string

veditable = editable + "000000000000000000000000000000000000000000" vDisplay = Display + "000000000000000000000000000000000000000" FieldsCount = 0

If mode = "update" then

s = "Execute " + Updateprocedure + ""

For Each r in t.Rows

For Each c in t.Columns

If vdisplay.chars(FieldsCount) = "0" or c.ToString = KeyField _ or veditable.chars(FieldsCount) = "0" then

Else

If isDropDown(c.ToString) then

Dim vdropdown As DropDownList

vdropdown = me.FindControl(c.ToString) column = c.ToString

value = vdropdown.SelectedItem.value Else

Dim tb As TextBox

tb = me.FindControl(c.ToString) column = c.ToString

Value = tb.text

End If

If Value = "" then

Value = "NULL"

End If

If c.DataType.ToString = "System.String" Then

If Value = "NULL" then

s = s + " @" + column + "=" + value + ", " Else

s = s + " @" + column + "='" + value + "', " End If

Else

s = s + " @" + column + "=" + value + ", " End If

End If

FieldsCount = FieldsCount + 1

Next c

Next r

s = s + "@" + KeyField + "=" + KeyValue

me.Controls.Add(new LiteralControl(s))

RunSql(s)

Else

s = "Execute " + Insertprocedure + ""

'Insert mode

For Each c in t.Columns

If vdisplay.chars(FieldsCount) = "0" or c.ToString = KeyField _ or veditable.chars(FieldsCount) = "0"then

Else

If isDropDown(c.ToString) then

Dim vdropdown As DropDownList vdropdown = me.FindControl(c.ToString) column = c.ToString

value = vdropdown.SelectedItem.value

Else

Dim tb As TextBox

tb = me.FindControl(c.ToString) column = c.ToString

Value = tb.text

End If

If Value = "" then

Value = "NULL"

End If

If c.DataType.ToString = "System.String" Then If Value = "NULL" then

s = s + " @" + column + "=" + value + ", " Else

s = s + " @" + column + "='" + value + "', " End If

Else

s = s + " @" + column + "=" + value + ", " End If

End If

FieldsCount = FieldsCount + 1

Next c

s = s + "@" + KeyField + "=NULL"

me.Controls.Add(new LiteralControl(s))

RunSql(s)

End If

End Sub

Sub RunSql(vSql as string) try

Dim s As string

Dim myConnection As OleDbConnection

myConnection = New OleDbConnection(ConnStr)

Dim mycommand As New OleDbCommand(vsql,myConnection) myconnection.Open()

myCommand.ExecuteNonQuery()

myconnection.Close()

Catch ex As OleDbException ' SQL error

Dim errItem As OleDbError

Dim errString As String

Dim s As string

For Each errItem In ex.Errors errString += ex.Message + "<br/>"

Next

s = "<br/><br/>SQL Error.Details follow:<br/>" & errString me.Controls.Add(new LiteralControl(s))

Catch myException as Exception me.Controls.Add(new LiteralControl("Exception: " +

myException.ToString())) End try

End Sub

'---------These Functions associated with DropDown Property---------

Function ParseDropDown()

Dim strChar As Object

Dim s As String

Dim j As Integer

Dim count As Integer Dim idx As integer count = 1

idx = 0

strChar = Split(DropDown, ";")