How to Debug Window Service without installing


Usually if we want to debug the windows service project to see if it is behaving as we expected, what we do is by installing the windows service and attaching that project to the process.

There is also another way to debug without installing it, what we need to do is by creating another project such as console project then copy and paste the code below.

  1.     Sub Main()
  2. #If Not Debug Then
  3.         Dim servicesToRun As System.ServiceProcess.ServiceBase()
  4.  
  5.         servicesToRun = New System.ServiceProcess.ServiceBase() {New MyService()}
  6.         System.ServiceProcess.ServiceBase.Run(servicesToRun)
  7. #Else
  8.         Dim service As AS2Client.AS2SenderService = New MyService()
  9.         service.myentrysub()
  10.  
  11.         System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
  12.  
  13. #End If
  14.  
  15.  
  16.        
  17.     End Sub


References:
Run Windows Service as a Console program

Debugging Windows Services under Visual Studio .NET

, , ,

No Comments


How to find the CONSTRAINTS of table in SQL Server

Below is the query on finding the constraints of the table

  1. select * from sys.objects where parent_object_id = object_id(N'tblDemographic01Def')

,

No Comments


Operand type clash: nvarchar(max) is incompatible with image

This error usually occurs when you are adding / updating a record on the database table with image/binary columns, and the value of that image column is null and you did not specify the data type of the parameter.

To resolve this issue you need to explicitly specify the datatype of that image/binary column because the default data type of the Sql parameter is varchar(string)

, , ,

No Comments


How to Capture form without showing it in .Net

  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5.  
  6. public class ControlPainter
  7. {
  8.   private const int
  9.     WM_PRINT = 0×317, PRF_CLIENT = 4,
  10.     PRF_CHILDREN = 0×10, PRF_NON_CLIENT = 2,
  11.     COMBINED_PRINTFLAGS = PRF_CLIENT | PRF_CHILDREN | PRF_NON_CLIENT;
  12.  
  13.   [DllImport("USER32.DLL")]
  14.   private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, int lParam);
  15.  
  16.   public static void PaintControl(Graphics graphics, Control control)
  17.   { // paint control onto graphics
  18.     IntPtr hWnd = control.Handle;
  19.     IntPtr hDC = graphics.GetHdc();
  20.     SendMessage(hWnd, WM_PRINT, hDC, COMBINED_PRINTFLAGS);
  21.     graphics.ReleaseHdc(hDC);
  22.   }
  23. }
  24.  
  25. <strong>TO USE IT:
  26. </strong>
  27. using (Form f = new MyForm()) {
  28. Image i = new Image(f.Width, f.Height);
  29. Graphics g = Graphics.FromImage(i);
  30. ControlPainter.PaintControl(g, f);
  31. }

Source

Reference

,

No Comments


How to get the difference between two dates in .net

You can use the built-in function for getting the difference between two dates.

Example:

  1.         Dim d1 As DateTime = Now
  2.         Dim d2 As DateTime = Now.AddDays(3)
  3.  
  4.         Dim m As TimeSpan = d2.Subtract(d1)
  5.         Dim t As String
  6.  
  7.         t = "Days : " & m.Days.ToString() & vbCrLf
  8.         t += "Hours : " & m.Hours.ToString() & vbCrLf
  9.         t += "Minutes : " & m.Minutes.ToString() & vbCrLf
  10.         t += "Seconds : " & m.Seconds.ToString() & vbCrLf
  11.         t += "Milliseconds : " & m.Milliseconds.ToString() & vbCrLf
  12.         MessageBox.Show(t)

, ,

No Comments


How to convert .net Form to Image

  1. Public Class Form1
  2.  
  3.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  4.         CreateBitmap(Me)
  5.     End Sub
  6.  
  7.     Private Sub CreateBitmap(ByVal con As Control)
  8.         'Dim g As Graphics = Me.CreateGraphics()
  9.  
  10.         Dim b As Bitmap = New Bitmap(Me.Width, Me.Height)
  11.         Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))
  12.  
  13.         b.Save("test.jpg", Imaging.ImageFormat.Jpeg)
  14.         b.Dispose()
  15.         'g.Dispose()
  16.     End Sub
  17. End Class

, ,

No Comments


sn.exe Access is denied

You can do this by running the command prompt as a administrator, just right click and run as administrator.

You can also refer to the following link if the above solution does not work:

  1. sn.exe fails with Access Denied error message
  2. How To Create an Assembly with a Strong Name in .NET Framework SDK

, ,

No Comments


Convert color to string

To convert a color to a string use the ColorConverter class but this can be access through TypeConverter class;

  1. Dim myColor as Drawing.Color = Color.Red
  2.  
  3. Dim clor As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(myColor)
  4. dim ColorName as string
  5.  
  6. ColorName = clor.ConvertToString(Color.Red)

, ,

No Comments


Convert color from string

To convert a color from a string use the ColorConverter class but this can be access through TypeConverter class;

  1. Dim myColor as Drawing.Color
  2.  
  3. Dim clor As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(GetType(Drawing.Color))
  4.  
  5. myColor = clor.ConvertFromString("Blue")

MyColor variable is  now ready to use.

, ,

No Comments


An error occurred while validating. HRESULT = ‘80004005′

An error occurred while validating. HRESULT = ‘80004005′, this error just came up when I’m trying to organize my .Net Project files in my Solution, I removed some of the projects and just use the compiled dll in referencing to the main Project. However, this error has appeared during rebuilding the setup project and it took some of my time to find the solution or the cause.

So, what I did was I removed all References from the Target Primary Output project and added them and rebuilding it again , :) that’s it no more errors.


“Thank you for reporting this issue. We were able to reproduce the problem and have identified the root cause. The problem is caused by cross-solution project reference between Solution1 and Solution2. From the attached project, the project “WindowsFormApplication1” in Solution2 references a project that is not in Solution2 (it references ClassLibrary1 from Solution1). To fix the error, the workaround is to copy the ClassLibrary1 project to Solution2 and re-add the reference to ClassLibrary1 within its own solution.


Project-to-project references only works within the same solution. If you have to split into two solutions and split the code for your class library into two projects, you need to also split the project that references the class library into two projects (one for each solution) in order to avoid project references outside the current solution.

I hope this helps.

Candy Chiang
Program Manager – Visual Studio”

For more information and explanation about this error just click here

, , ,

No Comments



SetPageWidth