dcsimg
Results 1 to 13 of 13

Thread: Still working on this project (new to c#) Streamreader to datatable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    70

    Still working on this project (new to c#) Streamreader to datatable

    I finally have data from an HttpWebResponse into a streamreader. I can write it to the console and it shows data so I know I have data. I have been searching on how to get that data into a datatable or even to a file which then I can load it into a datatable but can't find anything in google. Any help would be much appreciated.

    Thanks

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,039

    Re: Still working on this project (new to c#) Streamreader to datatable

    You have posted in the CodeBank forum, which is for sharing working code snippets, not for asking questions. I have asked the mods to move this thread to the C# forum. Please don't post a duplicate thread before they do that.

    In the meantime, perhaps you could post the relevant code that you already have and point out where in the process you are stuck. If you have a StreamReader then you can call ReadLine to get a String for each line. That is just like any other String, so breaking it up into parts will, in general, be like for any other String. If you're having specific issues doing that then we need specific details of that. Adding multiple substrings to a DataRow and that to a DataTable is undoubtedly something you could find information for on the web. Most likely you are searching for too broad a subject that is actually a combination of subjects that is specific too you rather than each individual subject that is probably well-covered.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,210

    Re: Still working on this project (new to c#) Streamreader to datatable

    Thread moved from the 'CodeBank C#' forum (which is for you to post working code examples, not questions) to the 'C#' forum

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    70

    Re: Still working on this project (new to c#) Streamreader to datatable

    I think you are correct. Here is the part of the code I am having a problem with

    using (var requestStream = selectRequest.GetRequestStream())
    {
    requestStream.Write(jsonArray, 0, jsonArray.Length);
    }
    // Executing HTTP request and getting reply from server.
    using (var response = (HttpWebResponse)selectRequest.GetResponse())
    {
    // Displaying reply in console.
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
    Console.WriteLine(reader.ReadToEnd());

    //write the file to disk
    string path = "C:\\Temp\\bpm.txt";
    using (StreamWriter outputfile = new StreamWriter(path))
    {
    outputfile.Write(reader.ReadToEnd());
    //outputfile.Write(content);
    }

    //get the data into a datatable
    var dt = new DataTable();



    }
    }

    The console.writeline(reader.ReadToEnd()) works fine. Infact the if I change it to Console.write(reader.readline()), it does the same thing. Hundreds of line in the console.
    When I issue the StremWriter, it comes out blank so therefor the datatable would be blank also. I have tries several other combinations to no avail. An excerpt of what the console looks like is this:

    {"rowConfig":{"Id":{"dataValueType":0},"Amount":{"dataValueType":5,"precision":2},"Probability":{"da taValueType":4},"Stage":{"dataValueType":10,"isLookup":true,"referenceSchemaName":"OpportunityStage" },"Account":{"dataValueType":10,"isLookup":true,"referenceSchemaName":"Account","primaryImageColumnN ame":"AccountLogo"},"Contact":{"dataValueType":10,"isLookup":true,"referenceSchemaName":"Contact","p rimaryImageColumnName":"Photo"}},"rows":[{"Amount":0.00,"Probability":0,"Stage":{"value":"a9aafdfe-2242-4f42-8cd5-2ae3b9556d79","displayValue":"Closed lost","primaryImageValue":""},"Account":{"value":"c5e852b1-7902-4a59-9bd2-4d7753644080","displayValue":"Dematic","primaryImageValue":""},"Contact":"","Id":"d4c3b8c3-75e7-493d-abda-61f35a75c3a5"},{"Amount":0.00,"Probability":0,"Stage":{"value":"a9aafdfe-2242-4f42-8cd5-2ae3b9556d79","display

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,210

    Re: Still working on this project (new to c#) Streamreader to datatable

    Quote Originally Posted by BSWhipp View Post
    When I issue the StremWriter, it comes out blank so therefor the datatable would be blank also. I have tries several other combinations to no avail.
    Try removing (or commenting out) the line: Console.WriteLine(reader.ReadToEnd());

    If you do need to both display it and write it to the file, store the result in a variable and then display+write the contents from the variable.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    70

    Re: Still working on this project (new to c#) Streamreader to datatable

    Like this?

    string results = reader.ReadLine();
    using (StreamWriter outputfile = new StreamWriter(path))
    {
    outputfile.Write(results);

    Ultimately I want to get it into a datatable for each field. Then I can import it into a SQL table.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,210

    Re: Still working on this project (new to c#) Streamreader to datatable

    Quote Originally Posted by BSWhipp View Post
    Like this?
    Yes.
    Ultimately I want to get it into a datatable for each field. Then I can import it into a SQL table.
    As the data appears to be JSON, use a JSON parser... the one from Newtonsoft is often recommended, and can apparently be added to your project via NuGet.

    I don't have any examples handy, but you should be able to pass the contents of the result variable into the parser to have it parse it all.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    70

    Re: Still working on this project (new to c#) Streamreader to datatable

    OK, I have newtonsoft already referenced but don't know how to use it. I will try to google it. But as I am know to this, the result are in JSON? I will google JSON parser to datatable?

    I really thank you for this input. The company I am working with to get this data is not quite forthcoming.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    70

    Re: Still working on this project (new to c#) Streamreader to datatable

    Having an issue converting to the datatable.

    This is the code:
    var json = new JavaScriptSerializer().Serialize(selectQuery);

    // Converting a JSON object string to a byte array.
    byte[] jsonArray = Encoding.UTF8.GetBytes(json);
    // Creating an instance of HTTP request.
    var selectRequest = HttpWebRequest.Create(selectQueryUri) as HttpWebRequest;
    // Defining request method.
    selectRequest.Method = "POST";
    // Defining request content type.
    selectRequest.ContentType = "application/json";
    // Adding earlier received authentication cookies to a data fetch query.
    selectRequest.CookieContainer = AuthCookie;
    // Set length for request content.
    selectRequest.ContentLength = jsonArray.Length;

    // Putting BPMCSRF token to the request header.
    CookieCollection cookieCollection = AuthCookie.GetCookies(new Uri(authServiceUri));
    string csrfToken = cookieCollection["BPMCSRF"].Value;
    selectRequest.Headers.Add("BPMCSRF", csrfToken);

    // Placing JSON object to request content.
    using (var requestStream = selectRequest.GetRequestStream())
    {
    requestStream.Write(jsonArray, 0, jsonArray.Length);
    }
    // Executing HTTP request and getting reply from server.
    using (var response = (HttpWebResponse)selectRequest.GetResponse())
    {
    //get the data into a datatable
    var dt = new DataTable();
    dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
    }

    Getting error

    An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

    Additional information: Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.

    When running rthe JSonConvert line.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,210

    Re: Still working on this project (new to c#) Streamreader to datatable

    The lack of Code tags around your code makes it extremely hard to read, but from the few lines before the end it seems that you aren't using the results variable (or an equivalent), so presumably what are you passing to DeserializeObject isn't the right thing.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    70

    Re: Still working on this project (new to c#) Streamreader to datatable

    I can believe I can't find anything on the internet that can help me. It looks like I will have to parse this out manually which I can do but JSon should (and probably does) do it or at least there should be more documentation on it so I can learn.

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,210

    Re: Still working on this project (new to c#) Streamreader to datatable

    Well you seemed to have the right thing for the first part of the process (getting the json text) back in post #6, and you seemed to take a step backwards in post #9.

    Once you have the json text (which will be in the variable results if you go back to what you had in post #6), you can then feed that into the parser.

    A simple search of this site (using the keywords: Newtonsoft datatable) gave several threads, the first of which looks to be useful to you:
    http://www.vbforums.com/showthread.p...o-DataGridView
    While it is VB code, the equivalent C# code will be virtually identical.

    If that also doesn't work for you, try not going directly to a datatable (as that may be what is causing the problem), and instead just try using a simpler parsing option (if you don't know how, search for examples - with a popular library like that, there will be many).

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    70

    Re: Still working on this project (new to c#) Streamreader to datatable

    What I ended up doing is manually creating a parser. It took about 20 lines of code but it works great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width