Wednesday, April 28, 2010

Remove grid lines in remote desktop

Computer Tips,Remove grid lines in remote desktop, Remote desktop issue

I am having been of late developing a Soft ware package in .NET Framework 3.5 for the LegaSync Project, to migrate the data from various legacy ERP systems (SAP,SYTELINE,MACPAC,MANMAN,JDE etc) to SQL server. when it came to production deployment, the client had hosted a server with the latest configuration ( Windows Server 2008 R2, which was a 64 bit machine.)

One of the client from an ERP had faced the issue of  getting annoying gridlines in her remote desktop sessions while logging into the remote machine. It was occuring regularly during every session. Sometimes it will take the entire window and other times it will only occupy a sub-area.

If you use Remote Desktop to remotely manage servers you might have probably ran into a situation recently where you've connected to a remote server and noticed grid lines or occasional "stray pixels" on your RDP window. This is due to a minor incompatibility between Windows Server Terminal Services and older versions of the RDP client.

Fortunately, the solution is very simple. Just download the Remote Desktop/Terminal Services Client 6.0 and install. You can find it here:



http://www.microsoft.com/downloads/details.aspx?familyid=26F11F0C-0D18-4306-ABCF-D4F18C8F5DF9&displaylang=en

Tuesday, April 27, 2010

Display line numbers in .NET Code Editor

.NET Tips,Display line numbers in code editor, display line numbers in VS .NET IDE
We can display the line numbers in the Visual studio .NET code editor.

In the VS.NET IDE ,

Click TOOLS > OPTIONS

Expand TEXT EDITOR (in left window)

Select Basic  > Editor

You will find the Line Numbers Option Under Interaction Group box.

 Check Line Numbers option




Click OK

code Editor with Line Number

Monday, April 26, 2010

Import Microsoft Access 2007 Database Tables into SQL Server 2008

Import Microsoft access 2007 database to SQL Server,Import Microsoft access 2007 database to SQL Server 2008,Import Microsoft access database to SQL Server,Import access 2007 database to SQL Server,Import access 2007 database to SQL Server 2008,Import Ms access 2007 database to SQL Server,Import Ms access database to SQL Server,Import Ms access 2007 database to SQL Server 2008,
Import MS access database, Import MS access 2007 database to SQL, SQL, Microsoft Access 2007,Import access 2007,Import MS access 2007,Import Microsoft access 2007 database tables to SQL Server,Import Microsoft access 2007 database tables to SQL Server 2008,Import Microsoft access database tables to SQL Server,Import access 2007 database tables to SQL Server,Import access 2007 database tables to SQL Server 2008,Import Ms access 2007 database tables to SQL Server,Import Ms access database tables to SQL Server,Import Ms access 2007 database tables to SQL Server 2008,
Import MS access database tables, Import MS access 2007 database tables to SQL, SQL, Microsoft Access 2007,Import access 2007 tables,Import MS access 2007 tables,

This article explains how to import Access 2007 database tables into MS SQL Server 2008.

To import data from a Microsoft Access 2007 database, we must install the OLEDB Provider for Microsoft Office 12.0 Access Database Engine.

1) Goto SQL Server Management Studio, Object Explorer

Right Click the desired database and select Tasks -> Import Data.



Follow the below steps to import data into a SQL Server 2008 database.




2)Select the Data source from which you want to import the data.
Because of the difference between the database engine of Microsoft Access 2007 and earlier version of Microsoft Access, it is not possible to connect to the Access 2007 database using data source “Microsoft Access”. You can use this if you wish to import data from a MDB format, but not an ACCDB from Access 2007.


If you have properly installed the 2007 Office System driver, you will see another Data Source option: “Microsoft Office 12.0 Access Database Engine.”



Step2: Select Microsoft Office 12.0 Access Database Engine


3) Click the Properties button to open the Data Link Properties window:



Step 3: Enter access 2007 database Path


Enter the full path of the Access 2007 database in Data Source Name. Click Test Connection to make sure the connection succeeds.


4) Click Ok to close the properties page. Click Next to choose the destination database


step4: Choose destination database


5. Click Next to continue.

Step 5: Select option to copy




choose option for Copy data from one or more tables or views.


6) Click Next, and select the Source Tables or Views to import. If necessary use the Edit Mapping button to map the Columns correctly.


Step 6: Select tables/view to copy


7) Click Next to continue.




 The next screen of the wizard shows two options.
 First one is to Execute Package Immediately and second one is to save SSIS package which can be used in the Business Intelligence Development Studio Project.

8) Click Next and Finish.




It shows the progress of copying data and lists out errors if any.




On successful import, you can expand the tables on the database and see the imported tables from Access database.









 

Display varbinary columns in Datagridview

Display varbinary columns in DataGridview,VB.NET,.NET Tips,Display varbinary,Display varbinary in Datagridview Varbinary to string,Handling of varbinary data,SQL Varbinary datatype, Byte[], Datagridview
I recently faced an issue while displaying SQL query results in a DataGridView, it was crashing on varbinary columns. The reason was that the default format for a varbinary column is to display it as an image. While that seems like a fine default, there is no way to change it.

The following VB.NET code shows how could I handle the varbinary data display after binding the dynamic datatable to DataGridview.

Steps to handle this scenario:

1. Bind the DataGridview DataSource with the datatable.
2. Create a new DataGridview Column of type String
2. Set the DataPropertyName and Name of new Datagridview Column as the Varbinary column name.
3. Remove the original  varbinary column from the DataGridview
4. Insert the new column at the same position as the original column in the Datagridview
5. Implement the CellFormatting event and set the value in the format you want . I have converted each byte  into hex format and displayed the value.

'Global declarationDim sByteArrayColumns As New ArrayList


Private Sub PopulateGrid()

RemoveHandler dbGridView.CellFormatting, AddressOf dbGridView_CellFormatting
AddHandler dbGridView.CellFormatting, AddressOf dbGridView_CellFormatting
  
Dim dataSource As New BindingSource(dt, Nothing)

dbGridView.DataSource = dataSource


sByteArrayColumns.Clear()


For colCnt As Integer = dbGridView.ColumnCount - 1 To 0 Step -1


If DirectCast(dbGridView.Columns(colCnt).ValueType, System.Type).IsArray Then


 sByteArrayColumns.Add(colCnt)

'Creation of new datagridview column
Dim dgColumn As New DataGridViewTextBoxColumn()
dgColumn.DataPropertyName = dbGridView.Columns(colCnt).Name
dgColumn.Name = dbGridView.Columns(colCnt).Name
dgColumn.HeaderText = dbGridView.Columns(colCnt).Name
dgColumn.ValueType = GetType(String)

'Removal of original varbinary column
dbGridView.Columns.RemoveAt(colCnt)

'Add the new column
dbGridView.Columns.Insert(colCnt, dgColumn)
dgColumn.Dispose()
dgColumn = Nothing
End If
Next

End Sub
Implementation of cell formatting event:

Private Sub dbGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)



Try


 If e.ColumnIndex < 0 OrElse e.Value Is Nothing Then
      Return
  End If

   Dim bArrayField As Boolean = False


   For i As Integer = 0 To sByteArrayColumns.Count - 1


   If e.ColumnIndex = CInt(sByteArrayColumns(i)) Then
        bArrayField = True
        Exit For
   End If


Next
 

If bArrayField Then
  Dim array As Byte() = DirectCast(e.Value, Byte())
  e.Value = String.Empty


  For Each b As Byte In array 'convert each byte value to hex value
      e.Value += [String].Format("{0:X2}", b)
Next

End If 
Catch ex As Exception 
End Try

End Sub


Sample Output

Thursday, April 22, 2010

System.Management.Automation dll missing

System.Management.Automation,.NET,.NET Tips,Microsoft
If the Powershell SDK is already installed, System.Management.Automation dll will be found in the following location.

C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0

The Windows PowerShell 2.0 Software Development Kit can be downloaded from the following link:

http://www.microsoft.com/downloads/details.aspx?FamilyID=50633A1F-A665-425D-923C-1A269F8AC084&displaylang=en

Tuesday, April 20, 2010

Populate DataGridView using CSV File

The following VB.NET Code is used to populate the DataGridView using CSV File.


I have used a 2 dimensional array to store the content of csv row and column wise


Also 2 One dimensional arrays are used to populate the row and column data respectively.


The logic is to parse the file content into string and store in a two dimensional array.


Then read row wise and column wise and populate the DataGridview.

A new DataGridView Row will be created for each line of data and the cells will be populated with the column data.

Private Sub PopulateDataGridViewFromCSV(ByVal SFileName As String)

   'Variables to store the count of rows and columns

   Dim nRows As Long
   Dim nCols As Long


  'Index variables to retrieve the data row wise and col wise

  Dim row As Integer
  Dim col As Integer


'Declaration of Two dimensional array to store the csv content row wise and column wise
Dim ParseArray(1, 1) As String


'Use Try Catch block to handle exceptions
Try


'Check if file exist


If File.Exists(SFileName) Then


   dbGridView.DataSource = Nothing
   dbGridView.Columns.Clear()

' Create an unbound DataGridView by declaring a column count.

  dbGridView.ColumnCount = 2
   dbGridView.ColumnHeadersVisible = True
'Set the column header names.
dbGridView.Columns(0).Name = "EmpID"
dbGridView.Columns(1).Name = "Name"

'Open the CSV File into stream

Dim sr As StreamReader = File.OpenText(SFileName)

'Declaration of two single dimensional arrays to store the individual content

Dim RowStringArray() As String
Dim ColStringArray() As String





'Load content of file to RowStringArray array


RowStringArray = sr.ReadToEnd().Split(Environment.NewLine)


'Retrieve the total number of rows
nRows = UBound(RowStringArray)



' Split the content based on "," delimiter


ColStringArray = RowStringArray(0).Split(",")


'Retrieve the total number of columns


nCols = UBound(ColStringArray)


' Redimension the array.


ReDim ParseArray(nRows, nCols)


'Copy the csv content into the 2-dimensional array.


For row = 0 To nRows




ColStringArray = RowStringArray(row).Split(",")



For col = 0 To nCols


ParseArray(row, col) = ColStringArray(col)


Next
Next



Dim dgRow As DataGridViewRow = Nothing


For row = 0 To nRows


dgRow = New DataGridViewRow()


'Add the row
dbGridView.Rows.Add(dgRow)


For col = 0 To nCols


dbGridView.Rows(row).Cells(col).Value = ParseArray(row, col)

Next
Next

End If


Catch ex As Exception


MessageBox.Show(ex.Message)

End Try
End Sub


USAGE


PopulateDataGridViewFromCSV ("C:\Test.csv")

will populate the DataGridView With the C:\Test.Csv file.

 
Sample File: Test.CSV

1001,XXX
1002,YYY

Output


Monday, April 19, 2010

Panel Double buffering VB.NET and C# - Flicker Free

I faced a problem when loading background image in the panel. It used to flicker a lot. I thought of double buffering concept. But there is no in-built double buffering property for panel.

All what I did is to write a class which inherits from Panel Class, and in the constructor  set the DoubleBuffered to true and set the appropriate control styles.

VB.NET Code

Public Class DoubleBufferPanel

Inherits Panel


Public Sub New()


Me.DoubleBuffered = True

SetStyle(ControlStyles.AllPaintingInWmPaint Or _

ControlStyles.DoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)

UpdateStyles()


End Sub
End Class

 
After building the project, we should be able to see the DoubleBufferPanel component in the ToolBox.

Double buffer Panel


Then drag and drop the panel  and used it. The problem is solved. It's so simple.

C# Code

public class DoubleBufferPanel : Panel
{


public DoubleBufferPanel()
{


// Set the value of the double-buffering style bits to true.
   this.DoubleBuffered = true;



   this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
    ControlStyles.AllPaintingInWmPaint,true);


    this.UpdateStyles();


}


}

Friday, April 16, 2010

Log off Remote Desktop Users

There are serveral DOS commands to forcibly log off the remote user. But we need to be an administrator of the target machine.

How to query for users on a machine?

First we can query to find out what users have a session on a remote machine? Windows provides the "quser" command which we can use to query for the sessions running on that machine.

The format is as follows:

quser /server:ServerName

Here is a screenshot running this command against a remote machine. Notice that it shows the User Name, state and ID of the session.


Query Users on a machine



How to log off a user of a machine?

How can we force the  user to log off. There is a handy little command called "logoff" that we can use to force a user to log off a machine based on their Session ID.

The format is as follows:

logoff SessionId /Server:ServerName


Log off User

Notice that I have used the Session Id which I got from the above.

Note: If you remotely log off a user, their log session goes away which could mean the unsaved data is lost, or if the user in the middle of an activity, we might be in trouble.


Of course some things are easier to do using a script:


Here is the VBScript code, which allows the user to remotely log off.

strComputer = ""

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems

ObjOperatingSystem.Win32Shutdown(0)

Next

Save the code in .vbs format and execute it.

Wednesday, April 14, 2010

Value Close() cannot be called while doing CreateHandle()

The error Value Close() cannot be called while doing CreateHandle() usually happens when we try to close the form in the constructor or Load event.

For example,

the following code gives the error:


Private Sub RootForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



'Some Validation
'If Failed then close the form
Me.Close 'The exception throws -->Value Close() cannot be called while doing CreateHandle()
End Sub

Solution:
The form should not be closed on load event. Instead we can write the procedure to close the form, and call the procedure in button click.

But the form cannot be closed during the loading point of time. So the form's closing call should be done after loading the form.

Display row count for all SQL tables

The following T-SQL script returns the number of rows in each table for a database.


SELECT '[' + SCHEMA_NAME(t.schema_id) + '].[' + t.name + ']' AS [Full Table Name], SCHEMA_NAME(t.schema_id) AS [Schema Name], t.name AS [Table Name],

i.rows AS [Row Count] FROM sys.tables AS t INNER JOIN

sys.sysindexes AS i ON t.object_id = i.id AND i.indid < 2
WHERE (t.type = 'U')
   
 
Sample Output:
 
Displaying row count for all tables