Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Sybase's DataWindow .NET: Way Beyond the .NET DataGrid : Page 5

Sybase's new DataWindow .NET lets you create powerful data-driven applications that provide advanced display and printing capabilities with less programming than ever. If you ever worked with PowerBuilder, you'll know why a .NET version of the DataWindow is such an important addition to your toolset. If not, you'll be surprised at the power of the DataWindow.

Using Your DataWindow
Now that you've created the DataWindow object, you can use it in a Windows application. Create a windows project called FirstDataWindow. I've used C# for the sample code, but you could use any .NET language. Save the project in the myPubFolders Folder where you saved the .pbl file containing the authors DataWindow earlier. Use Visual Studio's Solution Explorer to rename the default form to FirstDataWindow.cs. Resize the form until it's large enough to hold the buttons shown in Figure 23.

Figure 23. Sample Application Buttons: The figure shows the buttons you should add to create the sample application.
Figure 24. Sample Form with Named and Anchored Buttons: The figure shows how you should place and anchor the buttons on the sample form.
Name all button objects according to their visual label, e.g. buttonFilter, buttonAdd, buttonPrint, etc. Set the anchor property to "Bottom, Left" for all buttons.

When you're done, the form should look like Figure 24.

Adding the Transaction Object
You need to establish a transaction object to connect the application to the database. All databases support the transaction objects and return error codes and status codes via this object. Here's how to add the non-visual transaction object to the form (note that you could add it entirely through code—typically three to six lines of code depending on the database). Click Toolbox and select the Sybase DataWindow tab (see Figure 2). Click on the Transaction object, and slide it to the form. You won't see any visual indication, but there is now an additional control on the form. Because the sample application uses ODBC and you configured all the parameters earlier, you need to set only the dbparameter to:

ConnectString='DSN=myPubs;UID=;PWD=', DelimitIdentifier='No',CommitOnDisconnect='No'

The ODBC setting shown uses Windows Authentication. Provide your User ID and password in the ConnectString if you're using SQL Server authentication. Verify that the setting is correct by clicking the Test Connection link on the properties tab of the transaction (at the very bottom). For this example, you're setting the transaction object on the Windows Form. Typically, you would set the transaction one time for the entire application. The transaction object is analogous to a .NET DataAdapter—the DataWindow uses it to pass requests to the database and receive information and data from the database.

Adding the DataWindow
Next, add a DataWindow control to the form. Remember, the DataWindow control is generic; you set properties so that the control knows which DataWindow object to use. Select the DataWindow Control from the toolbox and drag it to the form. Visual Studio gives the control a default name of dataWindowControl1. Rename the control to dwc1 and resize it until it takes up most of the screen just above the buttons. Set the following properties:

LibraryList = C:\myPubsFolder\mypubsDataWindow.pbl ScrollBars = VerticalAndHorizontalSplit DataWindowObject = de_authors Anchor = Top, Bottom, Left, Right

Set the library list first. Then follow the button in the dialog to point the library list to the .pbl file you created using the DataWindow designer (see Figure 25).

Figure 25. Library List: Although you only have one library at present, the Library list lets you choose from among available libraries.
Figure 26. The Sample Form: The figure shows how your form should look after selecting the authors DataWindow library. Note that no data appears on the form yet.
After identifying the library list file you'll see all the DataWindows in that pbl library (you've created only one so far). Select the authors DataWindow You'll see the visual data presentation (without data) on the form, which should now look similar to Figure 26.

You won't be able to view the data until you add a bit of code to connect to the database and run a query.

Retrieving Data
Finally! Some programming! You want to connect to the database when the form opens. Place the following code in the form1_Load function.

// connect to the database using the transaction object transaction1.Connect();

Now add two lines to the Retrieve button clicked event:

// tell the DataWindow what transaction to use // and then retrieve the data dwc1.SetTransaction(transaction1); dwc1.Retrieve();

Add one line of code to the close button to close the form:


Run the application and click the Retrieve button and you'll see the data from the authors table. Resize the window and you'll see that the Page field in the header section automatically recalculates the pages. That page figure is accurate when you print as well.

Printing, Sorting, Filtering, and Exporting
Here's one of the main features of the DataWindow: It's a WYSIWYG (What You See Is What You Get) control. When you print, you'll get exactly what you see on the screen—except that the page numbers will reflect the number of printed pages rather than the number of onscreen pages.

To add the print functionality to buttonPrint_Click, place the following code in the handler. //print to the default printer device dwc1.Print(); That's it! To test the print feature, run the application and click the Print button. The rptAuthors.pdf file in the downloadable sample code shows the actual printout.

Sorting and filtering share the same concept; you first set the sort or filter condition, then execute the action (sort or filter). If you provide a null string as the condition, users will be able to create their own conditions. Add the following code to the ButtonFilter_Click and ButtonSort_Click event handlers:

// ButtonFilter_Click code String sNull = null; //set the filter (Null let's the user do it) dwc1.SetFilter(sNull); //execute the filter on the local dataset – // no trip to the server! dwc1.Filter(); // ButtonSort_Click code String sNull = null; //set the Sort (Null let's the user do it) dwc1.SetSort(sNull); //execute the sort on the local dataset – //no trip to the server! dwc1.Sort();

Now run the application, retrieve the data and test the filter. Enter a filter condition such as: phone like '4%'. The expression dialog lets you build the condition and won't let you save incorrect syntax. The Sort dialog lets users drag and drop the columns on which to sort. Note that, for simplicity, the sample code doesn't include any error handling or error prevention. In a real-world application, you would disable all the buttons until the data retrieval process completes. You could add a function and pass true or false to enable/disable all buttons.

One final flashy capability: The DataWindow can export to Excel, XML, CSV, Text, SQL Insert and PDF. Exporting to PDF requires you to deploy an additional ghostscript DLL, but you can generate a XML file with just a little code. Add the following code to the ButtonExport_Click method.

// save as XML based on FileSaveAsType.Xml dwc1.SaveAs("c:\\myPubsFolder\\test.xml", Sybase.DataWindow.FileSaveAsType.Xml);

You can view the generated XML file using Internet Explorer or your favorite XML viewer.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date