c# to vb.net

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1907 Views - Last Post: 28 October 2014 - 06:23 AM

#1 Neph1x   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 19-November 11

c# to vb.net

Posted 27 October 2014 - 03:46 PM

Hey everyone, i need a favor.
Does anybody can translate me this code from C# to VB.NET, this is code for file upload to webserver via PHP.

    
 {
            try
            {
                WebClient Client = new WebClient();
                foreach (string file in selectedFile)
                {
                    Client.Headers.Add("Content-Type", "binary/octet-stream");
                    byte[] result = Client.UploadFile(@"URL", "POST", file);
                }
            }
            catch
            {
                MessageBox.Show("Error");
            }
        }



Thank you!

This post has been edited by Neph1x: 27 October 2014 - 03:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: c# to vb.net

#2 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 218
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: c# to vb.net

Posted 27 October 2014 - 03:53 PM

Here is the output:

Try
	Dim Client As New WebClient()
	For Each file As String In selectedFile
		Client.Headers.Add("Content-Type", "binary/octet-stream")
		Dim result As Byte() = Client.UploadFile("URL", "POST", file)
	Next
Catch
	MessageBox.Show("Error")
End Try



There are a lot of online tools where you can convert C# to VB.NET and viceversa, such as this one.

This post has been edited by rusoaica: 27 October 2014 - 03:53 PM

Was This Post Helpful? 1
  • +
  • -

#3 Neph1x   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 19-November 11

Re: c# to vb.net

Posted 27 October 2014 - 03:55 PM

Tried, don't work very well. So this is reason why i am asking you for help.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: c# to vb.net

Posted 27 October 2014 - 03:57 PM

"Doesn't work" is meaningless to us. Do you get errors? Are you at all familiar with either language or are you just copy/pasting other people's code in order to cobble together an application, but not really familiar with programming?
Was This Post Helpful? 0
  • +
  • -

#5 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 218
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: c# to vb.net

Posted 27 October 2014 - 03:58 PM

What is the actual error? In what way is not working as expected?
Was This Post Helpful? 0
  • +
  • -

#6 Neph1x   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 19-November 11

Re: c# to vb.net

Posted 27 October 2014 - 04:02 PM

Yes i am familiar with VB.NET, and this code is from my teacher.
Code works fine, no errors. But when i'm trying to upload image to server i get exception error. VB.NET Code

Catch
    MessageBox.Show("Error")




But when i am trying to upload with C# code to web server everything works fine.
Was This Post Helpful? 0
  • +
  • -

#7 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 218
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: c# to vb.net

Posted 27 October 2014 - 04:03 PM

And what is the actual error message?

You should use

Catch ex As Exception
	MessageBox.Show(ex.ToString())


This post has been edited by rusoaica: 27 October 2014 - 04:04 PM

Was This Post Helpful? 1
  • +
  • -

#8 Yips   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 64
  • Joined: 11-October 14

Re: c# to vb.net

Posted 27 October 2014 - 04:03 PM

Try 
    Dim Client As WebClient = New WebClient
    For Each file As String In selectedFile
        Client.Headers.Add("Content-Type", "binary/octet-stream")
        Dim result() As Byte = Client.UploadFile("URL", "POST", file)
    Next
Catch  As System.Exception
    MessageBox.Show("Error")
End Try


Try this. Define what the catch is doing.
Was This Post Helpful? 0
  • +
  • -

#9 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 218
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: c# to vb.net

Posted 27 October 2014 - 04:07 PM

View PostYips, on 28 October 2014 - 01:03 AM, said:

Catch As System.Exception


catch WHAT as exception? You need a variable there, and also display that variable. Its pointless to do this:

Catch ex As System.Exception
   MessageBox.Show("Error")



because that doesn't output the error, but a simple string.

This post has been edited by rusoaica: 27 October 2014 - 04:08 PM

Was This Post Helpful? 1
  • +
  • -

#10 Neph1x   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 19-November 11

Re: c# to vb.net

Posted 27 October 2014 - 04:20 PM

Error: "An exception occurred during a WebClient request. - URL is 100% corrent
Was This Post Helpful? 0
  • +
  • -

#11 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 218
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: c# to vb.net

Posted 27 October 2014 - 04:24 PM

And what is that exception? Look for all the information you can give us, look also at the Exception.InnerException property, etc; "an exception occurred" is too vague to actually understand what the error is, what causes it and why.
Was This Post Helpful? 1
  • +
  • -

#12 Neph1x   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 19-November 11

Re: c# to vb.net

Posted 27 October 2014 - 04:40 PM

Ohh sorry for that..
Maybe this will be better.

Error Image
Was This Post Helpful? 0
  • +
  • -

#13 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 218
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: c# to vb.net

Posted 27 October 2014 - 04:46 PM

Isn't the error obvious enough? "FileNotFoundException: Could not find file etc etc"

Put a breakpoint inside the loop and check what the value of "file" variable is.
Was This Post Helpful? 1
  • +
  • -

#14 Neph1x   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 19-November 11

Re: c# to vb.net

Posted 27 October 2014 - 04:59 PM

I think that 'file' variable needs to be in my case image location. Because 'file' variable starts with "C" but this is all.
Real image location is "C:\Users\user\Pictures\profil.jpg"

Breakpoint
Was This Post Helpful? 0
  • +
  • -

#15 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 218
  • View blog
  • Posts: 689
  • Joined: 10-March 12

Re: c# to vb.net

Posted 27 October 2014 - 05:09 PM

when you use a For loop like that, you use it when you have a collection of values, and you need to take it one by one. If variable SelectedFile is a simple string containing a path, you should redesign your code like this:

Try
	Dim Client As New WebClient()
	Client.Headers.Add("Content-Type", "binary/octet-stream")
	Dim result As Byte() = Client.UploadFile("URL", "POST", selectedFile)
Catch ex As Exception
	MessageBox.Show(ex.ToString())
End Try


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2