-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

Hamita
  • 61
  • 7
  • "it's not working" - that doesn't really help. Does it compile? If so, do you get an exception at runtime? If not, how are the results different from what you expect to see? – stuartd Jun 21 '20 at 23:10
  • so what problem are you seeing? – Reg Edit Jun 21 '20 at 23:10
  • Thank you for your reply, it compile very fine without syntax error, but i have an exception when i click the button, and it says: it's is not a json format – Hamita Jun 21 '20 at 23:21
  • Doing a quick eyeball of your C# code, looks to me like that wouldn't run. or perhaps this is the clue. new List() { strFrom, strTo }), Where do strFrom & strTo come from? In the vb version I not you're sending arrays – Hursey Jun 21 '20 at 23:25
  • The original code was working with new List(), i edited it in my post now, thank you – Hamita Jun 21 '20 at 23:30
  • @stuartd I put more information – Hamita Jun 21 '20 at 23:47
  • You need to provide the English translation of those error messages for us. It's not for each and every person who is prepared to volunteer their time to help you to do it for themselves. – jmcilhinney Jun 22 '20 at 00:19
  • Online converters are good enough in some situations but they often lack in various areas to varying degrees. I would recommend downloading Instant VB and/or Instant C# from [Tangible Software Solutions](https://www.tangiblesoftwaresolutions.com/index.html) for the best conversion experience. The free versions have limitations but are enough for most people. – jmcilhinney Jun 22 '20 at 00:21
  • @jmcilhinney Thank you for your reply, i tried the Tangible Software, and it gives me the same error when i click the button, it says the format of entry is incorrect – Hamita Jun 22 '20 at 14:56

1 Answers1

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.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • `TextBox1.Text = "0000000000000000000000000000000000000000000000000000000000000000" ` `TextBox2.Text = "0" ` `TextBox3.Text = Bitcoin adress ` `TextBox4.Text = "6.5" ` It's for creating a coinbase transaction from the bitcoin core – Hamita Jun 22 '20 at 15:10
  • I don't know what a Bitcoin address looks like but in what universe is 6.5 an integer? – jmcilhinney Jun 22 '20 at 15:16
  • Bitcoin adress is a hexadecimal, like this `bc1q5t4sexcufx9ecnepfd4v88j8fvq3q5ymzetmqt` and 6.5 is a double yes – Hamita Jun 22 '20 at 15:25
  • That's not hexadecimal. Valid hexadecimal digits are 0-9 and A-F. Obviously both those values would cause `Convert.ToInt32` to fail. – jmcilhinney Jun 22 '20 at 15:30
  • So, in this case i must convert it to what? i tried `Convert.ToString` and it's giving the same error – Hamita Jun 22 '20 at 15:33
  • In the original C#, the code that sets the `jFromTx` variable passes `txtFromJSON.Text` to both `Convert.ToIn32` calls but you are passing two different values in the VB code, so that doesn't seem right. – jmcilhinney Jun 22 '20 at 15:33
  • *"i must convert it to what"*. If the original code converts to `Int32` then your code must too but it doesn't appear that you're converting the right data. – jmcilhinney Jun 22 '20 at 15:38
  • In the original code `txtFromJSON.Text` is the name of the textbox, in my vb code i didn't change it – Hamita Jun 22 '20 at 15:44
  • You should stop accepting the default names for things as those names are meaningless so who knows whether you've actually used the right ones in the right places? As I already pointed out, you're using two different `TextBoxes` where the original code uses the same one so there seems to be at least one mistake there. Anyway, I've addressed the problem so it's time for you to put some thought into what your code is supposed to do and what it actually does. Seems like you need to get a better understanding of the original code you're copying. I've done all I can. – jmcilhinney Jun 22 '20 at 15:52
  • Thank you very much for your time and for your help, i will t verify every step again and let you know – Hamita Jun 22 '20 at 15:58
  • I verified every textbox, and everything is in it's place, here is the original post: [https://stackoverflow.com/questions/53603131/trouble-posting-createrawtransaction-to-bitcoin-core-via-json-rpc] – Hamita Jun 22 '20 at 17:09
  • I am using a different `Textbox` because the coinbase transaction it's a special transaction with `0` as `vout` – Hamita Jun 22 '20 at 17:14