INTRO:


This project contains three parts. Users, Administrators and Database.

AssetTracking

1. Users login to any domain computers, AssetTrackingClient will run and collect information then write into or update the database.

2. Administrators login Asset Tracking Website and run query against User Name/Service Tag/Computer Name to find the related information.

webPage

 

Details and Code:


2. Client Code:

 public  string getSN()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
            string collectedInfo = ""; // here we will put the informa

            searcher.Query = new ObjectQuery("select * from Win32_BIOS");
            foreach (ManagementObject share in searcher.Get())
            {
                //then, the serial number of BIOS
                collectedInfo += share.GetPropertyValue("SerialNumber").ToString();
            }
            return collectedInfo;
        }
        public void WriteToServer()
        {
            string un = Environment.UserName;
            string cn = Environment.MachineName;
            string sn = getSN();
            try
            {
                SqlConnection myConnection = new SqlConnection("user id=<USERNAME>;" +
                                       "password=<PASS>;server=<SQLServerName>;" +
                                       "database=<DataBaseName>; " +
                                       "connection timeout=30");

                myConnection.Open();

                using (SqlCommand command = new SqlCommand("Select * from <TableName> where Host_Name = '" + cn + "'", myConnection))
                {
                    using (SqlDataReader reader2 = command.ExecuteReader())
                    {
                        command.Dispose();
                        // if the result set is not NULL
                        if (reader2.HasRows)
                        {
                            reader2.Close();
                            SqlCommand myCommand = new SqlCommand("update <tableName> set User_Name='" + un + "',Serial_Number='" + sn + "',Time_Stamp=getdate() where host_name='" + cn + "'", myConnection);
                            myCommand.ExecuteNonQuery();
                            myCommand.Dispose();
                            myConnection.Close();
                            System.IO.File.AppendAllText("c:\\logs.log","database get updated"+ getDateTime()+"\r\n");
                        }

                        else
                        {
                            reader2.Close();
                            SqlCommand myCommand = new SqlCommand("INSERT INTO <tableName> (Host_Name,User_Name,Serial_Number,Time_Stamp) VALUES ('" + cn + "','" + un + "','" + sn + "',getdate())", myConnection);
                            myCommand.ExecuteNonQuery();
                            myCommand.Dispose();
                            myConnection.Close();
                            System.IO.File.AppendAllText("c:\\logs.log", "database get inserted" + getDateTime() + "\r\n");
                        }


                    }
                }


            }
            catch (Exception err)
            {
                
              
            }
        } 

         public string getDateTime()
        {
            return (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString());
        }        

 

5. Query Page Code:

PCREG.ASPX

<%@ Page Title="" Language="VB" MasterPageFile="~/toolbox.master" AutoEventWireup="false" CodeFile="pcreg.aspx.vb" Inherits="_Pcreg" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div style="font:cambria 12px; text-align:center; margin:0 auto;" >
   
   <table style="width:100%">
   <tr>
   <td style="width:75%;font-size:medium;">
   
   </td>
   <td style="width:10%;font-size:medium; text-align:right;">
   Search
   </td>
      <td style="width:15%">
   <asp:TextBox ID="sq" runat="server" > </asp:TextBox> 
   </td>
   </tr>
   
   </table>
<div style="background-color:White; font-size:14px;width:80%;position:relative;left:0px;">  </div><div style="width:12%;position:relative; left:80%;"> </div>
    
   
    <br />
  
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Host_Name" 
            DataSourceID="SqlDataSource1" PageSize="25" CellPadding="4"  
            ForeColor="#333333" GridLines="None" HorizontalAlign="Center"  Font-Size ="Small">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
               
                <asp:TemplateField ShowHeader="False"  ItemStyle-Width="5%">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                            CommandName="Update" Text="Update" ></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                            CommandName="Cancel" Text="Cancel" ></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                   
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                            CommandName="Edit" Text="Edit"  ></asp:LinkButton>
                     
                    </ItemTemplate>
                </asp:TemplateField>
                <%-- Header finished---------- Hostname Templated Field start----------------%>

                 <asp:TemplateField HeaderText="Hostname " SortExpression="Host_Name"  ItemStyle-Width="8%">
                    <itemTemplate>
                <asp:literal  ID="Label25" runat="server" Text='<%# Bind("Host_Name") %>' ></asp:literal>
                    </itemTemplate>
                    </asp:TemplateField>
           

                     <%-- Host_Name finished-------------- Time_Stamp Field start----------------%>

                 <asp:TemplateField HeaderText="Time" SortExpression="Time_Stamp"  ItemStyle-Width="10%">
                    <itemTemplate>
                <asp:literal  ID="Label22" runat="server" Text='<%# Bind("Time_Stamp") %>' ></asp:literal>
                    </itemTemplate>
                    </asp:TemplateField>
                     <%-- Time_stamp finished-----------------%>

                <%-- User_Name Field start---------------%>

                    <asp:TemplateField HeaderText="User_Name " SortExpression="User_Name"  ItemStyle-Width="6%">
                    <EditItemTemplate>
                    <asp:TextBox Width="60px" ID="usernameBox" Text='<%# Bind("User_Name")%>' runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <itemTemplate>
                        <asp:LinkButton ID="lnkRedirect" runat="server" Text='<% #Eval("User_Name")%>' PostBackUrl='<% #Eval("url")%>'
                            OnClick="lnkRedirect_Click"></asp:LinkButton>
                         
                    </itemTemplate>
                    </asp:TemplateField>
                    <%------------username ended ------%>
                        <asp:TemplateField HeaderText="S_N " SortExpression="Serial_Number"  ItemStyle-Width="6%">
                    <EditItemTemplate>
                    <asp:TextBox Width="60px" ID="Serial_Number" Text='<%# Bind("Serial_Number")%>' runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <itemTemplate>
                        <asp:label ID="Serial_Number_label" runat="server" Text='<% #Bind("Serial_Number")%>' ></asp:label>
                         
                    </itemTemplate>
                    </asp:TemplateField>
                    <%-----------------SN ended------------%>


                
                <asp:TemplateField HeaderText="Select_Issue" SortExpression="Select_Issue"   ItemStyle-Width="16%">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" selectedvalue='<%# Bind("Select_Issue") %>'  >  
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>NameTag Removed - Redone </asp:ListItem>
                        <asp:ListItem>System Update - Reimaged  </asp:ListItem>
                        <asp:ListItem>Virus - Reimaged  </asp:ListItem>
                        <asp:ListItem>Harddisk Replacement </asp:ListItem>
                        <asp:ListItem>Screen Replacement </asp:ListItem>
                        <asp:ListItem>Power Panel Replacement </asp:ListItem>
                        <asp:ListItem>KeyBoard Replacement </asp:ListItem>
                        <asp:ListItem>Damage - High </asp:ListItem>
                        <asp:ListItem>Damage - Medium </asp:ListItem>
                        <asp:ListItem>Damage - Low </asp:ListItem>
                        <asp:ListItem>System Maintenance - Local </asp:ListItem>
                        <asp:ListItem>System Maintenance - LWT </asp:ListItem>
                           </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Select_Issue") %>'  ></asp:Label> 
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="History" SortExpression="History"  ItemStyle-Width="20%">
                    <EditItemTemplate>
                        <FTB:FreeTextBox ID="TextBoxNote2" runat="server"  Height="200px" ToolbarLayout="" Width="200px"
      Text='<%# Bind("History")%>'  />
                     
                    </EditItemTemplate>
                    <ItemTemplate>
                 <asp:literal  ID="Label221" runat="server" Text='<%# Bind("History") %>'  ></asp:literal>  
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Note" SortExpression="note"  ItemStyle-Width="20%">
                    <EditItemTemplate>
                     <FTB:FreeTextBox id="FreeTextBox1" runat="Server"  Text='<%# Bind("note") %>' Height="200px"  Width="200px" ToolbarLayout="" />
                     </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("note") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConflictDetection="CompareAllValues" 
            ConnectionString="<%$ ConnectionStrings:bscConnectionString %>" 
            DeleteCommand="DELETE FROM [<tableName>] WHERE [Host_Name] = @original_Host_Name " 
            InsertCommand="INSERT INTO [<tableName>] ([Host_Name], [Time_Stamp], [User_Name], [Serial_Number], [note], [Select_Issue], [History],[Damaged],[Returned]) VALUES (@Host_Name, @Time_Stamp, @User_Name, @Serial_Number, @note, @Select_Issue, @History)" 
            OldValuesParameterFormatString="original_{0}" 
            SelectCommand="SELECT [Host_Name], [Time_Stamp], [User_Name],'http://<ExternalSearchEngine>/Search.aspx?search='+ [User_Name] as 'url', [Serial_Number], [note], [Select_Issue], [History] FROM [<tableName>] WHERE ([Serial_Number] LIKE '%' + @Serial_Number + '%' or [Note] LIKE '%' + @Serial_Number + '%' or [Host_Name] LIKE '%' + @Serial_Number + '%' or [User_Name] LIKE '%' + @Serial_Number + '%')" 
            UpdateCommand="UPDATE [<tableName>] SET [Time_Stamp] = @Time_Stamp, 
            [Serial_Number] = @Serial_Number,
            [note] = @note,
            [User_Name] = @User_Name,
            [History] = 
             ( CASE
         WHEN @Select_Issue is null  THEN @History
         ELSE @Select_Issue +' at ' + LEFT(CONVERT(VARCHAR, getdate(), 120), 10) +CHAR(10) +CHAR(13) +'<BR>' + ISNULL(History, ' ') 
         END
              )

            WHERE [Host_Name] = @original_Host_Name ">
            <DeleteParameters>
                <asp:Parameter Name="original_Host_Name" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Host_Name" Type="String" />
                <asp:Parameter Name="Time_Stamp" Type="DateTime" />
                <asp:Parameter Name="User_Name" Type="String" />
                <asp:Parameter Name="Serial_Number" Type="String" />
                <asp:Parameter Name="note" Type="String" />
                <asp:Parameter Name="Select_Issue" Type="String" />
                <asp:Parameter Name="Completed" Type="String" />
                <asp:Parameter Name="History" Type="String" />
                <asp:Parameter Name="Returned" Type="String" />
                <asp:Parameter Name="Damaged" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="sq" DefaultValue="%" Name="Serial_Number" 
                    PropertyName="Text" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Time_Stamp" Type="DateTime" />
                <asp:Parameter Name="User_Name" Type="String" />
                <asp:Parameter Name="Serial_Number" Type="String" />
                <asp:Parameter Name="note" Type="String" />
                <asp:Parameter Name="Select_Issue" Type="String" />
                <asp:Parameter Name="History" Type="String" />
                <asp:Parameter Name="original_Host_Name" Type="String" />
                <asp:Parameter Name="original_Time_Stamp" Type="DateTime" />
                <asp:Parameter Name="original_User_Name" Type="String" />
                <asp:Parameter Name="original_Serial_Number" Type="String" />
                <asp:Parameter Name="original_note" Type="String" />
                <asp:Parameter Name="original_Select_Issue" Type="String" />
                <asp:Parameter Name="original_History" Type="String" />
                <asp:Parameter Name="Original_Returned" Type="String" />
                <asp:Parameter Name="original_Damaged" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
       
    </div>
</asp:Content>

 

PCREG.APSX.VB

Imports System.Web.Configuration

Partial Class _Pcreg
    Inherits System.Web.UI.Page
    Protected Sub lnkRedirect_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim grdRow As GridViewRow = DirectCast(DirectCast(sender, LinkButton).NamingContainer, GridViewRow)
        Dim lnkButton As LinkButton = TryCast(grdRow.FindControl("lnkRedirect"), LinkButton)

        Response.Write("<script type='text/javascript'>detailedresults=window.open(lnkButton.PostBackUrl);</script>")





    End Sub
    Protected Sub showSelectIssue(ByVal sender As Object, ByVal e As EventArgs)
        GridView1.Columns(5).Visible = "true"

    End Sub
    Protected Sub invisibleColumn5(ByVal sender As Object, ByVal e As EventArgs)
        GridView1.Columns(5).Visible = "false"

    End Sub



End Class

 

About Lei

I am an IT specialist with over 10 year experience - years on Automation, on-Premise or Azure.

I am happy to develop however never want be a full time developer. Only do what I have to do. If it has to be PowerShell,HTML, PHP, CSS, C#, VBS or JS, front end or backend, so be it, doesn't matter!

Spent years with Windows, SCCM, SharePoint, SQL and Exchange servers. For last several years, I have been actively working under On Premise > Azure environment.

THERE IS NO WAY BACK!!!

Current Certificates:
    Microsoft® Certified-
  • -Enterprise Administrator
  • -Database Administrator
  • -SharePoint Administrator
  • -Administering and Deploying SCCM 2012
Red Hat Certified Technician
ITIL V3 Foundation - Practitioner

Working on Azure Certificates now and hopefully they can stop upgrading their questions one day! GIVE ME A BREAK!

Contact Lei

Name *
Email *
Comments *

Traffic since 10/08/2016

Today52
Yesterday83
This week676
This month1972
Total416332

Visitor Info

  • IP: 54.227.51.103
  • Browser: Unknown
  • Browser Version:
  • Operating System: Unknown

Who Is Online

1
Online

2017-12-17

Login