News

Tuesday, June 19, 2007

Developing Microsoft® .NET Controls with Microsoft Visual Basic® .NET

Customize the intrinsic controls in the .NET Framework—or build your own—for better usability
 
Every click on a menu item or a dialog box—every interaction with a control—shapes the user's experience and satisfaction with your software. Learn how to maximize the usability and impact of your Visual Basic .NET–based solutions by using the powerful intrinsic controls in the .NET Framework right out of the box—or by building complex controls from scratch. Expert developer and teacher John Connell shares a wealth of code samples and techniques to illustrate how .NET controls operate as well as how to integrate even the most complex controls into Windows Forms and Microsoft ASP.NET Web Forms. Whether you're developing in-house or commercial software, you'll learn how to build custom controls and other interface components that make your programs easy—and enjoyable—to use.

- Discover how to use encryption technologies, isolated storage, and serialization in the .NET Framework in your own programs and controls
- Build custom .NET type editors, type converters, and designers to add and manage sophisticated properties in your custom controls
- Learn how to inherit and enhance .NET controls—from handling events to building custom designers
- Aggregate multiple .NET intrinsic controls into one user control for simpler, custom functionality
- Use GDI+ to perform hit tests, create dynamic bitmaps, and add graphics to Windows Forms and ASP.NET controls
- Add licensing to your controls, including custom validation of authorized users
- Manipulate the built-in ASP.NET Web server controls, from basic HTML wrappers to the powerful DataGrid control
- Design ASP.NET custom server controls to create Web page hit counters, wireless applications, and more

Wednesday, June 13, 2007

HOW TO: Validate XML Fragments Against an XML Schema in Visual Basic .NET

SUMMARY

This step-by-step article describes how to use XmlValidatingReader and XMLSchemaCollection objects to validate an Extensible Markup Language (XML) fragment against an XML schema.

XmlValidatingReader implements the XmlReader class and provides support for XML data validation. The Schemas property of XmlValidatingReader connects the reader to the schema files cached in an XmlSchemaCollection. The ValidationType property of XmlValidatingReader specifies the type of validation the reader should perform. If you set the property to ValidationType.None, you create a nonvalidating reader.

You can only add XML Schema Definition Language (XSD) schemas and XML-Data Reduced (XDR) schemas to XmlSchemaCollection. Use the Add method with a namespace URI to load schemas. For XML schemas, the typical namespace URI is the targetNamespace property of the schema.
 

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
Microsoft Visual Studio .NET installed on a compatible Microsoft Windows operating system
This article assumes that you are familiar with the following topics:
Visual Basic .NET
Basic XML standards
XSD schemas
 

Create an XSD Schema

Paste the following code in a new text file named C:\Books.xsd:
 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:bookstore-schema" elementFormDefault="qualified" targetNamespace="urn:bookstore-schema">
<xsd:element name="bookstore" type="bookstoreType" />
<xsd:element name="comment" type="xsd:string" />
<xsd:element name="author" type="authorName"/>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType" />
<xsd:element ref="comment" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element ref="author" />
<xsd:element name="price" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string" />
</xsd:complexType>

</xsd:schema>
 

Create a Visual Basic .NET Application

1. Create a new Visual Basic .NET Windows application
 
2. Drag Button1 to Form1. Paste the following code to add a private member variable to Class Form1:
 
Dim m_success As Boolean
3. Paste the following sub procedure to create a ValidationEventHandler that raises validation errors in the XMLValidatingReader object:     
 
Public Sub ValidationEventHandle(ByVal sender As Object, ByVal args As ValidationEventArgs)
        m_success = False
        Console.WriteLine((ControlChars.CrLf & ControlChars.Tab & "Validation error: " & args.Message))
    End Sub 'ValidationEventHandle
 
NOTE: You must include an event handler to receive information about validation errors in the Data Type Definition (DTD), the XML-Data Reduced (XDR) schema, and the XML schema definition language (XSD) schema. The event handler receives an argument of type ValidationEventArgs that contains data related to this event.

The callback handler can use the ValidationEventArgs.Severity property to guarantee that an XML instance document is validated against a schema. The Severity property enables you to distinguish between a validation error (Severity is equal to XmlSeverityType.Error) which indicates a fatal error, and a validation warning (Severity is equal to XmlSeverityType.Warning) which indicates that no schema information is available.
 
4. Paste the following code in the Button1_Click event procedure:
 
        Dim reader As XmlValidatingReader = Nothing
        Dim myschema As New XmlSchemaCollection()

        Try
            'Create the XML fragment to be parsed.
            Dim xmlFrag As String = "<author  xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" & _
                            "<first-name>Herman</first-name>" & _
                           "<last-name>Melville</last-name>" & _
                         "</author>"

            'Create the XmlParserContext.
            Dim context As New XmlParserContext(Nothing, Nothing, "", XmlSpace.None)
          
            'Implement the reader.
            reader = New XmlValidatingReader(xmlFrag, XmlNodeType.Element, context)
            'Add the schema.
            myschema.Add("urn:bookstore-schema", "Books.xsd")

            'Set the schema type and add the schema to the reader.
            reader.ValidationType = ValidationType.Schema
            reader.Schemas.Add(myschema)

            'Add the handler to raise the validation event.
            AddHandler reader.ValidationEventHandler, AddressOf Me.ValidationEventHandle

            While reader.Read

            End While
            Console.WriteLine("Completed validating xmlfragment")

        Catch XmlExp As XmlException
            Console.WriteLine(XmlExp.Message)
        Catch XmlSchExp As XmlSchemaException
            Console.WriteLine(XmlSchExp.Message)
        Catch GenExp As Exception
            Console.WriteLine(GenExp.Message)
        End Try
    End Sub
 
5.When the following message is displayed in the output window, the XML fragment is a valid element:
 
Completed validating xmlfragment
 
NOTE: The XMLValidatingReader object validates only the type declarations and the top level elements in the XML Schema. XML fragments, such as sub elements, are considered to be local. You cannot pass XML fragments to XmlValidatingReader for direct validation unless you declare the XML fragments as top-level elements and set the reference at the required level.
 
APPLIES TO
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft .NET Framework 1.1
- Microsoft .NET Framework 1.0

How to connect to an Oracle database by using ASP and ADO

 

INTRODUCTION

This article discusses how to connect to an Oracle database by using a Microsoft Active Server Pages (ASP) page and Microsoft ActiveX Data Objects (ADO).
 

MORE INFORMATION

To connect to an Oracle database, you can create an ASP page that contains the following code.
 
Note
 
Make sure that the connect string has a valid user ID and password and that the SQL statement references a valid table.
 
<%@ Language=VBScript %>
   <html>
   <head>
   <title>Oracle Test</title>
   </head>
   <body>
   <center>
   <%
     Set objConn = Server.CreateObject("ADODB.Connection")
     objConn.Open "Provider=MSDAORA;Data Source=<Your_TNSNames_Alias>;User Id=<userid>;Password=<password>;"

     Set objRs = objConn.Execute("SELECT * FROM DEMO.EMPLOYEE")

     Response.Write "<table border=1 cellpadding=4>"
     Response.Write "<tr>"

     For I = 0 To objRS.Fields.Count - 1
       Response.Write "<td><b>" & objRS(I).Name & "</b></td>"
     Next

     Response.Write "</tr>"

     Do While Not objRS.EOF
       Response.Write "<tr>"

       For I = 0 To objRS.Fields.Count - 1
         Response.Write "<td>" & objRS(I) & "</td>"
       Next

       Response.Write "</tr>"

       objRS.MoveNext
     Loop

     Response.Write "</table>"

     objRs.Close
     objConn.Close
   %>
   </center>
   </body>
   </html>
 
APPLIES TO
- Microsoft Active Server Pages 4.0
- Microsoft Internet Information Services 6.0
- Microsoft Data Access Components 2.8