|
Hello,
I realy like the program the MahendranG made (HostName, IP And MAC Address). The only problem is that I don't know anything about C# and little about VB.NET... the program is in C#. Anyway, I worked on the converion from C# to VB.NET but having some problems. When I run the program and click the Display button I get the following message...
Error: Arithmetic operation resulted in an overflow.
I click the OK button and the program closes. Now, I also get one warnings; Public Property Address() As Long' is obsolete: 'This property has been deprecated. It is address family dependent. Please use IPAddress.Equals method to perform comparisons. But if I change it I get a hole pack of errors. Can anyone please help me with this?
Improts
<br />
Imports Microsoft.VisualBasic<br />
Imports System<br />
Imports System.Drawing<br />
Imports System.Collections<br />
Imports System.ComponentModel<br />
Imports System.Windows.Forms<br />
Imports System.Data<br />
Imports System.Net<br />
Imports System.DirectoryServices<br />
Imports System.Runtime.InteropServices<br />
C#
<br />
private void ButDisplay_Click(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
<br />
this.Status.Text = "Collecting Information...";<br />
<br />
if(this.TxtWorkGroup.Text.Trim() == "")<br />
{<br />
MessageBox.Show("The Work Group name Should Not be Empty");<br />
return;<br />
}<br />
<br />
<br />
DirectoryEntry DomainEntry = new DirectoryEntry("WinNT://" + this.TxtWorkGroup.Text.Trim());<br />
DomainEntry.Children.SchemaFilter.Add("computer");<br />
<br />
<br />
foreach(DirectoryEntry machine in DomainEntry.Children)<br />
{<br />
string[] Ipaddr = new string[3];<br />
Ipaddr[0] = machine.Name;<br />
<br />
System.Net.IPHostEntry Tempaddr = null;<br />
<br />
try<br />
{<br />
Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(machine.Name);<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show("Unable to connect woth the system :" + machine.Name );<br />
continue;<br />
}<br />
System.Net.IPAddress[] TempAd = Tempaddr.AddressList;<br />
foreach(IPAddress TempA in TempAd)<br />
{<br />
Ipaddr[1] = TempA.ToString();<br />
<br />
byte[] ab = new byte[6];<br />
int len = ab.Length;<br />
<br />
int r = SendARP( (int) TempA.Address, 0, ab, ref len );<br />
string mac = BitConverter.ToString( ab, 0, 6 );<br />
<br />
Ipaddr[2] = mac;<br />
} <br />
<br />
System.Windows.Forms.ListViewItem TempItem = new ListViewItem(Ipaddr);<br />
<br />
this.ListHostIP.Items.Add(TempItem);<br />
}<br />
<br />
this.Status.Text = "Displayed";<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.Message,"Error",System.Windows.Forms.MessageBoxButtons.OK );<br />
Application.Exit();<br />
}<br />
<br />
}<br />
VB.NET Conversion
<br />
Private Sub ButDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButDisplay.Click<br />
Try<br />
<br />
Me.Status.Text = "Collecting Information..."<br />
<br />
If Me.TxtWorkGroup.Text.Trim() = "" Then<br />
MessageBox.Show("The Work Group name Should Not be Empty")<br />
Return<br />
End If<br />
<br />
<br />
' Use Your work Group WinNT://&&&&(Work Group Name)<br />
Dim DomainEntry As DirectoryEntry = New DirectoryEntry("WinNT://" & Me.TxtWorkGroup.Text.Trim())<br />
DomainEntry.Children.SchemaFilter.Add("computer")<br />
<br />
<br />
' To Get all the System names And Display with the Ip Address<br />
For Each machine As DirectoryEntry In DomainEntry.Children<br />
'INSTANT VB NOTE: The local variable Ipaddr was renamed since Visual Basic will not uniquely identify class members when local variables have the same name:<br />
Dim Ipaddr_Renamed As String() = New String(2){}<br />
Ipaddr_Renamed(0) = machine.Name<br />
<br />
Dim Tempaddr As System.Net.IPHostEntry = Nothing<br />
<br />
Try<br />
Tempaddr = CType(Dns.GetHostEntry(machine.Name), System.Net.IPHostEntry)<br />
Catch ex As Exception<br />
MessageBox.Show("Unable to connect woth the system :" & machine.Name)<br />
Continue For<br />
End Try<br />
Dim TempAd As System.Net.IPAddress() = Tempaddr.AddressList<br />
For Each TempA As IPAddress In TempAd<br />
Ipaddr_Renamed(1) = TempA.ToString()<br />
<br />
Dim ab As Byte() = New Byte(5){}<br />
Dim len As Integer = ab.Length<br />
<br />
' This Function Used to Get The Physical Address<br />
Dim r As Integer = SendARP(CInt(TempA.Address), 0, ab, len)<br />
Dim mac As String = BitConverter.ToString(ab, 0, 6)<br />
<br />
Ipaddr_Renamed(2) = mac<br />
Next TempA<br />
<br />
Dim TempItem As System.Windows.Forms.ListViewItem = New ListViewItem(Ipaddr_Renamed)<br />
<br />
Me.ListHostIP.Items.Add(TempItem)<br />
Next machine<br />
<br />
Me.Status.Text = "Displayed"<br />
Catch ex As Exception<br />
MessageBox.Show(ex.Message,"Error",System.Windows.Forms.MessageBoxButtons.OK)<br />
Application.Exit()<br />
End Try<br />
<br />
End Sub<br />
|
|
|
|
|
Our Instant VB C# to VB.NET converter produces:
Private Sub ButDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Me.Status.Text = "Collecting Information..."
If Me.TxtWorkGroup.Text.Trim() = "" Then
MessageBox.Show("The Work Group name Should Not be Empty")
Return
End If
' Use Your work Group WinNT://&&&&(Work Group Name)
Dim DomainEntry As DirectoryEntry = New DirectoryEntry("WinNT://" & Me.TxtWorkGroup.Text.Trim())
DomainEntry.Children.SchemaFilter.Add("computer")
' To Get all the System names And Display with the Ip Address
For Each machine As DirectoryEntry In DomainEntry.Children
Dim Ipaddr As String() = New String(2){}
Ipaddr(0) = machine.Name
Dim Tempaddr As System.Net.IPHostEntry = Nothing
Try
Tempaddr = CType(Dns.GetHostByName(machine.Name), System.Net.IPHostEntry)
Catch ex As Exception
MessageBox.Show("Unable to connect woth the system :" & machine.Name)
Continue For
End Try
Dim TempAd As System.Net.IPAddress() = Tempaddr.AddressList
For Each TempA As IPAddress In TempAd
Ipaddr(1) = TempA.ToString()
Dim ab As Byte() = New Byte(5){}
Dim len As Integer = ab.Length
' This Function Used to Get The Physical Address
Dim r As Integer = SendARP(CInt(TempA.Address), 0, ab, len)
Dim mac As String = BitConverter.ToString(ab, 0, 6)
Ipaddr(2) = mac
Next TempA
Dim TempItem As System.Windows.Forms.ListViewItem = New ListViewItem(Ipaddr)
Me.ListHostIP.Items.Add(TempItem)
Next machine
Me.Status.Text = "Displayed"
Catch ex As Exception
MessageBox.Show(ex.Message,"Error",System.Windows.Forms.MessageBoxButtons.OK)
Application.Exit()
End Try
End Sub
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter and VB to C++ converter
Instant J#: VB to J# converter
Clear VB: Cleans up VB.NET code
Clear C#: Cleans up C# code
|
|
|
|
|
hi im very new to vb,in my application i need to read the content of the excel file thro rows.i have tocheck whether the rows are left blank.thanks in advance.
and im using VB6.0
so please help me to get start my code
with regards,
susa
|
|
|
|
|
|
hi Everybody
i am working on window application in vb.net
actually i want to show my form under the visio window means to make visio as parent window for that form, how can i do that .
if anybody know please help me.
Thanks
|
|
|
|
|
You can't. You cannot make your application a child window of another application.
If you want to do something like this, you'll have to write an Add-On for Visio.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
yes i know that it can be done by using set paarent window but it is not working or i used in the wrong way if anyone of you know please help me
Thanks
-- modified at 6:47 Friday 7th April, 2006
|
|
|
|
|
Once again, NO, YOU CAN'T DO THIS!!
You can NOT make your window a child window of another application!
Why? Because when the other application repaints it's own window, your window will never get repainted. This is because your window is not going to get, and can NOT get, the WM_PAINT messages it needs to tell it to repaint at the appropriate time.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
but in visual basic 6.0 i do this several time by using set parent API.
Is this APi doesnot work in vb.nrt
Thanks
|
|
|
|
|
Exactly!
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hey,
Me : Newbie in VS.NET2005
Question :
I have an application in which i define a connectionstring, sqlconnection etc.
then i can use this connection on this form!
But on another form in the same project i need exactly the same DB (Connectionstring,...)
and I don't want to type this twice!
So if my DB changes of server or gets a new name when the appplication goes live, i only have to change this at one place!
I know you can do something like this in ASP.NET
special section in WEB.CONFIG,
But where do you do this for a normal Application!
Thnx
A.
|
|
|
|
|
APP.CONFIG
how vital enterprise application are for proactive organizations leveraging collective synergy to think outside the box and formulate their key objectives into a win-win game plan with a quality-driven approach that focuses on empowering key players to drive-up their core competencies and increase expectations with an all-around initiative to drive up the bottom-line. But of course, that's all a "high level" overview of things
--thedailywtf 3/21/06
|
|
|
|
|
And Where do i find this, how do i use it in my code?
|
|
|
|
|
Go to the Project menu, then click Add New Item. In the window that comes up, find and double-click Application Configuration File. A new file called app.config will be added to your project. Double-click this file to open it in an editor window. You'll see a bunch of XML stuff.
Under the <configuration> tag, add something that looks like this:
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="myConnectionString" value="your connection string goes here!" />
</appSettings>
.
.
.
Now, to get this connection string from anywhere inside your app, just use:
Imports System.Configuration
.
.
.
Dim connString As String = ConfigurationSettings.AppSettings("myConnectionString")
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi
I want to dynamically cast to whatever type obj is. So I want to change the Boolean part to something like obj.GetType().
obj = CType(node.SelectSingleNode(name).InnerText, Boolean)
|
|
|
|
|
That's not going to work since there is no way to determine what the object type is from it's XML text representation. For example:
<xml version="1.0" encoding="utf-8" ?>
<ComplexNumber>
<R>3</R>
<I>5</I>
</ComplexNumber>
"3" can be anything. So you'll have to determine what that is SUPPOSED to be ahead of time before you attempt a conversion. Is that "3" really a string value containing a 3 character? Is it an Integer? Is it supposed to be a Double or a Decimal? How about a Byte? There's just no way to determine that.
You have to know ahead of time what that type is before you call CType, or any other conversion method, so you can supply the appropriate type name in the CType call. One method of doing this would be to attribute your XML elements with the Type name of the value, like this:
<xml version="1.0" encoding="utf-8" ?>
<ComplexNumber>
<R typename="System.Integer">3</R>
<I typename="System.Integer">5</I>
</ComplexNumber>
Retrieve the value of the typename attribute, then pass it into a conversion like this:
obj = Convert.ChangeType(xmldoc.SelectSingleNode(name).InnerText, Convert.GetTypeCode(typename))
But, you have another problem. What is defining obj ??? Anything you return from this line of code will get boxed, if possible, by the Type that is obj , like if obj was defined as Object . This is a nice little performance hit if you're doing this many times...
Dave Kreskowiak
Microsoft MVP - Visual Basic
-- modified at 16:05 Thursday 6th April, 2006
|
|
|
|
|
Hi Dave,
Thanks for the response. My question was probably not very clear. What I want to do is not cast using the xml innertext type but the obj(name) type.
I know this looks wired and I guess it is. What I’m doing (and I know when it comes to performance it is just wrong) is to map from the xml node to a property in the object using the name of the properties. Now it becomes even worse, I use reflection to get the properties names and I invoke them instead of setting the normal way. I know this is extremely inefficient in performance terms, but it is very dynamic and I like the simplicity of the code.
So what happens is:
1) An XML node is read
2) Loop over the child nodes
3) Set the object (Query) properties to the node value with the same name. (Dynamically casting to the property’s type)
Here is the function that does the trick.
<br />
'Builds a query from a xml node<br />
Private Function BuildQuery(ByRef node As XmlNode) As Query<br />
<br />
Try<br />
Dim obj As New Query<br />
<br />
'Map xmlnodes to properties where the names match<br />
For Each name As String In obj.Properties<br />
If Not node.SelectSingleNode(name) Is Nothing Then<br />
<br />
'Dynamically cast to the type of the property <br />
Dim code As Integer = CInt(Convert.GetTypeCode(obj(name)))<br />
obj(name) = Convert.ChangeType(node.SelectSingleNode(name).InnerText, code)<br />
<br />
End If<br />
Next<br />
<br />
Return obj<br />
Catch ex As Exception<br />
Throw New ApplicationException("Falied to build Databasequery object", ex)<br />
End Try<br />
End Function<br />
On a side note I only use this rarely in the code, for reading data into object and moving data from object into file or database. These are expensive operations anyway so the overhead is not that great compared to the actual workload. That is at least the theory, but I need to do some test to check if it is correct.
Thank you very much for your reply.
Johan Hertz
|
|
|
|
|
I guess I didn't understand what you were asking the first time around. I'm happy that you found a way to do it! I, personally, have never had the need to do something like this...
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi Dave,
To honest, I don’t really “need” to do it in this way either. But it is a nice little exercise, and I think it might even be usefully
Once again thanks for your help, it was very appreciated.
|
|
|
|
|
hi friends
i am working in vb6, i have a main form which contains a menu and deferent menu item liek add edit delte etc i want when i click menu itemm Add a form will open on it, i di dnot now how i can do this help me plz
regards
Farooq Azam
|
|
|
|
|
Handle the Click event of the menu item, then just create a new instance of your Add form and show it. Since this is in response to Adding something to a document, you'll probably want to show the form Modally.
Dim addForm As New MyAddForm
addForm.Show vbModal, Me
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
thanks friend Dave i will need ur help in future too regards bye
Farooq Azam
|
|
|
|
|
can anybody send me the code how to insert data into database using my form textbox values.
Aavesh
Aavesh
|
|
|
|
|
What database are you using?
|
|
|
|
|
i'm using mysql please give me the code i'm getting frustrated
thanx
|
|
|
|