Locations of visitors to this page


Onteora Software - March 2007

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Vista: File System Transactions

Vista: File System Transactions



Windows Vista has several new functions for using transactions when working with files.  Here is an example on how to create a transaction, work with some files, and commit it.


Imports System.Runtime.InteropServices
Imports Microsoft.Win32.SafeHandles
Imports System.IO


Module Module1

    Public Declare Auto Function CreateTransaction Lib "Ktmw32.dll" (ByVal Attributes As IntPtr, _
        ByVal guid As IntPtr, ByVal options As Integer, ByVal isolationlevel As Integer, _
        ByVal isolationflags As Integer, ByVal milliseconds As Integer, ByVal description As String) As IntPtr

    Public Declare Auto Function RollbackTransaction Lib "Ktmw32.dll" (ByVal handle As IntPtr) As Boolean

    Public Declare Auto Function CommitTransaction Lib "Ktmw32.dll" (ByVal handle As IntPtr) As Boolean

    Public Declare Auto Function CloseHandle Lib "Kernel32.dll" (ByVal handle As IntPtr) As Boolean

    Public Declare Auto Function CreateFileTransacted Lib "Kernel32.dll" (ByVal fileName As String, _
        ByVal desiredAccess As Integer, ByVal ShareMode As Integer, ByVal SecurityAttributes As IntPtr, _
        ByVal CreationDisposition As Integer, ByVal FlagsAndAttributes As Integer, _
        ByVal hTemplateFile As IntPtr, ByVal transaction As IntPtr, _
        ByVal miniversion As IntPtr, ByVal extendedOpenInfo As IntPtr) As SafeFileHandle

    Public Declare Auto Function DeleteFileTransacted Lib "Kernel32.dll" (ByVal fileName As String, _
        ByVal transaction As IntPtr) As Boolean

    Private Const GENERIC_READ As Integer = &H80000000
    Private Const GENERIC_WRITE As Integer = &H40000000
    Private Const CREATE_NEW As Integer = 1
    Private Const CREATE_ALWAYS As Integer = 2
    Private Const OPEN_EXISTING As Integer = 3

    Sub Main()
        Using sw As StreamWriter = File.CreateText("test1.txt")
            sw.WriteLine("This is the first file")
        End Using

        Using sw2 As StreamWriter = File.CreateText("test2.txt")
            sw2.WriteLine("The other file")
        End Using

        Console.WriteLine("Before delete")
        Console.WriteLine("------------------")
        Dim diBefore As New DirectoryInfo(My.Application.Info.DirectoryPath)
        For Each fi As FileInfo In diBefore.GetFiles
            Console.WriteLine(fi.Name)
        Next
        Console.WriteLine("------------------")
        Console.WriteLine("After transaction delete")
        Dim tx As IntPtr = CreateTransaction(IntPtr.Zero, IntPtr.Zero, 0, 0, 0, 0, Nothing)
        Dim b As Boolean = DeleteFileTransacted("test1.txt", tx)
        Dim sh As SafeFileHandle
        sh = CreateFileTransacted("SafeTest.txt", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, CREATE_NEW, 0, IntPtr.Zero, tx, IntPtr.Zero, IntPtr.Zero)

        Dim fst As New FileStream(sh, FileAccess.Write)
        Dim swt As New StreamWriter(fst)
        swt.WriteLine("Hello World")
        swt.Close()

        For Each fi As FileInfo In diBefore.GetFiles
            Console.WriteLine(fi.Name)
        Next
        Console.WriteLine("------------------")
        Console.WriteLine("After commit delete")
        CommitTransaction(tx)

        For Each fi As FileInfo In diBefore.GetFiles
            Console.WriteLine(fi.Name)
        Next
        CloseHandle(tx)


    End Sub

End Module

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: VB | Vista
Posted by Ken Tucker on Wednesday, March 21, 2007 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Vista: Get WinSat Info

Vista: Get WinSat Info



Windows Vista has a performance index for your hardware.  The scores range from 1(worst) to 5.9(best).  If your application uses a lot of graphics you might get poor performance on a computer with a graphics score of 1.  You can use this number to scale back on the graphics to improve your apps performance.  This example gets displays the scores in the forms paint event.  For this example add a reference to the WinSat 1.0 type library in the com tab.


Imports WINSATLib
Imports System.Text

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim ws As New CQueryWinSAT
        Dim sbInfo As New StringBuilder

        For x As Integer = 0 To 4
            Dim info As IProvideWinSATAssessmentInfo = ws.Info.GetAssessmentInfo(x)
            sbInfo.Append(info.Title & vbCrLf)
            sbInfo.Append(info.Description & vbCrLf)
            sbInfo.Append(info.Score & vbCrLf & vbCrLf)
        Next

        e.Graphics.DrawString(sbInfo.ToString, Me.Font, Brushes.Black, 0, 0)
    End Sub
End Class

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: VB | Vista
Posted by Ken Tucker on Wednesday, March 21, 2007 10:12 PM
Permalink | Comments (1) | Post RSSRSS comment feed

List Vista RSS feeds

List Vista RSS feeds



To list the RSS feeds added to IE7 in windows Vista add a reference to Microsoft.Feeds 1.0. You will find it in the com tab. 


Imports Microsoft.Feeds.Interop

Public Class Form1
    Dim mgr As New FeedsManager
    Dim fldr As IFeedFolder

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        fldr = mgr.RootFolder

        ListFeeds(fldr)

    End Sub

    Private Sub ListFeeds(ByVal fldr As IFeedFolder)
        For Each feed As IFeed In fldr.Feeds
            Trace.WriteLine(feed.Name)
            ListItems(feed)
        Next
        For Each f As IFeedFolder In fldr.Subfolders
            Trace.WriteLine(f.Name)
            ListItems(f)
        Next
    End Sub

    Private Sub ListItems(ByVal feed As IFeed)
        Trace.Indent()
        For Each item As IFeedItem In feed.Items
            Trace.WriteLine(item.Title)
        Next
        Trace.Unindent()
    End Sub
End Class

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: VB | Vista
Posted by Ken Tucker on Tuesday, March 20, 2007 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Nested GridViews

Nested GridViews



In this example we are going to display the orders and its details from the Northwind database in one GridView.  In an TemplateField place a GridView which calls a function to get the child records.



The Web Page

 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" CellPadding="4" GridLines="None"
                AutoGenerateColumns="False" ForeColor="#333333">
                <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="OrderId" HeaderText="Order ID" />
                    <asp:TemplateField HeaderText="Order Info">
                        <ItemTemplate>
                            <span>Customer </span>
                            <asp:Label ID="lblCust" Text='<%# Eval("CustomerID") %>' runat="server" /><br />
                            <span>Order Date </span>
                            <asp:Label ID="lblOrdDate" Text='<%# Convert.ToDateTime(Eval("OrderDate")).ToShortDateString %>'
                                runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Ordered Items">
                        <ItemTemplate>
                            <asp:GridView runat="server" ID="Details" DataSource='<%# GetDetails(Eval("OrderID")) %>'>
                                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                                <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                                <AlternatingRowStyle BackColor="#DCDCDC" />
                            </asp:GridView>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#999999" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

 

The Code Behind File

 

Imports System.Data.SqlClient
Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim dtDetails As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim dt As New DataTable
            Dim strConn As String
            Dim da As SqlDataAdapter
            Dim conn As SqlConnection

            strConn = "Server = .\sqlexpress;Database = NorthWind; Integrated Security = SSPI;"
            conn = New SqlConnection(strConn)

            da = New SqlDataAdapter("Select * from Orders", conn)
            da.Fill(dt)
            da = New SqlDataAdapter("Select * from [Order Details]", conn)
            da.Fill(dtDetails)
            GridView1.DataSource = dt
            GridView1.DataBind()
        End If

    End Sub

    Public Function GetDetails(ByVal strID As String) As DataTable
        Dim dt As DataTable = dtDetails.Clone
        For Each dr As DataRow In dtDetails.Select("OrderID = " & strID)
            dt.ImportRow(dr)
        Next
        Return dt
    End Function
End Class

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Asp | VB
Posted by Ken Tucker on Friday, March 16, 2007 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Get XML string into a DataGridView

Get XML string into a DataGridView



In the msdn forums I see people asking how do they get a string that contains xml to display in the DataGridView. They way to do this is to read the string into a StringReader and use the DataSet,Readxml method to convert the xml into a datatable. Note the xml must be well formed for this to work.



 Dim srXML As New IO.StringReader(strXML)
 Dim dsXML As New DataSet
 dsXML.ReadXml(srXML)

 DataGridView1.DataSource = dsXML.Tables(0)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: DataGridView | VB
Posted by Ken Tucker on Sunday, March 11, 2007 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

TableAdapter: Use a transaction with an TableAdapter

TableAdapter: Use a transaction with an TableAdapter



The easiest way to use an transaction with a TableAdapter is to use an TransactionScope.  First create a TransActionScope, update the database and finally commit the transaction.  Note not all database type work with System.Transactions. Add a reference to System.Transaction for this example. 

        Using tc As New TransactionScope
            Try
                EmployeeTableAdapter.Update(PubsDataSet.employee)
                'complete the transaction if there were no errors
                tc.Complete()
            Catch
                'something went wrong let the transaction roll back
            End Try
        End Using

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Ado.Net
Posted by Ken Tucker on Saturday, March 10, 2007 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

SMO: Connect to Remote Server

SMO: Connect to Remote Server



Here is a simple example on connecting to a Remote SQL Server with the SMO class. In this example I use a secure string for the password.



        Dim srv1 As New Server("ServerName")
        srv1.ConnectionContext.LoginSecure = False
        srv1.ConnectionContext.SecurePassword = GetSecureString("Password")
        srv1.ConnectionContext.Login = "UserName"

  Function GetSecureString(ByVal str As String) As Security.SecureString
        Dim ss As New System.Security.SecureString

        For Each c As Char In str.ToCharArray
            ss.AppendChar(c)
        Next

        ' prevent changes

        ss.MakeReadOnly()
        Return ss
    End Function

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Sql | VB
Posted by Ken Tucker on Friday, March 09, 2007 11:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Vista Updates and Tools

Vista Updates and Tools



First the Visual Studio 2005 SP1 update for Vista has been released.


Service Pack 2 for SQL Server 2005 is out. This is the only version of SQL Server 2005 that is supported on Vista.


Finally the free Virtual PC 2007 is available

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Vista
Posted by Ken Tucker on Tuesday, March 06, 2007 11:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed