手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

Creating a Server Component with VB - Redesigned - Part 2

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

GetRows() Error Code

The error code that we''''ll add to our new database code using the GetRows() method will look very similar to the error code that was discussed above.

Adding Error Handling Code With GetRows()


''''///// Check if any data was returned
    If Rs.EOF = True And Rs.BOF = True Then
        ''''///// No data returned, set error information, raise error
        Err.Number = 40000
        Err.Source = "NoteProject::NoteClass::ShowNotes"
        Err.Description = "00000001<BR>NoteID and NoteTitle database select statement failure<BR>" & sql
        Err.Raise Err.Number, Err.Source, Err.Description
    Else
        ''''///// Data returned - NoteID and NoteTitle go into vRecordArray
        vRecordArray = Rs.GetRows
    End If

We basically assign a different error number (40000) and a different error description to the error code. If the SQL statement fails to return a record, this error message will be the one that''''s returned.

Presenting with Style

Since our method is designed for use from ASP files, we''''ll want to return a string that is browser ready. The details of the database table (NoteStyleTable), which contains the fields used as the HTML tag values, were covered earlier in "The Database Tables" section.

The string that''''s returned from the ShowNotes() method will contain two HTML TABLE data records, one containing the note titles and the other note text. So we''''ll need to enclose the method call within HTML TABLE record tags within our ASP file. You can design the style table to include fields for the TABLE and TR tags, thus returning an entire table rather than just the record data code. But I found that changes to the TABLE and TR tags are much more frequent and having them in the ASP file makes changes quicker to get to. You can always sneak the TABLE and TR tags within the database table structure that''''s described here, if you decide you want an entire HTML table returned rather that just the HTML table record data.

ASP file code that places the method in HTML TABLE record tags


<TABLE>
<TR>
	''''--> Method call goes here
</TR>
</TABLE>

We need to write method code that will supply the HTML values that are used to format our database information and which will be inserted in the ASP file at the method call''''s location. The eight variables for the HTML formatting will either be assigned default values within the method or retrieved from the database. When the intStyleID method parameter is set to zero, the default style values will be used. If the value of intStyleID is above zero, it will be used to retrieve the correct style database record values.

For this section of example code, the process of assigning the default values and, alternatively, retrieving the HTML values from the database will be demonstrated. For now we''''ll leave out the code that supplies our note database information and just return a method string that contains the HTML formatting of the style variable values. Once this section is covered, we''''ll use the HTML formatting values to demonstrate how to construct the actual HTML return string that contains the database note information.

The style code structure is basic If/Then code. Here''''s the gist of it.


If intStyleID = 0 Then

	''''--> ASSIGN DEFAULT VAULES TO THE HTML STYLE VARIABLES
Else

	''''--> OPEN A DATABASE RECORDSET 
	''''--> RETRIEVE VALUES FROM DATABASE AND ASSIGN THEM TO THE HTML STYLE VARIABLES
	''''--> CLOSE THE DATABASE RECORDSET

End If

Here''''s the code that fills in this structure.

Code for Using Presentation Styles
Public Function ShowNotes(ByVal lngNoteID As Long, _
	Optional ByVal intStyleID As Integer = 0, _
	Optional ByVal strURL As String = "NoteExample.asp", _
	Optional ByVal strDbConnectionString As String = "NotesDSN") As String


    ''''///// DIM AND INIT ////////////////////////////////////////////
    ''''///// DB Connectivity Variables
    Dim sql As String
    Dim Rs As New ADODB.Recordset
    Dim objCmd As New ADODB.Command
    Dim objConn As New ADODB.Connection
    
    ''''///// Variable to hold constructed text
    Dim strReturnString As String
    
    ''''///// Note Style Properties
    Dim strTitleDataStart As String
    Dim strTitleFontStart As String
    Dim strTitleFontEnd As String
    Dim strTitleDataEnd As String
    Dim strTextDataStart As String
    Dim strTextFontStart As String
    Dim strTextFontEnd As String
    Dim strTextDataEnd As String


    ''''///// OPEN DATABASE ///////////////////////////////////////////
    objConn.Open strDbConnectionString
    objConn.BeginTrans


    ''''--> ANY OTHER DATABASE CODE GOES HERE


    ''''///// SET NOTE STYLE PROPERTIES ///////////////////////////////
    If intStyleID = 0 Then
    
        ''''///// SET NOTE STYLE TO DEFAULT VALUES
        ''''///// Note Title Default Style Values
        strTitleDataStart = "<TD WIDTH=""74"" NOWRAP VALIGN=""top"">"
        strTitleFontStart = "<FONT FACE=""Verdana"" SIZE=""1"" COLOR=""GRAY"">"
        strTitleFontEnd = "</FONT>"
        strTitleDataEnd = "</TD>"
        
        ''''///// Note Text Default Style Values
        strTextDataStart = "<TD WIDTH=""124"" VALIGN=""top"" BGCOLOR=""#EFEFEF"">"
        strTextFontStart = "<FONT FACE=""Verdana"" SIZE=""1"" COLOR=""NAVY"">"
        strTextFontEnd = "</FONT>"
        strTextDataEnd = "</TD>"
    
    Else
    
        ''''///// SET NOTE STYLE FROM VALUES IN DATABASE
        sql = "SELECT * FROM NoteStyleTable WHERE intStyleID = " & intStyleID
        objCmd.CommandText = sql
        objCmd.CommandType = adCmdText
        Set objCmd.ActiveConnection = objConn
        Rs.Open objCmd
    
        ''''///// Note title database style values
        strTitleDataStart = "" & Rs.Fields("strTitleDataStart")
        strTitleFontStart = "" & Rs.Fields("strTitleFontStart")
        strTitleFontEnd = "" & Rs.Fields("strTitleFontEnd")
        strTitleDataEnd = "" & Rs.Fields("strTitleDataEnd")
            
        ''''///// Note text database style values
        strTextDataStart = "" & Rs.Fields("strTextDataStart")
        strTextFontStart = "" & Rs.Fields("strTextFontStart")
        strTextFontEnd = "" & Rs.Fields("strTextFontEnd")
        strTextDataEnd = "" & Rs.Fields("strTextDataEnd")

        ''''///// Close this Recordset        
        Rs.Close

    End If


    ''''///// FINISH AND CLOSE UP DATABASE PROCESSES //////////////////
    objConn.CommitTrans
    objConn.Close
    Set Rs = Nothing
    Set objCmd = Nothing
    Set objConn = Nothing


    ''''--> OTHER METHOD CODE GOES HERE


    ''''///// TEMPARY RETURN STRING EXAMPLE
    strReturnString = strTextDataStart
    strReturnString  = strReturnString  & strTextFontStart
    strReturnString  = strReturnString  & "Test"
    strReturnString  = strReturnString  & strTextFontEnd
    strReturnString  = strReturnString  & strTextDataEnd

    strReturnString  = strReturnString  & strTitleDataStart
    strReturnString  = strReturnString  & strTitleFontStart
    strReturnString  = strReturnString  & "Another Test"
    strReturnString  = strReturnString  & strTitleFontEnd
    strReturnString  = strReturnString  & strTitleDataEnd

    ''''///// SEND BACK CONSTRUCTED STRING /////////////////////////////
    ShowNotes = strReturnString

End Function

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!