|
You haven't shown the code you're using to generate the JSON.
At a guess, you'll need to make your emp property a Dictionary<string, object> :
emp = new Dictionary<string, object>
{
["Country"] = "USA",
["Grade"] = "2B",
["abc"] = 155,
["abcd"] = 200,
["abcde"] = 300,
},
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
So, I would actively discourage this approach. If your domain model looks like that, there is no reason the JSON representation of your domain model shouldn't as well. Such a structure will also result in downstream problems when you make changes at the source level.
If you absolutely need to do this, though, you can implement a new JsonConverter for your object. Have a look at:
Custom JsonConverter
Custom JsonConverter<T>
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|
|
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string site = (row.FindControl("txtSite") as TextBox).Text;
string fake = (row.FindControl("txtFake") as TextBox).Text;
string query = "UPDATE Sites SET Sites=@Site, Fake=@Fake WHERE id=@id";
string constr = "Data Source=LAPTOP-01LEC0RG;Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=BlackList;User Id=sa;Password=blacklist;Connect Timeout=30;";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@id", Id);
cmd.Parameters.AddWithValue("@Site", site);
cmd.Parameters.AddWithValue("@fake", fake);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
This is my code. my update option does not function when i click edit.
|
|
|
|
|
What does happen? Have you traced the code with the debugger, and why do you not check the return value from the ExecuteNonQuery ?
|
|
|
|
|
Don't connect to your database as sa . That is an unrestricted account which could be used to destroy your server. Instead, use a specific account which has only the permissions required by your application.
Also, avoid hard-coding the connection string. Store it in a configuration file:
Connection Strings and Configuration Files | Microsoft Docs[^]
And if that's your real sa password that you've just posted to a public forum, then you should change it ASAP!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello.
Sorry in advance for the long post.
We currently have a c# console application that pulls medical claims from our client via a web service. Our application doesn't handle multi-threading at the moment. Unfortunately, we've recently been told by the client that we're going to start receiving millions of claims all at once (as opposed to the typical few thousand per day) & after doing the math, we've determined it would take weeks for this application to process that many claims, so we thought introducing multi-threading into the application would help. However, this raised a question of a potential issue: if it's multi-threaded, how do we ensure no claims get lost in the mix and never pulled?
Our current process is as follows:
1) We pull an initial list of all claim id's from the client. This is a one-time pull and we store the list.
2) We then read off the next claim from that list & call their web service for the details of that claim. We do this one claim at a time, until we've finished pulling the details of every claim from that list.
3) Before pulling each claim, we store the value of the claim id we're trying to process into a "Last Claim ID Processed" field in our db table, in case there's ever a system crash and we need to pick up later with the claim id where we left off with. This way, our application just basically says "ok, I see there was a failure last time we ran. Lets start off with claim id#12345 rather than starting from the very beginning of the list"
4) Finally, we store that claim info in our database and move onto the next claim.
The bulk of our time is at step #2, where we process each claim, so this is the piece we want to split into multi-threading. Where we can split this process into about 10 threads and each one can asynchronously read the next claim id from the claim list that we pulled in step #1. Theoretically, that should make our application process claims 10 times faster than it currently is. But my problem is how would each thread know which other claims are already in the midst of being processed?
So again, my concern is, how do we ensure no claims get lost in the mix? For example, I'm concerned about the following scenario:
1) Thread #1 reads from the claim list & pulls the next claim. Claim id 10001. Before processing, it stores the value of this claim id into our "Last Claim ID Processed" field in our import summary table, to let us know it's the last claim processed in case of a crash (step #3 above)
2) Thread #2 reads from the list & pulls claim id 10002 & repeats the same process
3) Thread #3 reads from the list & pulls claim id 10003 & repeats the same process
4) Thread #1 finishes processing claim id 10001 & starts pulling claim id 10004. And again, stores that claim id value into the import summary table.
5) Thread #3 crashes and never finishes pulling claim id 10003...
In the above scenario, we have a big problem! Claim 10003 crashes but we'll never know because thread #1 has already overwritten the "Last Claim ID processed" field with 10004.
If you've stuck with me this long, I thank you (again, sorry for the long post). But I guess I'm just looking for a little advice on the best way to handle this new multi-threading process we want to integrate. I need to be VERY careful that no claims end up lost in the mix.
Thanks in advance for any advice you can provide!
|
|
|
|
|
Hi,
When things get handled out of order, as in multi-threading, you can't handle all possible mishaps with a single "Last Claim ID Processed". What you need is a list/table of claims where each claim has a "status" field, so you can have any number of threads looking for and dealing with unhandled claims. And you will want at least three possible statuses: new, started, and done. (Obviously done would not be needed if a claim would be removed when done processing).
modified 15-Nov-18 17:19pm.
|
|
|
|
|
Yesterday I asked this same question. Today I want to follow up on it
First, some clarification... I'm working on a Xamarin Forms app. The app will only be able to run INSIDE our VPN.
Could I just check "System.Environment.UserDomainName" to see if returns the right domain name?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
Tried that.. I VPN in on my phone, run the Android app in VS2017, the app starts on my phone, and Environment.UserDomainName always returns "localhost".
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
That's pretty much what the Remarks section in the MSDN page says will happen. When I run it on my PC it returns my PC name.
|
|
|
|
|
What I'm trying to accomplish is to determine if the app is being run on our network. The users will VPN in, then run the app. I need to check that their connected before continuing to let app load.
I've also tried "Dns.GetHostAddresses("www.the_domain.com").FirstOrDefault()". This returns a 192.168.... address.
Any idea how to check if the user is VPN's in from an Android app?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
As far as I am aware, this will always return the address related to the system on which the app is running. It's a while since I did this for real.
|
|
|
|
|
|
hi kevin,
you can use IP api to get the isp name,
if the user is connected to VPN then ip will match with the ISP
e.g. https://ipinfo.io/
=====================================================
The grass is always greener on the other side of the fence
|
|
|
|
|
Can someone help how I can also copy user's desktop folder's subfolders and their files with the code below this text?
For moment I can only copy files from \" + computer + "\c$\Users\Desktop folder but not the subfolders and their files.
string[] dirs = Directory.GetDirectories("\\\\" + computer + "\\c$\\Users\\");
foreach (string item in dirs)
{
FileInfo f = new FileInfo(item);
user2 = "\\\\" + computer + "\\c$\\Users\\"+ f.Name + "\\Desktop";
if (Directory.Exists(user2))
{
user = "C:\\Backup\\" + computer + "\\" + f.Name + "\\Desktop";
Directory.CreateDirectory(user);
DirectoryInfo folder = new DirectoryInfo(user2);
FileInfo[] files = folder.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(user, file.Name);
file.CopyTo(temppath, true);
}
}
modified 15-Nov-18 12:00pm.
|
|
|
|
|
What exactly is happening, and why not just use "C:\Users\ ..."?
|
|
|
|
|
Hi,
what you need is a recursive method (say DeepCopyDir()) that copies an entire folder by:
(1) copying its files, using Directory.GetFiles and File.Copy for each of them;
(2) copying its subfolders, using Directory.GetDirectories and DeepCopyDir for each of them.
|
|
|
|
|
Hi all, first of all I'm a novice when it comes to parsing Json , I'm attempting ( with some success ) to get information from the OED API, this is my first attempt
using System;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
namespace OxfordDictionary
{
class Program
{
static HttpWebRequest req = null;
static string PrimeUrl = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/";
static string uri = PrimeUrl + "crossword";
static string theJson = "";
static HttpWebResponse HWR_Response = null;
static Stream respStream = null;
static StreamReader sr = null;
static void Main(string[] args)
{
req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Headers.Add("app_id", "my id");
req.Headers.Add("app_key", "app key");
req.Method = WebRequestMethods.Http.Get;
req.Accept = "application/json";
HWR_Response = (HttpWebResponse)req.GetResponse();
respStream = HWR_Response.GetResponseStream();
sr = new StreamReader(respStream, Encoding.UTF8);
theJson = @sr.ReadToEnd();
sr.Close();
var results = JObject.Parse(theJson)["results"];
string value = "";
string name = "";
string output = "";
foreach (JObject o in results.Children<JObject>())
{
foreach (JProperty p in o.Properties())
{
name = p.Name;
foreach (var v in p.Values())
{
value = v.ToString();
output += value;
}
}
}
Console.WriteLine(output);
Console.ReadKey();
}
}
}
which returns
crossword{
"entries": [
{
"etymologies": [
"said to have been invented by the journalist Arthur Wynne, whose puzzle (called a ‘word-cross’) appeared in a Sunday newspaper, the New York World, on 21 December 1913"
],
"grammaticalFeatures": [
{
"text": "Singular",
"type": "Number"
}
],
"homographNumber": "000",
"senses": [
{
"definitions": [
"a puzzle consisting of a grid of squares and blanks into which words crossing vertically and horizontally are written according to clues"
],
"domains": [
"Games"
],
"examples": [
{
"text": "she settled down to do the crossword"
}
],
"id": "m_en_gbus0236440.007",
"short_definitions": [
"puzzle consisting of grid of squares and blanks into which words crossing vertically and horizontally are written according to clues"
]
}
],
"variantForms": [
{
"text": "crossword puzzle"
}
]
}
],
"language": "en",
"lexicalCategory": "Noun",
"pronunciations": [
{
"audioFile": "<a href="http://audio.oxforddictionaries.com/en/mp3/crossword_gb_1_8.mp3">http://audio.oxforddictionaries.com/en/mp3/crossword_gb_1_8.mp3</a>",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "ˈkrɒswəːd"
}
],
"text": "crossword"
}headwordcrossword
the bit I'm interested in is
"definitions": [
"a puzzle consisting of a grid of squares and blanks into which words crossing vertically and horizontally are written according to clues"
I've tried googling but can't find anything to help me, so my question is ? how can I parse definitions[0] ?
We can’t stop here, this is bat country - Hunter S Thompson RIP
modified 15-Nov-18 10:03am.
|
|
|
|
|
The first thing to note is that there are three problems with the JSON data:
1) Remove the introductory "crossword"
2) Remove the trailing "headwordcrossword"
3) Escape the double quotes in the "audiofile" href:
"audioFile": "<a href="http://audio.oxforddictionaries.com/en/mp3/crossword_gb_1_8.mp3">http://audio.oxforddictionaries.com/en/mp3/crossword_gb_1_8.mp3</a>", To:
"audioFile": "<a href=\"http://audio.oxforddictionaries.com/en/mp3/crossword_gb_1_8.mp3\">http://audio.oxforddictionaries.com/en/mp3/crossword_gb_1_8.mp3</a>",
When you do that, it'll parse fine.
There is an online JSON to C# class converter I use: json2csharp - generate c# classes from json[^] which gives me these classes:
public class GrammaticalFeature
{
public string text { get; set; }
public string type { get; set; }
}
public class Example
{
public string text { get; set; }
}
public class Sens
{
public List<string> definitions { get; set; }
public List<string> domains { get; set; }
public List<Example> examples { get; set; }
public string id { get; set; }
public List<string> short_definitions { get; set; }
}
public class VariantForm
{
public string text { get; set; }
}
public class Entry
{
public List<string> etymologies { get; set; }
public List<GrammaticalFeature> grammaticalFeatures { get; set; }
public string homographNumber { get; set; }
public List<Sens> senses { get; set; }
public List<VariantForm> variantForms { get; set; }
}
public class Pronunciation
{
public string audioFile { get; set; }
public List<string> dialects { get; set; }
public string phoneticNotation { get; set; }
public string phoneticSpelling { get; set; }
}
public class RootObject
{
public List<Entry> entries { get; set; }
public string language { get; set; }
public string lexicalCategory { get; set; }
public List<Pronunciation> pronunciations { get; set; }
public string text { get; set; }
} Add those to your app, and try Newtonsoft JSOn parser - it should be a single line of code! Json.NET - Newtonsoft[^]
Give it a try - it normally works very nicely for me!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Cheers Paul
We can’t stop here, this is bat country - Hunter S Thompson RIP
|
|
|
|
|
 Hi Paul , very cool class generator but I'm having problems deserialisingn the Json ( all members are null ), the output I show here is not accurate as no href or other tags are present ( maybe it's this site adding them but they are not present in the returned text) so ignore them
{
"metadata": {
"provider": "Oxford University Press"
},
"results": [
{
"id": "crossword",
"language": "en",
"lexicalEntries": [
{
"entries": [
{
"etymologies": [
"said to have been invented by the journalist Arthur Wynne, whose puzzle (called a ‘word-cross’) appeared in a Sunday newspaper, the New York World, on 21 December 1913"
],
"grammaticalFeatures": [
{
"text": "Singular",
"type": "Number"
}
],
"homographNumber": "000",
"senses": [
{
"definitions": [
"a puzzle consisting of a grid of squares and blanks into which words crossing vertically and horizontally are written according to clues"
],
"domains": [
"Games"
],
"examples": [
{
"text": "she settled down to do the crossword"
}
],
"id": "m_en_gbus0236440.007",
"short_definitions": [
"puzzle consisting of grid of squares and blanks into which words crossing vertically and horizontally are written according to clues"
]
}
],
"variantForms": [
{
"text": "crossword puzzle"
}
]
}
],
"language": "en",
"lexicalCategory": "Noun",
"pronunciations": [
{
"audioFile": "<a href="http://audio.oxforddictionaries.com/en/mp3/crossword_gb_1_8.mp3">http://audio.oxforddictionaries.com/en/mp3/crossword_gb_1_8.mp3</a>",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "ˈkrɒswəːd"
}
],
"text": "crossword"
}
],
"type": "headword",
"word": "crossword"
}
]
}
We can’t stop here, this is bat country - Hunter S Thompson RIP
|
|
|
|
|
It's probably the "audiofile" entry - it's still got spurious double quotes:
"audioFile": "<a href="http:// ... .mp3">http:// ... .mp3</a>",
^ ^ They terminate the string as far as JSON is concerned an need to be escaped.
I'd suggest a simple regex?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If it helps, this works for me with your data:
string jsonText = File.ReadAllText(@"D:\Test Data\Crossword.json");
Regex regex = new Regex("(\"<a href=\"http://)(.*?)(\">)", RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
jsonText = regex.Replace(jsonText, "\"<a href=\\\"http://$2\\\">");
RootObject ro = JsonConvert.DeserializeObject<RootObject>(jsonText);
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Don't know why but I'm still getting null properties - the object isn't null though
We can’t stop here, this is bat country - Hunter S Thompson RIP
|
|
|
|
|