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

Microsoft ASP .NET Professional Projects - Premier Press

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

ds = t.Populate(ConnStr, SQL) Grid1.DataSource=ds.Tables("vTable").DefaultView Grid1.DataBind()

'populate drop down list

SQL = "Select * from groups order by code_display" ds = t.Populate(ConnStr, SQL)

acode_category.DataSource=ds.Tables("vTable").DefaultView acode_category.DataBind()

hidePanel() 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 hidePanel()

if AddPanel.visible = true then AddPanel.visible = false

end if End Sub

Sub RunSql(vSQL as string)

Dim t As New NameSpaceHersh.SQLService

Dim s As string

s = t.RunSQL(ConnStr,vSQL) Grid1.EditItemIndex = -1 Rebind

if s <> "Success" then

Message.Text = s

Message.Style("color") = "red"

End if

End Sub

End Class

Chapter 21: Incorporating Web Services in the

Transactions Form

Overview

In Chapter 16, I built the Transactions web form (Transactions.aspx together with the Code Behind form Transactions.vb) for the Personal Finance Manager. In this chapter, I will modify this form so that it uses the SQLService web service for its database interaction.

I will have to modify the ReBind and the RunSql methods, both of which reside in the Code Behind file called Transactions.vb. The aspx form Transactions.aspx remains unchanged.

The ReBind method binds a DataGrid and a DropDownList control to a database table. Here is the original method that I will change.

The Original ReBind Method

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) "

sql = sql + " 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)

myCommand.Fill(ds, "transactions")

'Binding a Grid

Grid1.DataSource=ds.Tables("transactions").DefaultView

Grid1.DataBind()

'populate account selection drop down list

' which is visible in the add mode

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

To use the SQLService web service, I have changed this to the following:

The Modified ReBind Method

Sub ReBind()

Dim t As New NameSpaceHersh.SQLService

Dim ds As DataSet

SQL = " select m.code_value,m.code_display,t.*, h.* ," sql = sql + "(select code_display from masters where "

sql = sql + " 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)" ds = t.Populate(ConnStr, SQL) Grid1.DataSource=ds.Tables("vTable").DefaultView Grid1.DataBind()

'populate account(add mode) selection drop down list 'SQL = "Select * from masters"

SQL = "Select * from masters where code_value <> "

SQL = SQL + " (select selection from tblSelection)"

ds = t.Populate(ConnStr, SQL)

aposted_display.DataSource=ds.Tables("vTable").DefaultView

aposted_display.DataBind()

addshow.visible = true

End Sub

In this function, I call the web service method Populate twice, each time passing to it the connection string and a SQL query as parameters. A DataSet containing the database rows is returned from the function, which I use to bind the DataGrid and DropDownList control respectively.

The second function that I need to modify is the RunSql function. This function is used to add, delete, or update a row. The appropriate event handlers pass to this function a SQL action query or a stored procedure name with appropriate parameters. This is the original code snippet:

The Original RunSql Function

Sub RunSql(vsql as string) try

Dim mycommand2 As New OleDbCommand(vsql,myConnection) myConnection.Open()

myCommand2.ExecuteNonQuery()

myConnection.Close() 'turn off editing Grid1.EditItemIndex = -1

Catch ex As OleDbException ' SQL error

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("DataBase Error :" + errString) Catch myException as Exception

Response.Write("Exception: " + myException.ToString()) Message.Style("color") = "red"

Finally message.text = vsql

end try End Sub

Sub hidePanel()

if AddPanel.visible = true then AddPanel.visible = false 'reset values

adate.text = "" aref.text = "" adr_amount.text = "" acr_amount.text = "" anarr.text = ""

end if End Sub

Here is the modified version of this function:

Modified Version of the RunSql Function

Sub RunSql(vsql as string)

Dim t As New NameSpaceHersh.SQLService Dim s As string

s = t.RunSQL(ConnStr,vSQL) Grid1.EditItemIndex = -1 Rebind

if s <> "Success" then

Message.Text = s

Message.Style("color") = "red"

End if

End Sub

This function calls the RunSql function of the web service and passes it a connection string as well as a SQL action query/procedure call string as input parameters. If the procedure/query was executed successfully, the string Success is returned from the function. Otherwise, the appropriate error string is returned, which is displayed in the browser.

Here is the listing of the modified Code Behind form Transactions.vb:

Transactions.vb

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 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); " ConnStr = ConnStr+" Initial Catalog=ASPNET;User ID=sa;" if NOT (isPostBack)

Dim code As string, display As string 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()

Dim t As New NameSpaceHersh.SQLService

Dim ds As DataSet

SQL = " select m.code_value,m.code_display,t.*, h.* ," sql = sql + "(select code_display from masters where " sql = sql + " 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)" ds = t.Populate(ConnStr, SQL) Grid1.DataSource=ds.Tables("vTable").DefaultView Grid1.DataBind()

'populate account(add mode) selection drop down list sql = "Select * from masters where code_value <> " sql = sql + " (select selection from tblSelection)"

ds = t.Populate(ConnStr, SQL) aposted_display.DataSource=ds.Tables("vTable").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)

Dim t As New NameSpaceHersh.SQLService

Dim s As string

s = t.RunSQL(ConnStr,vSQL) Grid1.EditItemIndex = -1 Rebind

if s <> "Success" then Message.Text = s Message.Style("color") = "red" End if

End Sub

Sub hidePanel()

if AddPanel.visible = true then AddPanel.visible = false

'reset values 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

Chapter 22: Incorporating Web Services in the Trial

Balance

Overview

In this chapter, I will modify the TrialBalance.aspx web form built in Chapter 17 (The Trial Balance Report) so that it can use the SQLService service. I need to modify the ReBind function that was used to bind two DataGrid controls to two DataView controls. Here is the code listing of the original function that I will modify:

The Original ReBind Function

Sub ReBind()

sql = "SELECT code_display, closing, "

sql = sql + " dr_amount = CASE type WHEN 'A' THEN "

sql = sql + " closing WHEN 'E' THEN closing

ELSE

0 END, "

sql = sql + "

cr_amount = CASE type

WHEN 'I'

"

 

sql = sql + "

THEN closing WHEN 'L'

THEN closing

ELSE 0 END "

sql = sql + " From Masters"

myCommand = New OleDbDataAdapter(SQL, myConnection) myCommand.Fill(ds, "Masters")

'Binding a Grid

Grid1.DataSource=ds.Tables("Masters").DefaultView

Grid1.DataBind()

'totals

sql = "SELECT 'Total' as nothing ,"

sql = sql + " (Select sum(closing) From masters " sql = sql + " where type in('A','E')) as dr_total , " sql = sql + " (Select sum(closing) From masters " sql = sql + " where type in('I','L')) as cr_total "

myCommand = New OleDbDataAdapter(SQL, myConnection) myCommand.Fill(ds, "totals")

'Binding a Grid

Grid2.DataSource=ds.Tables("totals").DefaultView

Grid2.DataBind()

End Sub

To use the SQLService service, I have changed this to the following:

The Modified ReBind() Function

 

 

 

 

 

 

 

 

Sub ReBind()

 

 

 

 

Dim t As New NameSpaceHersh.SQLService

 

 

Dim ds As DataSet

 

 

 

sql = "SELECT code_display, closing, "

 

 

sql = sql + " dr_amount = CASE type

WHEN 'A'

THEN "

sql = sql + " closing WHEN 'E' THEN closing ELSE

0 END, "

sql = sql + "

cr_amount = CASE type

WHEN 'I'

"

 

sql = sql + "

THEN closing WHEN 'L'

THEN closing

ELSE 0 END "