After converting from csharp to vb when using an online converter

-2

The csharp code works perfectly but when i convert it, it's not working, i dont know what i am doing wrong. It compiles, but when i click the button to call the function, it gives me the error below,

Help will be appreciated, thank you.

Csharp code:

private void button1_Click(object sender, EventArgs e)
{
    JContainer jArray = new JArray();

    JObject jFromTx = new JObject
    {
        { "txid", data["result"][Convert.ToInt32(txtFromJSON.Text)]["txid"] },
        { "vout", data["result"][Convert.ToInt32(txtFromJSON.Text)]["vout"] }
    };

    jArray.Add(jFromTx);

    JObject jToTx = new JObject
    {
        { Convert.ToString(data["result"][Convert.ToInt32(txtToJSON.Text)]["address"]), Convert.ToDouble(txtAmountToSpend.Text) }
    };

    JContainer jArray2 = new JArray
    {
        jToTx
    };

   
    data = JObject.Parse(RequestServer("createrawtransaction", new List<JToken>() {  jArray, jArray2 }));
}

My code:

        Dim data As New JObject

        Dim jArray As JContainer = New JArray()

        Dim jToTx As JObject = New JObject From {{Convert.ToString(data("result")(Convert.ToInt32(TextBox1.Text))("address")), Convert.ToDouble(TextBox2.Text)}}

        Dim jArray2 As JContainer = New JArray From {jToTx}

        Dim jFromTx As JObject = New JObject From {{"txid", data("result")(Convert.ToInt32(TextBox3.Text))("txid")}, {"vout", data("result")(Convert.ToInt32(TextBox4.Text))("vout")}}

        jArray.Add(jFromTx)

        data = JObject.Parse(RequestServer("createrawtransaction", New List(Of JToken) From {jArray, jArray2}))


        TextBox22.Text = data

EDIT: My RequestServer function is:

 Public Shared Function RequestServer(ByVal methodName As String, ByVal parameters As List(Of JToken)) As JToken
        Dim ServerIp As String = "http://localhost:8332"
        Dim UserName As String = "hama"
        Dim Password As String = "hama"
        Dim webRequest As HttpWebRequest = CType(webRequest.Create(ServerIp), HttpWebRequest)
        webRequest.Credentials = New NetworkCredential(UserName, Password)

        webRequest.ContentType = "application/json-rpc"
        webRequest.Method = "POST"


        Dim respVal As String = String.Empty
        Dim joe As JObject = New JObject
        joe.Add(New JProperty("jsonrpc", 1))
        joe.Add(New JProperty("id", 1))
        joe.Add(New JProperty("method", methodName))
        Dim props As JArray = New JArray
        For Each parameter In parameters
            props.Add(parameter)
        Next
        joe.Add(New JProperty("params", props))
        ' serialize json for the request
        Dim s As String = JsonConvert.SerializeObject(joe)
        Dim byteArray() As Byte = Encoding.UTF8.GetBytes(s)
        webRequest.ContentLength = byteArray.Length
        Dim dataStream As Stream = webRequest.GetRequestStream
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim streamReader As StreamReader = Nothing
        Try
            Dim webResponse As WebResponse = webRequest.GetResponse
            streamReader = New StreamReader(webResponse.GetResponseStream, True)
            respVal = streamReader.ReadToEnd
            Dim data = JsonConvert.DeserializeObject(respVal).ToString
            Return data
        Catch exp As Exception

        Finally
            If (Not (streamReader) Is Nothing) Then
                streamReader.Close()
            End If

        End Try

        Return String.Empty
    End Function

The error code that visual studio show, i paste it below:

System.FormatException
  HResult=0x80131537
  Message=Le format de la chaîne d'entrée est incorrect.
  Source=mscorlib
  Arborescence des appels de procédure :
   à System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   à System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   à System.Convert.ToInt32(String value)
   à WindowsApp3.Form1.Button3_Click(Object sender, EventArgs e) dans C:\Users\Hama\source\repos\WindowsApp3\WindowsApp3\Form1.vb :ligne 1287
   à System.Windows.Forms.Control.OnClick(EventArgs e)
   à System.Windows.Forms.Button.OnClick(EventArgs e)
   à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   à System.Windows.Forms.Control.WndProc(Message& m)
   à System.Windows.Forms.ButtonBase.WndProc(Message& m)
   à System.Windows.Forms.Button.WndProc(Message& m)
   à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   à WindowsApp3.My.MyApplication.Main(String[] Args) dans  :ligne 81

  Cette exception a été levée à l'origine dans cette pile des appels :
    System.Number.StringToNumber(string, System.Globalization.NumberStyles, ref System.Number.NumberBuffer, System.Globalization.NumberFormatInfo, bool)
    System.Number.ParseInt32(string, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)
    System.Convert.ToInt32(string)
    WindowsApp3.Form1.Button3_Click(Object, System.EventArgs) dans Form1.vb
    System.Windows.Forms.Control.OnClick(System.EventArgs)
    System.Windows.Forms.Button.OnClick(System.EventArgs)
    System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs)
    System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int)
    System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message)
    System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message)
    ...
    [Pile des appels tronqué]

Here is the original post: Trouble posting CREATERAWTRANSACTION to Bitcoin Core via JSON-RPC

vb.net
asked on Stack Overflow Jun 21, 2020 by Hamita • edited Jun 22, 2020 by Hamita

1 Answer

0

The exception is telling you that the text you're passing to Convert.ToIn32 does not represent a number. There are three calls to that method in your code so it's not clear which one is at fault but at least one of TextBox1.Text, TextBox3.Text and TextBox4.Text does not represent a number. This is why you need to validate user input.

answered on Stack Overflow Jun 22, 2020 by jmcilhinney

User contributions licensed under CC BY-SA 3.0