Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have a problem in getting line number when displaying error. the problem details is

as follows.

1. From my Debug folder when i run the exe and generate error i can see the line number

(as per the below shown code).

2. But when i release the exe to customer and when he gets error. the Line number in

this case is always 0.

3. I have found that there is one .pdb file responsible for line number, but the problem is as this file contains source code line i do not want to distribute the same to our customer.

================Source Snippet==============================
Dim trace As New System.Diagnostics.StackTrace(ex, True)
MessageBox.Show("Line: " + trace.GetFrame(0).GetFileLineNumber().ToString)
============================================================

Any urgent help on this will be highly appreciated.

Thanks and Regards,
Jignesh Patel
Posted

If it's compiled in release mode and not published, you can still get the line number. You just need to change a setting.

Go to the Project Properties
Click on the Compile tab
Click Advanced Compile Options.
Select Full or pdb-only from the Generate debug info ComboBox.

This will work if you are using the Build option.

If however, you are Publishing it, I don't think you'll be able to get the line number.

The best idea would be to put specific lines that could throw an exception into their own Try/Catch block.

I've seen instances like:
VB
Try
  Dim regKey as RegistryKey
  regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\DataCubeUtility", False)
  Dim filePath as String = regKey.GetValue("DataCubeObjectPath")
  
  Dim serializer as Serialization.XmlSerializer = Nothing
  serializer = New Serialization.XmlSerializer(GetType(myCubeClasses.myCubeDef))

  Dim dataFile as New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None)

  myCubeType = serializer.Deserialize(dataFile)

  dataFile.Close()
  dataFile.Dispose
Catch ex as Exception
  MessageBox.Show(ex.Message)
End Try


That is a huge waste of a Try/Catch block. The majority of that will never throw an error. You should break it down and check things yourself. Put the things that could throw an error into it's own Try/Catch block and then customize the message so you know where the error was...as in:
VB
Dim regKey as RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\DataCubeUtility", False)
If regKey Is Nothing Then
  MessageBox.Show("No valid Registry Key")
  Return
End If

Dim filePath as String = regKey.GetValue("DataCubeObjectPath")
If Not File.Exists(filePath) Then
  MessageBox.Show("File " & filePath & " does not exist")
  Return
End If

Dim serializer as Serialization.XmlSerializer = Nothing

Try
  serializer = New Serialization.XmlSerializer(GetType(myCubeClasses.myCubeDef))
Catch ex as Exception
  MessageBox.Show("Unable to create the serializer" & vbNewLine & _
                  "ex.Message: " & ex.Message & vbNewLine & _
                  "ex.StackTrace: " & ex.StackTrace)
  Return
End Try

'etc...


If you did that, you wouldn't need the line numbers.
 
Share this answer
 
I don't think the StackTrace is available when compiled in Release mode.

MSDN Info on StackTrace[^]
 
Share this answer
 
Actually, I did find the answer if you're using ClickOnce deployment.

Go back to the Project Properties.
Click on the Publish Tab.
Click the Application Files button.
Check the box at the bottom-left to Show all files.

This will show all of the files, including the pdbs.

Just include everything. This will give you the line numbers.
 
Share this answer
 
Hi,

Thanks guys for all your help.

But after much googling i have found one solution. I have used on tool
MZTools V 6.

It has its own custom error line number and using the Erl function i can even get the line number in Release mode without using .pdb file.


Please suggest if you have any better solution.


Thanks and Regards,
Jignesh Patel.
 
Share this answer
 
Hi,

Please try this code. and you get Erl() Number.
VB
   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim dblNum As Double
        Dim buff As Date

100:    On Error GoTo PROC_ERR
110:    dblNum = Rnd(4)

120:    buff = CDate(CStr(dblNum))
130:    Exit Sub

PROC_ERR:
140:    MsgBox("Value: " & dblNum & vbCrLf & _
               "Error Line: " & Erl() & vbCrLf & _
               "Error: (" & Err.Number & ") " & Err.Description, vbCritical)
    End Sub
 
Share this answer
 
Comments
Ralf Meier 28-Jul-15 11:43am    
Very good that you complete a more than 5 years old Thread ...
But I'm not sure that the inquirer needs this help anymore ...
CHill60 29-Jul-15 9:39am    
Reason for my downvote:
Line numbers / labels inserted into code... imagine having to do this to a large production system - then trying to insert lines of code later.
Use of On error goto...in vb.net ... really?
Far, far better approach is detailed in Solution 2 posted over 5 years ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900