0

I am trying to transfer my datatable using UDP but I can't. How can I do that ?

Here is my code :

    void function()
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }

        SqlCommand cmd = new SqlCommand("SELECT * FROM Tablo2 ORDER BY id", conn);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);

        adp.Fill(dtable);

        dtable.TableName = "mydatatable";
        dtable.WriteXml(ms);

        sendBytes = ms.GetBuffer();

        conn.Close();
    }

I am taking the data from an MSSQL database and insert them into my datatable. I want to transfer XML file with UDP and recieve the datatable back in another windows form application.

I need help. Thanks for your help.

alibey
  • 19
  • 5
  • possible duplicate of [Sending and Receiving UDP packets](http://stackoverflow.com/questions/12864999/sending-and-receiving-udp-packets) – Renatas M. Sep 05 '14 at 06:57
  • What did you try so far? This site is not a tutorial service where we provide the source to things you want to do. This site is about helping people with specific problems. "Never tried" is not a specific problem. – nvoigt Sep 05 '14 at 06:58
  • Why UDP? You're broadcasting a database? If you insist I would suggest [0MQ](http://zeromq.org/) to handle the low level details. There's a .NET wrapper available via NuGet. – Zer0 Sep 05 '14 at 07:22
  • possible duplicate of [How can I transfer datatable with UDP in c#?](http://stackoverflow.com/questions/25643295/how-can-i-transfer-datatable-with-udp-in-c) – Daniel Kelley Sep 05 '14 at 07:46
  • @Reniuz i know how to send messages but i couldn't figure out that how can i transfer xml files ? – alibey Sep 05 '14 at 08:00
  • In your example you already have byte array(sendBytes) of xml, you know how to send message...cant figure out where is problem. – Renatas M. Sep 05 '14 at 08:08
  • @Reniuz I can't send xml files. It has a different system. I need to use readxml writexml etc. but i am not familiar with this statements. – alibey Sep 05 '14 at 08:13
  • 1
    Steps you need to do: you write your xml data to ms(with dtable.WriteXml), from ms you getting byte array, you send byte array via UDP, you receive byte array in other application, you convert byte array to string or deserialize to object or do anything you want. Now where is problem. Please be more specific. Dont use I cant, I need...show an example what you already did and in which part of your code is problem. – Renatas M. Sep 05 '14 at 08:26
  • @Reniuz OK. I am going to explain you in a different way. Let say i transfer the xml file. I want to return that byte array to xml again or turn it something that i can show in GridView – alibey Sep 05 '14 at 08:36
  • @Reniuz dtable.WriteXml(ms,XmlWriteMode.WriteSchema); sendBytes = ms.GetBuffer(); client.Send(ms, ms.Length, remoteIPEndPoint); if I do this is the XML file transfer correctly ? – alibey Sep 05 '14 at 08:44
  • Yes it should and in the other app where you read byte array you can do `MemoryStream ms = new MemorySream(yourByteArray); dtable.ReadXml(ms);` – Renatas M. Sep 05 '14 at 09:41
  • Yes, i did that but i got an error something like there is no directory in root.(I don't know what is the exact error in English because error message appears in Turkish.) – alibey Sep 05 '14 at 09:48
  • @Reniuz Yes, i did that but i got an error something like there is no directory in root.(I don't know what is the exact error in English because error message appears in Turkish.) – alibey Sep 05 '14 at 11:53
  • use `string yourxml = Encoding.UTF8.GetString(yourByteArray);` and view xml structure you got received. And about error...well if you dont know so how I suppose to know. :/ – Renatas M. Sep 05 '14 at 12:16
  • @Reniuz thank you for your help. I have another error in this line : client.Send(sendBytes, sendBytes.Length, remoteIPEndPoint); Error is : SocketException is Unhandled. – alibey Sep 05 '14 at 12:40
  • @Reniuz And the other error that i said i don't know is in this line : dtable.ReadXml(ms); and the Error is : XmlException was Unhandled – alibey Sep 05 '14 at 12:42
  • 1
    That's not helping at all. These exceptions are to broad to say what is wrong in your code. You know the idea, you now know how it should work. Now what you need to do is to handle exceptions and make it work and this is your work. I just told you some hints and can't do your work. :) – Renatas M. Sep 05 '14 at 13:02
  • @Reniuz I really appreciate your help. Thank you very much. I have only one last question and that's all. When I use dtable.ReadXml(ms); , Can I you it in dataGridView1.DataSource = dtable; for inserting data from my transferred file ? – alibey Sep 05 '14 at 13:18
  • Yes. dtable.ReadXml is same as Fill. It just fills it from xml. Please use documentation for more information and usage examples. http://msdn.microsoft.com/en-us/library/yfxbc3by(v=vs.110).aspx – Renatas M. Sep 05 '14 at 13:22
  • @Reniuz I will use documentation. Thank you again. – alibey Sep 05 '14 at 13:28

0 Answers0