|
You didn't break any rules, it's just that the author will probably have a better idea of what you're trying to do.
|
|
|
|
|
I usually make UI controls contain data that is otherwise well ordered elsewhere. BeginInvoke means the code is running on the UI thread, killing responsiveness. The catch is you can't access UI members. Thus the justification for me using separation of powers so to speak.
Also, three seconds is slow to loop through all components on a UI and get information. Perhaps, the ui can be re-thought to be less complex?
|
|
|
|
|
i have a data table which as Name and employee Id Details
Name EmpID
Tom 1238
Dick 1069
if i use the code
string json = JsonConvert.SerializeObject(dt, Formatting.Indented);
i get Json data as follows
[
{
"Name": "Tom",
"EmpID": 1238
},
{
"Name": "Dick",
"EmpID": 1069
}
]
But i need the JSon data in the below format how do i achieve it???
"DATA": [
["Tom", 1238],
["Dick", 1069]
]
|
|
|
|
|
You can probably override the ToString method and return your desired format.
|
|
|
|
|
Something like this should get you started:
var wrapper = new
{
DATA = dt.Rows.Cast<DataRow>()
.Select(row => row.ItemArray)
.ToArray()
};
string json = JsonConvert.SerializeObject(wrapper, Formatting.Indented);
That will produce:
{
"DATA": [
[
"Tom",
1238
],
[
"Dick",
1069
]
]
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I'm quite new to C# and been working on this problem for days and already at my dead end . I have been trying to learn from sample codes from forums and doing lots of cuts and pastes. Hope someone can help.
I have a Windows Service where I am getting data from web, write it to a .txt file and email at scheduled time.
I can send scheduled email alone. But when I tried to extract data from web, write that data in .txt file and email that data as scheduled altogether, the program hangs at new StreamWriter declaration. I used eventLog1.WriteEntry() to check where it stops. There is no error message. The program just hangs there. I really don't know what is happening. Can someone help? This is what I have done so far ..
protected override void OnStart(string[] args)
{
System.Timers.Timer time = new System.Timers.Timer();
time.Start();
time.Interval = (1000 * 60 * 5);
time.Elapsed += new ElapsedEventHandler(time_elapsed);
time.AutoReset = true;
time.Start();
}
public void time_elapsed(object sender, ElapsedEventArgs e)
{
DateTime scheduledRun = DateTime.Today.AddHours(13.20);
DateTime lastRan = lastTime.LastWriteTime;
if (DateTime.Now > scheduledRun)
{
TimeSpan sinceLastRun = DateTime.Now - lastRan;
if (sinceLastRun.Minutes > 5)
{
importFrmURL("http:// ..", "@"\Import1.txt"");
}
}
}
public void importFrmURL(string url, string dir)
{
System.IO.StreamWriter fileWriter;
fileWriter = new System.IO.StreamWriter(dir);
try
{
var client = new WebClient();
using (var stream = client.OpenRead(url))
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
fileWriter.WriteLine(line);
}
fileWriter.Close();
}
}
catch (Exception e)
{
eventLog1.WriteEntry("Error occurred!\n\r" + e.ToString());
}
}
modified 28-Nov-13 2:28am.
|
|
|
|
|
Use a full path instead of the filename only, e.g. @"C:\Temp\Import1.txt".
Also, I'd suggest to create a FileStream first, and then write into that stream:
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
using (StreamWriter writer = new StreamWriter(fs))
{
}
}
|
|
|
|
|
Yes this works! Can you please explain to me what happened when I don't do FileStream? Or if there is an article I can read from.
|
|
|
|
|
Well, the parameters of the FileStream constructor mean that you can create the file when it does not exist already. Don't know for StreamWriter when the file does not yet exist.
|
|
|
|
|
But this doesn't work when I try to save this .txt file in a server drive (e.g. S:\). It is connected and I can see the S drive in my computer but why can't I write there? Can't I just treat it like any other folder and save the text file under @"S:\Project1\Import1.txt" etc?
|
|
|
|
|
A mapped drive requires authentication, and your windows service is probably not running under the users' credentials.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Something else to be aware of - when you are running a Windows Service, it may not actually know there's an S drive. This is a mapped drive designation that applies to a particular account - unless you set the service to run as that account, it will not know about it.
|
|
|
|
|
I have an object which mimics a table structure in sql. Lets call this object validation. Validation has a property isrequired with a get and set.
when i use this property in code i.e.
validation val = new validation
and then try to change the value of isrequired intellisense says i can not assign to this isrequired because it is read only.
I have a user that wants some items for some objects to be required where as other objects isrequired is false. The table that this object mimics has fieldname and isrequired either true or false. in this case the database has isrequired as true. I just want to change the isrequired in the object and not affect change in the database table.
when i use form.fieldname.isrequiered = false it tells me I can not assign because it is read only. Thanks.
|
|
|
|
|
I would suggest changing it from readonly to read/write.
Veni, vidi, abiit domum
|
|
|
|
|
Thank you so much for your response. where would that change take place?
something like form.fieldname.isrequired.attribute = read/write?
|
|
|
|
|
Unless you show the code where the variable's properties are declared it's anyone's guess.
Veni, vidi, abiit domum
|
|
|
|
|
Pretty basic property establishment
[XmlAttribute("IsRequired")]
[Bindable(true)]
public bool? IsRequired
{
get { return GetColumnValue<bool?>(Columns.IsRequired); }
set { SetColumnValue(Columns.IsRequired, value); }
}
Does this help?
|
|
|
|
|
seeker62 wrote: Does this help? I'm afraid not; I don't recognise what that code is doing.
Veni, vidi, abiit domum
|
|
|
|
|
Ok thanks for your input. I am not sure what else I can provide. I am sure it has something to do with the fact the object is directly correlated to the table in sql useing subsonic to create the object of the table. Again thanks for your input.
|
|
|
|
|
seeker62 wrote: useing subsonic to create the object of the table. No idea what that is. If it's a third party library then you should probably try their website.
Veni, vidi, abiit domum
|
|
|
|
|
This looks like you are getting the Column Value with GetColumnValue call. Not sure what Columns.IsRequired is, another property? My guess is that it comes from the table data. If that is correct then you are trying to set data in the table column with your setter.
Your problem seems to be that Columns.IsRequired is set to read only, not your IsRequired property.
The only way that I know of to set a class property read only is to not give it a setter.
|
|
|
|
|
or maybe also make the setter private
|
|
|
|
|
I am trying to develop a tool which will add a host into the DNS server calling dnscmd.exe. I am assigning the property FileName as "dnscmd.exe" and pass the required i/p parameters. I did call nslookup.exe same way as I have called dnscmd.exe but in case of nslookup.exe code is working properly but in case of dnscmd.exe it's throwing exception while starting the process (here p.Start()) with message as "The system cannot find the file specified" .I am sharing the code snippet in top.
I have uncomment the WorkingDirectory and FileName (with fully qualified path of dnscmd) but still got the the same error. Someone please reply on my post and provide the resolution as I'll implement it ASAP. Thanks in advance
Public bool AddtoDns(IStore iStr)
{
#region "Comment for Testing"
bool isAdded = true;
string strTid = "T00000013";
string strDnsServerName= "OD-EV-W-DC-5.7-11T.COM";
string strZoneName= "7-11t.com";
string strIP= "10.172.13.10"
System.Diagnostics.Process p = new System.Diagnostics.Process();
try
{
if (!CheckDnsEntry(iStr))
{
p.StartInfo.FileName = "dnscmd.exe";
p.StartInfo.Arguments = strDnsServerName + " " + "/RecordAdd" + " " + strZoneName + " " + strTid + " " + "A" + " " + strIP;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
isAdded = true;
}
else
{
isAdded = false;
}
return isAdded;
}
catch (Exception ex)
{
throw ex;
}
finally
{
p.Dispose();
}
#endregion
}
|
|
|
|
|
The most obvious thing to check is: "is 'dnscmd' installed, available, and on the system path?"
For example: if you run up a command box and type "dnscmd" into it, does it run? It doesn't on my computer...
The only instant messaging I do involves my middle finger.
English doesn't borrow from other languages.
English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.
|
|
|
|
|
yes dnscmd.exe installed in my computer under c:\windows\system32 (OS-Windows7). Now I am just trying to execute dns command using the below code-
private void button3_Click(object sender, EventArgs e)
{
Process.Start("C:\\Windows\\System32\\cmd.exe", "/K \"dnscmd\"");
}
The above single line code works fine when we replace dnscmd to nslookup,ipconfig etc but when I am trying to run the single line code with dnscmd it opens a cmd window and showing "not recognized as an internal or external command" message but if I open a cmd window from run (not from c# programe) in the same computer (working fine from all directory) then dnscmd works fine and able to add dns entry also.
So, is there any possibility on access issue or anything else which causing abnormal behaviour of dnscmd. Please help!!!!
Subhendu
|
|
|
|