Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Hard Drive Space Values into Datagrid as columns

Posted on 2004-11-15
8
Medium Priority
?
445 Views
Last Modified: 2009-07-29
I am looking to get the hard drives and spaces from my servers and have the below working great to return it via an array - but wanted to do return each  value as a column in a datagrid - is this possible and if so how?
--------------------
    Private Function SystemList(ByVal strComputer As String) As ArrayList

        Dim al2 As New ArrayList
        Dim co As ConnectionOptions = New ConnectionOptions()
        co.Username = "Nsadm13"
        co.Password = "houston"
        Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
        Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("Select * from Win32_LogicalDisk where DriveType=3")
        Dim Query1 As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
        Dim queryCollection1 As ManagementObjectCollection = Query1.Get()
        Dim mo As ManagementObject

        For Each mo In queryCollection1
            al2.Add( _
             "<b>Drive</b>: " & mo("Name").ToString() & _
                        "<br><b>Free Space</b>: " & Data.ConvertBytes(CType(mo("FreeSpace").ToString, Long)) & _
                        "<br><b>Size</b>: " & Data.ConvertBytes(CType(mo("Size").ToString, Long)) _
                       )
        Next
        Return (al2)

    End Function
0
Comment
Question by:tbaseflug
  • 4
  • 4
8 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12584624
Instead of using an arraylist use a datatable
0
 

Author Comment

by:tbaseflug
ID: 12584687
Any way that you could show me based upon my code above?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12584793
Private Function SystemList(ByVal strComputer As String) As datatable

        Dim al2 As New datatable
        al2.columns.add("Diskname", gettype(System.string))
        al2.columns.add("FreeSpace", gettype(System.int32))
        al2.columns.add("Size", gettype(System.int32))

        Dim co As ConnectionOptions = New ConnectionOptions()
        co.Username = "Nsadm13"
        co.Password = "houston"
        Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
        Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("Select * from Win32_LogicalDisk where DriveType=3")
        Dim Query1 As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
        Dim queryCollection1 As ManagementObjectCollection = Query1.Get()
        Dim mo As ManagementObject

        For Each mo In queryCollection1
dim dr as datarows = dt.newrow
dr.item("Diskname") = mo("Name").ToString()
dr.item("FreeSpace") = Data.ConvertBytes(CType(mo("FreeSpace").ToString, int32))
dr.item("Size") = Data.ConvertBytes(CType(mo("Size").ToString, int32))
dt.rows.add(dr)
        Next
        Return (al2)

    End Function
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:tbaseflug
ID: 12585133
RonaldBiemans -

That works great!!!  However, is there any way to flip it so that the drives appear across the top as column headers and that the size and space columns are rows under then., e.g.

C     D     E
---   ---   ---
10   15   20
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12585353
Hmmm, there is but you have to do a bit of cheating, The trick is to transpose your datatable.

Can this wait till tomorrow because I have to leave now, but I can give you the code first thing in the morning



0
 

Author Comment

by:tbaseflug
ID: 12585574
Certainly - I just appreciate the help!
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 2000 total points
ID: 12591638
Hi tbaseflug, try this


Private Function SystemList(ByVal strComputer As String) As datatable

        Dim dt As New datatable
        dt.columns.add("Diskname", gettype(System.string))
        dt.columns.add("FreeSpace", gettype(System.int32))
        dt.columns.add("Size", gettype(System.int32))

        Dim co As ConnectionOptions = New ConnectionOptions()
        co.Username = "Nsadm13"
        co.Password = "houston"
        Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
        Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("Select * from Win32_LogicalDisk where DriveType=3")
        Dim Query1 As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
        Dim queryCollection1 As ManagementObjectCollection = Query1.Get()
        Dim mo As ManagementObject

        For Each mo In queryCollection1
             dim dr as datarows = dt.newrow
             dr.item("Diskname") = mo("Name").ToString()
             dr.item("FreeSpace") = Data.ConvertBytes(CType(mo("FreeSpace").ToString, int32))
             dr.item("Size") = Data.ConvertBytes(CType(mo("Size").ToString, int32))
             dt.rows.add(dr)
        Next

        Dim dt2 As New DataTable
        dt2.Columns.Add("Diskinfo")
        For Each dr2 As DataRow In dt.Rows
            dt2.Columns.Add(dr2.Item("Diskname"), GetType(System.String))
        Next


        For i As Integer = 1 To dt.Columns.Count - 1
            Dim dr2 As DataRow = dt2.NewRow
            dr2(0) = dt.Columns(i).ColumnName
            For r As Integer = 0 To dt.Rows.Count - 1
                dr2(r + 1) = dt.Rows(r).Item(i)
            Next
            dt2.Rows.Add(dr2)
        Next
        Return (dt2)

    End Function
0
 

Author Comment

by:tbaseflug
ID: 12594440
RonaldBiemans -

Wow, that works great!!! You have certainly earned the points.  I am going to open another 500 pt question as I have a list of server names coming from my SQL db and am looping through the server names in a datareader and will need to add the server name to the grid as well - you have earned the pts for this one and will certyainly give another 500 if you want to help on the last part of this?
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question