Tải bản đầy đủ (.pdf) (50 trang)

Tài liệu Practical Database Programming With Visual C#.NET- P17 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.98 MB, 50 trang )


9.5 Build ASP.NET Web Service to Update and Delete Data for SQL Server Database

823

Figure 9.86
Running result for the Web method GetSQLCourse.

Figure 9.87
Parameter - input Web interface.
c09.indd 823c09.indd 823 2/11/2010 3:02:07 PM2/11/2010 3:02:07 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
824
Chapter 9 ASP.NET Web Services
Figure 9.89
Parameter - input interface.
Figure 9.88
Running result of the Web method GetSQLCourseDetail.
To confi rm this data deleting, close the current running result interface shown in
Figure 9.90 and click on the Back button to return to the home page of the Web Service
project. Click on the Web method GetSQLCourse to run it to pick up all courses taught
by the selected faculty
Ying Bai
. Enter the faculty name
Ying Bai
into the Value box
as the input parameter to this method and click on the Invoke button to run it. The
running result is shown in Figure 9.91 .
From the running result shown in Figure 9.91 , it can be found that the course with
the
course_id


of CSE - 526 has been deleted from the Course table.
c09.indd 824c09.indd 824 2/11/2010 3:02:08 PM2/11/2010 3:02:08 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.5 Build ASP.NET Web Service to Update and Delete Data for SQL Server Database

825

Figure 9.90
Running result of the Web method SQLDeleteSP.

Figure 9.91
Running result of the Web method GetSQLCourse.
c09.indd 825c09.indd 825 2/11/2010 3:02:09 PM2/11/2010 3:02:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
826
Chapter 9 ASP.NET Web Services
To get a clearer picture for this data deleting, let ’ s try to run another Web method
GetSQLCourseDetail(). Close the current running result interface shown in Figure 9.91
and click on the Back button to return to the home page. Select and click the Web method
GetSQLCourseDetail to try to run it. Enter CSE - 526 as the
course_id
to the Value
box as the input parameter to this method and click on the Invoke button to run it.
The running process becomes very slow. The reason for this is because a message
box is displayed behind the top page. Try to move the current top page to either side of
the screen, and you can fi nd that a message box with a message "No matched course
found" has shown up. This means that the queried course has been deleted from the
Course table and it cannot be found from that table again.
Click on the OK button to the message box to close it, and the running result is dis-

played, which is shown in Figure 9.92 . The following returned values are displayed for
two member data:
• SQLOK:
false
• SQLError: No matched course found
This is identical with the warning message displayed in the message box as this
method runs. Close the current page and our Web Service project. Our Web Service
project is very successful.
As a reminder, it is highly recommended to recover all deleted data from all tables
in our sample database. To do that, open our sample database and the Course table from
either the Server Explorer in Visual Studio.NET or Microsoft SQL Server Management
Figure 9.92
Running result of the Web method GetSQLCourseDetail.
c09.indd 826c09.indd 826 2/11/2010 3:02:11 PM2/11/2010 3:02:11 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.7 Build Web-Based Web Service Clients to Use Web Services

827
Studio, and add all pieces of course information shown in Table 9.6 in Section 9.5.3.2 in
this chapter for the deleted course CSE - 526 into our Course table.
You can remove all message box methods MessageBox() from this Web Service
project to speed up the execution of this Web Service if you like. A completed Web
Service project WebServiceSQLUpdateDelete can be found at the folder DBProjects\
Chapter 9 located at the accompanying ftp site (see Chapter 1 ).
Next let ’ s take care of building some Windows - based and Web - based client projects
to use this Web Service.
9.6 BUILD WINDOWS - BASED WEB SERVICE CLIENTS TO
USE WEB SERVICES
In order to save space, Section 9.6, which provided a detailed discussion in how to build

a Windows - based client project WinClientSQLUpdateDelete to consume our Web
Service project WebServiceSQLUpdateDelete, has been moved to the accompanying ftp
site with a fi le named WinClientSQLUpdateDelete.pdf that can be found from the
folder DBProjects\Chapter 9\Doc that is located at the site />sci_tech_med/practical_database . For your convenience, a completed Windows - based
client project, WinClientSQLUpdateDelete, has also been developed and debugged,
which can be found from the folder DBProjects\Chapter 9 at the same ftp site.
9.7 BUILD WEB - BASED WEB SERVICE CLIENTS TO
USE WEB SERVICES
There is no signifi cant difference between building a Windows - based and a Web - based
client project to use a Web Service. To save time and space, we try to modify an existing
Web - based client project WebClientSQLInsert we developed in the previous section to
make it our new Web - based client project WebClientSQLUpdateDelete.
Actually we can copy and rename that entire project as our new Web - based client
project. However, we prefer to create a new ASP.NET website project, and then copy
and modify the Course page.
This section can be developed in the following sequences:
1. Create a new ASP.NET website project WebClientSQLUpdateDelete and add an
existing website page Course.aspx from the project WebClientSQLInsert into our new
project.
2. Add a Web Service reference to our new project and modify the Web form window of the
Course.aspx to meet our data updating and deleting requirements.
3. Modify the codes in the related methods of the Course.aspx.cs fi le to call the associated
Web method to perform our data updating and deleting. The code modifi cations include
the following sections:
a. Remove the Insert button ’ s Click method cmdInsert_Click() since we do not need any
data insertion action in this application.
b. Remove the user - defi ned FillCourseDataSet() method since no DataSet method will be
used in this application.
c09.indd 827c09.indd 827 2/11/2010 3:02:11 PM2/11/2010 3:02:11 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

828
Chapter 9 ASP.NET Web Services
c. Remove the TextChanged event method of the Course ID textbox since we do not need
this event and its event method in this application.
d. Modify the codes inside the Page_Load() method.
e. Develop the codes for the Update button ’ s Click method.
f. Develop the codes for the Delete button ’ s Click method.
g. Modify the codes in the Select button ’ s Click method and the related methods such as
ProcessObject() and FillCourseListBox().
h. Modify the codes in the SelectedIndexChanged event method of the course listbox
control and the related method FillCourseDetail().
Now let ’ s start these modifi cations with the fi rst step listed above.
9.7.1 Create New Website Project and Add Existing Web Page
Open Visual Studio.NET and go to the File|New Web Site menu item to create a new
website project. Enter C:\Chapter 9\WebClientSQLUpdateDelete into the name box
that is next to the Location box, and click on OK to create this new project.
On the opened new project window, right - click on our new project icon
WebClientSQLUpdateDelete from the Solution Explorer window, and select the item
Add Existing Item from the pop - up menu to open the Add Existing Item dialog box.
Browse to our Web project WebClientSQLInsert, select it, and then click on the Add
button to open all existing items for this website project. Select both items, Course.aspx
and Course.aspx.cs , from the list and click on the Add button to add these two items
into our new website project.
9.7.2 Add Web Service Reference and Modify
Web Form Window
To add a Web reference of our Web Service to this new Website project, right - click
on our new project icon from the Solution Explorer window and select the item
Add Web Reference from the pop - up menu. Now open our Web Service project
WebServiceSQLUpdateDelete and click the Start Debugging button to run it. As the
project runs, copy the URL from the Address box and paste it into the URL box in our

Add Web Reference dialog box. Then click on the green Go button to search and add
this Web Service as a reference to our client project. You can modify this Web reference
name to any name you want. In this application, we prefer to change it to
WS_
SQLUpdateDelete
. Your fi nished Add Web Reference dialog box should match the
one shown in Figure 9.93 .
Click on the Add Reference button to fi nish this adding Web reference process.
Immediately you can fi nd that the following three fi les are created in the Solution
Explorer window under the folder App_WebReferences :
• WebServiceSQLUpdateDelete.disco
• WebServiceSQLUpdateDelete.discomap
• WebServiceSQLUpdateDelete.wsdl
c09.indd 828c09.indd 828 2/11/2010 3:02:11 PM2/11/2010 3:02:11 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.7 Build Web-Based Web Service Clients to Use Web Services

829
The only modifi cation to the Web page of the Course.aspx is the DropDownList
box control ComboMethod . Because we only use one method to perform the data updat-
ing and deleting action in our Web Service project, we do not need the Method combobox
control in this application. We can remove this control from the graphic user interface.
However, it does not matter if we keep it without using it in our Web client project. Our
fi nished graphic user interface is shown in Figure 9.94 .
Now let ’ s take care of modifi cations to the codes in related user - defi ned methods in
the Course.aspx page.
9.7.3 Modify Codes for Related Methods
Before we can perform any code modifi cation, fi rst we need to perform the following
operations to delete some unused methods in our new project:

1. Remove the Insert button ’ s Click method cmdInsert_Click() since we do not need any data
insertion action in this application.
2. Remove the user - defi ned FillCourseDataSet() method since no DataSet method will be
used in this application.
3. Remove the TextChanged event method of the Course ID textbox since we do not need
this event and its event method in this application.
The fi rst code modifi cation is to change the codes in the Page_Load() method and
some global variables.

Figure 9.93
Finished Add Web Reference dialog box.
c09.indd 829c09.indd 829 2/11/2010 3:02:11 PM2/11/2010 3:02:11 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
830
Chapter 9 ASP.NET Web Services
9.7.3.1 Modify Codes in Page_Load Method
Perform the following changes to complete this modifi cation:
1. Remove the fi eld - level variables dsFlag and wsDataSet .
2. Change the name of the base class for the fi eld - level instance wsSQLResult from WS_
SQLInsert.SQLInsertBase to WS_SQLUpdateDelete.SQLBase .
3. In the Page_Load() method, remove the code that is used to add and display the second
Web method, DataSet Method , from the ComboMethod control.
Your modifi ed codes for the Page_Load() method should match that shown in Figure
9.95 . The modifi ed codes have been highlighted in bold. The next step is to develop the
codes for the Update button ’ s Click method.
9.7.3.2 Develop Codes for Update Button ’s Click Method
The function of this method is: When a faculty name is selected and all six pieces of
updating course information are entered in the six textbox controls, the updating course
information will be passed to the Web method SQLUpdateSP() we developed in our
Web Service project and a stored procedure WebUpdateCourseSP() is executed to

perform this course updating action as the Update button is clicked on by the user. Now
let ’ s double - click on the Update button to open its Click method, and enter the codes
shown in Figure 9.96 into this method.
Let ’ s take a closer look at this piece of code to see how it works.
A. A new instance of our Web proxy class, wsSQLUpdate, is created, and this instance is used
to access the Web method SQLUpdateSP() we developed in our Web Service class
Figure 9.94
Modifi ed graphic user interface.
c09.indd 830c09.indd 830 2/11/2010 3:02:12 PM2/11/2010 3:02:12 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.7 Build Web-Based Web Service Clients to Use Web Services

831
public partial class Course : System.Web.UI.Page
{
WS_SQLUpdateDelete.SQLBase wsSQLResult = new WS_SQLUpdateDelete.SQLBase();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //these items can only be added into the combo box in one time
{
ComboName.Items.Add("Ying Bai");
ComboName.Items.Add("Satish Bhalla");
ComboName.Items.Add("Black Anderson");
ComboName.Items.Add("Steve Johnson");
ComboName.Items.Add("Jenney King");
ComboName.Items.Add("Alice Brown");
ComboName.Items.Add("Debby Angles");
ComboName.Items.Add("Jeff Henry");
ComboMethod.Items.Add("Stored Procedure Method");

}
}
Course Page_Load()

Figure 9.95
Modifi ed Page_Load method.
protected void cmdUpdate_Click(object sender, EventArgs e)
{
WS_SQLUpdateDelete.WebServiceSQLUpdateDelete wsSQLUpdate = new
WS_SQLUpdateDelete.WebServiceSQLUpdateDelete();
string errMsg;
try
{
wsSQLResult = wsSQLUpdate.SQLUpdateSP(ComboName.Text, txtCourseID.Text, txtCourseName.Text,
txtSchedule.Text, txtClassRoom.Text, Convert.ToInt32(txtCredits.Text), Convert.ToInt32(txtEnroll.Text));
}
catch (Exception err)
{
errMsg = "Web service is wrong: " + err.Message;
Response.Write("<script>alert('" + errMsg + "')</script>");
}
if (wsSQLResult.SQLOK == false)
Response.Write("<script>alert('" + wsSQLResult.SQLError + "')</script>");
}
A
B
C
D
E
Course cmdUpdate_Click()


Figure 9.96
Codes for the Update button Click method.
WebServiceSQLUpdateDelete to perform the data updating action against our sample
database.
B. A local string variable errMsg is also created, and it is used to reserve the error source
that will be displayed as a part of an error message later.
C. A
try

catch
block is used to call the Web method SQLUpdateSP() with six pieces of
course updating data to execute a stored procedure WebUpdateCourseSP() to perform
this course updating action.
D. An error message will be displayed if any error is encountered during that data
updating action. Note the displaying format of this error message. To display a string
variable in a message box in the client side, one must use the Java script function
c09.indd 831c09.indd 831 2/11/2010 3:02:13 PM2/11/2010 3:02:13 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
832

Chapter 9 ASP.NET Web Services
alert() with the input string variable as an argument that is enclosed and represented
by ' " + input_string + " ' .
E. Besides the system error checking, we also need to check the member data SQLOK that
is defi ned in our base class in the Web Service project to make sure that this data updating
is application error free. A returned
false
value of this member data indicates that this
data updating encountered some application error, and the error source stored in another

member data SQLError is displayed using the Java script function alert() .
In a similar way, we can develop the codes for the Delete button ’ s Click method to
perform the data deleting actions against our sample database.
9.7.3.3 Develop Codes for Delete Button ’ s Click Method
The function of this method is: When a
course_id
has been selected either from
the listbox control or from the Course ID textbox control in this client page window,
the selected course with a primary key that equals to that
course_id
will be deleted
from all tables, including the child and parent tables, from our sample relational
database.
Double - click on the Delete button from our client page window to open the Delete
Click method, and enter the codes shown in Figure 9.97 into this method.
Let ’ s take a closer look at this piece of code to see how it works.
A. A new instance of our Web proxy class, wsSQLDelete , is created, and this instance is used
to access the Web method SQLDeleteSP() we developed in our Web Service class
WebServiceSQLUpdateDelete to perform the data deleting action in our sample
database.
B. A local string variable errMsg is also created, and it is used to reserve the error source
that will be displayed as a part of an error message later.
C. A
try

catch
block is used to call the Web method SQLDeleteSP() with one piece of
course information,
course_id
, that works as an identifi er to run a stored procedure

WebDeleteCourseSP() to perform this course deleting action in our sample database.
protected void cmdDelete_Click(object sender, EventArgs e)
{
WS_SQLUpdateDelete.WebServiceSQLUpdateDelete wsSQLDelete = new
WS_SQLUpdateDelete.WebServiceSQLUpdateDelete();
string errMsg;
try
{
wsSQLResult = wsSQLDelete.SQLDeleteSP(txtCourseID.Text);
}
catch (Exception err)
{
errMsg = "Web service is wrong: " + err.Message;
Response.Write("<script>alert('" + errMsg + "')</script>");
}
if (wsSQLResult.SQLOK == false)
Response.Write("<script>alert('" + wsSQLResult.SQLError + "')</script>");
}
A
B
C
D
Course cmdDelete_Click()
E

Figure 9.97
Codes for the Delete button Click method.
c09.indd 832c09.indd 832 2/11/2010 3:02:13 PM2/11/2010 3:02:13 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.


9.7 Build Web-Based Web Service Clients to Use Web Services

833
D. An error message will be displayed if any error is encountered during that data deleting
action. Note the displaying format of this error message. To displayed a string variable
in a message box in the client side, one must use the Java script function alert() with
the input string variable as an argument that is enclosed and represented by
' " + input_string + " ' .
E. Besides the system error checking, we also need to check the member data SQLOK that
is defi ned in our base class in the Web Service project to make sure that this data deleting
is application error free. A returned
false
value of this member data indicates that this
data deleting encountered some application error and the error source stored in another
member data SQLError is displayed.
Go to the File|Save All menu item to save these modifi cations and developments.
Next let ’ s perform the code modifi cation to the Select button ’ s Click method.
9.7.3.4 Modify Codes in Select Button ’ s Click Method and Related Methods
The function of this method is: Either after a data updating or deleting action is per-
formed, we need to confi rm this operation by retrieving the related courses taught by the
selected faculty from our sample database. To do that, a desired faculty should be selected
from the Faculty Name combobox control, and the Select button should be clicked by
the user. Then this method will call the Web method GetSQLCourse() we developed in
our Web Service project, and an instance that contains all retrieved courses taught by the
selected faculty is returned from that Web method. Some user - defi ned methods are
executed to extract those courses from the returned instance and display them in the
listbox control in our client page window. Open this method and perform the modifi ca-
tions shown in Figure 9.98 to this method:
Let ’ s take a closer look at this piece of code to see how it works.
A. Rename the new instance ’ s name to

wsSQLSelect
and change the Web proxy class ’ s
name to WS_SQLUpdateDelete.WebServiceSQLUpdateDelete .
B. Add one more local string variable errMsg that will be used to store the error source later.
Remove the
if

else

end if
block for the method checking process since we have
protected void cmdSelect_Click(object sender, EventArgs e)
{
WS_SQLUpdateDelete.WebServiceSQLUpdateDelete wsSQLSelect = new
WS_SQLUpdateDelete.WebServiceSQLUpdateDelete();
string errMsg;
try { wsSQLResult = wsSQLSelect.GetSQLCourse(ComboName.Text); }
catch (Exception err)
{
errMsg = "Web service is wrong: " + err.Message;
Response.Write("<script>alert('" + errMsg + "')</script>");
}
if (wsSQLResult.SQLOK == false)
Response.Write("<script>alert('" + wsSQLResult.SQLError + "')</script>");
ProcessObject(ref wsSQLResult);
}
A
B
C
D

E
F
Course
cmdSelect_Click()

Figure 9.98
Modifi ed codes for the Select button Click method.
c09.indd 833c09.indd 833 2/11/2010 3:02:14 PM2/11/2010 3:02:14 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
834

Chapter 9 ASP.NET Web Services
only one method, stored procedure method, used in this application. Also remove all codes
between the
else
and
end if
half - block since we do not have the DataSet method used
in this project.
C. Change the instance name of our Web proxy class from wsSQLInsert to wsSQLSelect
and Web method ’ s name from GetSQLInsert() to GetSQLCourse() .
D. Change the name of the member data from SQLInsertOK to SQLOK .
E. Change the name of another member data from SQLInsertError to SQLError .
F. Remove the last two methods, FillCourseDataSet() and Application["dsFlag"] =
false , since we do not need these two operations in this application.
All modifi cation parts have been highlighted in bold.
Two user - defi ned methods are related to this Select button ’ s Click method, and they
are ProcessObject() and FillCourseListBox(). The modifi cations to these two methods
include the following steps:
A. Change the data type of passed argument

wsResult
from WS_SQLInsert. SQLInsertBase
to WS_SQLUpdateDelete.SQLBase .
B. Change the
if
block condition variable from SQLInsertOK to SQLOK .
C. Change the error message member data from SQLInsertError to SQLError .
D. Change the data type of passed argument
wsResult
from WS_SQLInsert. SQLInsertBase
to WS_SQLUpdateDelete.SQLBase .
Two modifi ed methods are shown in Figure 9.99 and the modifi ed parts have been
highlighted in bold.
Go to the File|Save All menu item to save these modifi cations. Next we will perform
the modifi cations to the last method SelectedIndexChanged, which is an event method
of the course listbox control CourseList in our page window.
private void ProcessObject(ref WS_SQLUpdateDelete.SQLBase wsResult)
{
string errMsg;
if (wsResult.SQLOK == true)
FillCourseListBox(ref wsResult);
else
{
errMsg = "Course information cannot be retrieved: " + wsResult.SQLError;
Response.Write("<script>alert('" + errMsg + "')</script>");
}
}
private void FillCourseListBox(ref WS_SQLUpdateDelete.SQLBase sqlResult)
{
int index = 0;

CourseList.Items.Clear(); //clean up the course listbox
for (index = 0; index <= sqlResult.CourseID.Length - 1; index++)
{
CourseList.Items.Add(sqlResult.CourseID[index]);
}
}
A
B
C
D
Course ProcessObject()

Figure 9.99
Modifi ed methods ProcessObject and FillCourseListBox.
c09.indd 834c09.indd 834 2/11/2010 3:02:14 PM2/11/2010 3:02:14 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.7 Build Web-Based Web Service Clients to Use Web Services

835
9.7.3.5 Modify Codes in Selected I ndex C hanged Method
Open the SelectedIndexChanged method of the listbox control CourseList and perform
the modifi cations shown in Figure 9.100 to this method. Let ’ s take a closer look at these
modifi cations.
A. Rename the new instance ’ s name to
wsSQLSelect
and change the Web proxy class ’ s
name to WS_SQLUpdateDelete.WebServiceSQLUpdateDelete .
B. Change the instance name of our Web proxy class from wsSQLInsert to wsSQLSelect
and Web method ’ s name from GetSQLInsert to GetSQLCourseDetail .

C. Change the name of the member data from SQLInsertOK to SQLOK .
D. Change the name of the member data from SQLInsertError to SQLError .
The modifi cation to the related user - defi ned method FillCourseDetail() is to change
the data type of the passed argument from WS_SQLInsert.SQLInsertBase to WS_
SQLUpdateDelete.SQLBase .
At this point, we have fi nished all modifi cations to this Web - based client project,
and now it is time for us to run this project to access our Web Service to perform the
data updating and deleting actions. However, before we can run this project, make
sure that our Web Service project WebServiceSQLUpdateDelete is in the open
status. This can be identifi ed by a small white icon located in the status bar at the
bottom of the screen. If you cannot fi nd this icon, open our Web Service project
WebServiceSQLUpdateDelete and click on the Start Debugging button to run it. As
long as our Web Service runs once, it can be closed by clicking on the Close button to
terminate it, but the small white icon should be in there, which means that our Web
Service is open and ready to be accessed.
Now click on the Build|Build Web Site menu item to build our project. You may
encounter some compiling errors: One is the txtCourseID_TextChanged defi nition and
the other one is the cmdInsert_Click defi nition. Both errors are located at the Course.
aspx page since we have deleted those two methods. Open that page and remove those
defi nitions from the Course.aspx page. Rebuild project and click on the Start Debugging
button from our client project to run it. First, let ’ s test the data updating function by
protected void CourseList_SelectedIndexChanged(object sender, EventArgs e)
{
WS_SQLUpdateDelete.WebServiceSQLUpdateDelete wsSQLSelect = new
WS_SQLUpdateDelete.WebServiceSQLUpdateDelete();
string errMsg;
try { wsSQLResult = wsSQLSelect.GetSQLCourseDetail(CourseList.Text); }
catch (Exception err) { errMsg = "Web service is wrong: " + err.Message;
Response.Write("<script>alert('" + errMsg + "')</script>"); }
if (wsSQLResult.SQLOK == false)

Response.Write("<script>alert('" + wsSQLResult.SQLError + "')</script>");
FillCourseDetail(ref wsSQLResult);
}
A
B
C
D
Course CourseList_SelectedIndexChanged()

Figure 9.100
Modifi ed codes for the SelectedIndexChanged method.
c09.indd 835c09.indd 835 2/11/2010 3:02:14 PM2/11/2010 3:02:14 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
836
Chapter 9 ASP.NET Web Services
updating a course record CSE - 665. However, before we can do that, we prefer to retrieve
the current information for the course CSE - 665. Click on the Select button to get all
courses currently taught by the selected faculty member
Ying Bai
, and all
course_id
will be retrieved and displayed in the listbox control. Click on the course CSE - 665 from
the listbox control to get the detailed information for this course. Immediately the
detailed information related to course CSE - 665 is displayed in the associated textbox
control, which is shown in Figure 9.101 .
Now enter the following updating information for the course CSE - 665 into the associ-
ated textbox, which is shown in Figure 9.102 .
Now click on the Update button to call the Web method SQLUpdateSP() in our Web
service project to update this course record. To check whether the course CSE - 665 has
been updated or not, fi rst select another course from the listbox, such as CSC - 234A, and

then click on the course CSE - 665 from the listbox control. Immediately the detailed
information about this updated course is displayed in the associated textbox, as shown
in Figure 9.103 . It can be found that this course has been updated successfully with our
updating information.
To test the deleting function, keep the course CSE - 665 selected from the listbox, click
on the Delete button to delete this record from the Course table. To confi rm this course
deleting action, click on the Select button to try to retrieve all courses taught by the
selected faculty. Immediately all courses are returned and displayed in the listbox control.
It can be found that the course CSE - 665 has been removed from the Course table and
you cannot fi nd it from the listbox now.
Click on the Back button to terminate our client project. Our client project is very
successful.
Figure 9.101
Detailed information of the course CSE - 665.
c09.indd 836c09.indd 836 2/11/2010 3:02:14 PM2/11/2010 3:02:14 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.7 Build Web-Based Web Service Clients to Use Web Services

837

Figure 9.102
Updating information for the course CSE - 665.

Figure 9.103
Updated course CSE - 665.
c09.indd 837c09.indd 837 2/11/2010 3:02:15 PM2/11/2010 3:02:15 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
838


Chapter 9 ASP.NET Web Services
It is highly recommended to recover the deleted course CSE - 665 for our Course table
since we want to keep our database neat and complete. You can recover this data by
using one of the following fi ve methods:
1. Use the Server Explorer window in Visual Studio.NET to open our sample database
CSE_DEPT.mdf and our Course data table.
2. Use the Microsoft SQL Server Management Studio or Studio Express to open our sample
database CSE_DEPT.mdf and our Course data table.
3. Use our Web Service project WebServiceSQLInsert to insert a new course to perform this
course recovering.
4. Use our Windows - based Web Service client project WinClientSQLInsert to perform this
course recovering.
5. Use our Web - based Web Service client project WebClientSQLInsert to insert a new course
to recover this course record.
Relatively speaking, using the last three methods to recover this course information
is professional since normally no one wants to access and change the content of a database
directly by opening the database to do modifi cations.
Refer to Table 9.7 to recover the deleted course CSE-665.
A complete Web - based Web Service client project WebClientSQLUpdateDelete can
be found at the folder DBProjects\Chapter 9 at the accompanying ftp site (see Chapter 1 ).
At this point, we have fi nished the discussion about how to access and manipulate
data in the SQL Server database via ASP.NET Web Services. In the next section, we will
discuss how to access and manipulate data in the Oracle database via ASP.NET Web
Services.
9.8 BUILD ASP . NET WEB SERVICE PROJECT TO ACCESS
ORACLE DATABASE
Basically, the procedure to build an ASP.NET Web Service to access the Oracle database
is very similar to the procedure to build an ASP.NET Web Service to access the SQL
Server database. The main differences are:
1. The connection string defi ned in the Web confi guration fi le web.confi g

2. The namespace directories listed at the top of each Web Service page
3. The stored procedures used by each Web Service page
Table 9.7 The Recovered Record for the Course CSE - 665

Column Name Column Value
course_id CSE-665
course Neural Network Systems
credit 3
classroom TC-315
schedule T-H: 1:00-2:25 PM
enrollment 26
faculty_id B78880

c09.indd 838c09.indd 838 2/11/2010 3:02:19 PM2/11/2010 3:02:19 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.8 Build ASP.NET Web Service Project to Access Oracle Database

839
4. The protocol of the data query string used by each Web Service page
5. The nominal names of dynamic parameters for the Parameters collection object
These fi ve distinct points exist between the procedures to build a Web Service to
access two kinds of database. Let ’ s give a little more detailed discussion for these issues.
First, when connecting to a different database, the connection string is obviously
different, which includes the protocol and security issues in that connection string. Refer
to Section 5.19.1 in Chapter 5 to get a clear picture for the difference that exists in the
connection strings between these two kinds of databases.
Second, as we know, ADO.NET provides different Data Providers to support users
to access the different databases, and these Data Providers are database dependent,
which means that different Data Providers are needed to access the different databases.

For the Oracle database, ADO.NET provides the namespace System.Data.OracleClient
that contains all necessary data components to access and manipulate data stored in an
Oracle database. In other words, to use matched data components provided by the ADO.
NET to access an Oracle database, one must use the associated namespace to access those
data components.
Third, the prototype and structure of a stored procedure are different for the differ-
ent databases. To call a stored procedure to perform a data action against an SQL Server
database is totally different from calling a stored procedure to perform the similar data
action against an Oracle database.
For differences 4 and 5 listed above, it is clear that the format of a query string is
different when calling the different databases. Also the nominal name of the dynamic
parameter is distinguished when it is used for the different databases.
Based on the above discussion and analysis as well as the similarity between the
SQL Server and Oracle databases, we try to develop our Web Service projects to access
the Oracle database by modifying some existing Web Service projects in the
following sections. In this section, we concentrate on the modifi cations to the Web Service
project WebServiceSQLSelect and make it our new Web Service project
WebServiceOracleSelect.
9.8.1 Build Web Service Project Web S ervice O racle S elect
In this section, we try to modify an existing Web Service project WebServiceSQLSelect
to make it our new Web Service project WebServiceOracleSelect, and allow it to access
the Oracle database to perform the data selection queries.
Open Windows Explorer and create a new folder Chapter 9 under the root directory
if you have not done that, and then open Internet Explorer and browse to our desired
source Web Service project WebServiceSQLSelect located at the folder DBProjects\
Chapter 9 at the accompanying ftp site (see Chapter 1 ). Copy and paste this project into
our new folder Chapter 9. Rename it WebServiceOracleSelect. Perform the following
modifi cations to this project:
1. Change the main Web Service page from WebServiceSQLSelect.asmx to
WebServiceOracleSelect.asmx .

2. Open the App_Code folder and change the name of our base class fi le from SQLSelectBase.
cs to OracleSelectBase.cs .
c09.indd 839c09.indd 839 2/11/2010 3:02:19 PM2/11/2010 3:02:19 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
840
Chapter 9 ASP.NET Web Services
3. Open the App_Code folder and change the name of our derived class fi le from
SQLSelectResult.cs to OracleSelectResult.cs .
4. Open the App_Code folder and change the name of our code - behind page from
WebServiceSQLSelect.cs to WebServiceOracleSelect.cs .
Now open Visual Studio.NET 2008 and our new Web Service project
WebServiceOracleSelect to perform the associated modifi cations to the contents of the
fi les we renamed above. First, let ’ s perform the modifi cations to our main Web Service
page WebServiceOracleSelect.asmx . Open this page by double - clicking on it from the
Solution Explorer window and perform the following modifi cations:
• Change
CodeBehind= " ~ /App_Code/WebServiceSQLSelect.cs "
to
CodeBehind= " ~ /App_Code/WebServiceOracleSelect.cs "
• Change
Class=" WebServiceSQLSelect "
to
Class= " WebServiceOracleSelect "
Second, open the base class fi le OracleSelectBase.cs and perform the following
modifi cations:
• Change the class name and the constructor ’ s name from SQLSelectBase to OracleSelectBase .
• Change the name of the fi rst member data from SQLRequestOK to OracleRequestOK .
• Change the name of the second member data from SQLRequestError to OracleRequestError .
Next open the derived class fi le OracleSelectResult.cs by double - clicking on it from
the Solution Explorer window, and perform the following modifi cations:

• Change the class name and the constructor ’ s name from SQLSelectResult to
OracleSelectResult .
• Change the base class name (after the resolution operator : ) from SQLSelectBase to
OracleSelectBase .
9.8.2 Modify Connection String
Double - click on our Web confi guration fi le web.confi g from the Solution Explorer
window to open it. Change the content of the connection string that is under the tag
< connectionStrings > to:
< add name= " ora_conn " connectionString= " Server=XE;User
ID=CSE_DEPT;Password=reback; " / >
The Oracle database server XE is used for the server name, the User ID is our sample
database CSE_DEPT and the Password is determined by the user when adding a new
account to create a new user database.
9.8.3 Modify Namespace Directories
First, we need to add an Oracle Data Provider reference to our Web Service project. To
do that, right - click on our new project icon WebServiceOracleSelect from the Solution
Explorer window, and then select the item Add Reference from the pop - up menu to
c09.indd 840c09.indd 840 2/11/2010 3:02:19 PM2/11/2010 3:02:19 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.8 Build ASP.NET Web Service Project to Access Oracle Database

841
open the Add Reference dialog box. Browse down along the list until you fi nd the item
System.Data.OracleClient , click to select it and then click on the OK button to add it
into our project.
Now double - click on our code - behind page WebServiceOracleSelect.cs to open it.
On the opened page, add one more namespace line to the top of this page:
using System.Data.OracleClient;
Also change the name of our Web Service class, which is located after the accessing

mode public class , and the constructor of this class from WebServiceSQLSelect to
WebServiceOracleSelect .
Next we will perform the necessary modifi cations to three Web methods combined
with those fi ve differences listed above.
9.8.4 Modify Web Method Get SQLS elect and Related Methods
The following issues are related to this modifi cation:
1. The name of this Web method and the name of the returned data type class
2. The query string used in this Web method
3. The names of the data components used in this Web method
4. The user - defi ned SQLConn() and ReportError() methods
5. The name of the dynamic parameter
Let ’ s perform those modifi cations step by step according to this sequence.
Open the Web method GetSQLSelect() and perform the modifi cations shown in
Figure 9.104 to this method.
A. Rename this Web method to GetOracleSelect and the name of the returned class to
OracleSelectResult.
B. Change the prefi x of all data classes from Sql to Oracle and the prefi x of all data objects
from sql to ora , respectively.
C. Modify the query string by replacing the LIKE @ symbol before the dynamic parameter

facultyName
with the symbol =: , which is an assignment operator used in the Oracle
database.
D. Change the returned instance name from SQLResult to OracleResult and the member
data from SQLRequestOK to OracleRequestOK .
E. Change the name of the user - defi ned method from SQLConn() to OracleConn() .
F. Change the prefi x from sql to ora for all data objects.
G. Modify the nominal name of the dynamic parameter by removing the
@
symbol before the

nominal name
facultyName
. Also change its data type from SqlDbType.Text to
OracleType.VarChar .
H. Change the name of the returned instance from SQLResult to OracleResult and Change
the prefi x from sql to ora for all data objects.
Your modifi ed Web method GetOracleSelect() should match that shown in Figure
9.104 . All modifi ed parts have been highlighted in bold.
c09.indd 841c09.indd 841 2/11/2010 3:02:20 PM2/11/2010 3:02:20 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
842

Chapter 9 ASP.NET Web Services
Next let ’ s modify three user - defi ned methods: SQLConn(), FillFacultyReader(), and
ReportError(), which are related to the GetOracleSelect() method. Open these methods
and perform the modifi cations shown in Figure 9.105 .
Let ’ s talk a closer look at these modifi cations to see how they work:
A. Change the name of this method from SQLConn() to OracleConn() and the returning
class name from SqlConnection to OracleConnection . Also change the connection string
from
sql_conn
to
ora_conn
.
B. Change the data type of the returned connection object to OracleConnection .
C. Change the data type of the fi rst passed argument from SQLSelectResult to
OracleSelectResult . Also change the data type of the second passed argument from
SqlDataReader to OracleDataReader .
D. Change the data type of the passed argument from SQLSelectResult to OracleSelectResult .
E. Change the name of the fi rst member data from SQLRequestOK to OracleRequestOK .

F. Change the name of the second member data from SQLRequestError to
OracleRequestError .
[WebMethod]
public OracleSelectResult GetOracleSelect(string FacultyName)
{
OracleConnection oraConnection = new OracleConnection();
OracleSelectResult OracleResult = new OracleSelectResult();
OracleCommand oraCommand = new OracleCommand();
OracleDataReader oraReader;
string cmdString = "SELECT faculty_id, office, phone, college, title, email FROM Faculty " +
"WHERE faculty_name =: facultyName";
OracleResult.OracleRequestOK = true;
oraConnection = OracleConn();
if (oraConnection == null)
{
OracleResult.OracleRequestError = "Database connection is failed";
ReportError(OracleResult);
return null;
}
oraCommand.Connection = oraConnection;
oraCommand.CommandType = CommandType.Text;
oraCommand.CommandText = cmdString;
oraCommand.Parameters.Add("facultyName", OracleType.VarChar).Value = FacultyName;

oraReader = oraCommand.ExecuteReader();
if (oraReader.HasRows == true)
FillFacultyReader(ref OracleResult, oraReader);
else
{
OracleResult.OracleRequestError = "No matched faculty found";

ReportError(OracleResult);
}
oraReader.Close();
oraConnection.Close();
oraCommand.Dispose();
return OracleResult;
}
A
B
C
D
E
F
G
H
WebServiceOracleSelect
GetOracleSelect()

Figure 9.104
Modifi ed Web method GetOracleSelect.
c09.indd 842c09.indd 842 2/11/2010 3:02:20 PM2/11/2010 3:02:20 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×