|
As has been pointed out, all the magic happens when you "SaveChanges".
Prior to that, EF is capturing your "intentions" via add, updates and deletes to the "in-memory" data model; which involves entity keys, hash codes and "attached / detached" entities.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi,
I generate programmatically an excel file and I want to save it in a shared directory in the network but there is an error on the line like this xls.saveas("\\\\myserver\\shareddirectory\\myfile.xlsx");
Do you have any idea ?
Thanks !
|
|
|
|
|
Member 13421843 wrote: Do you have any idea ? No, because you have not told us what the error is.
|
|
|
|
|
Yes. Save it to a local location and copy it after it is saved. That way you'll know if there's a problem with saving or with network-access, and you'll have the added benefit that someone can sneakernet the file to where it is needed regardless of the network.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Is it "like this" or "exactly" like this?
(There is no "universal" "share").
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
i need to add option in proprieties of performance counter because the category name don't appear in properties
|
|
|
|
|
Sorry, what? Is this a C# application you're writing, or is it a question about Visual Studio?
This space for rent
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Perhaps if you try to show us sample code, and point out where your problem comes, it might help?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm going to guess what you are trying to do, but it may be way off.
AMMAR ALNAJIM wrote: because the category name don't appear in properties Sounds like the CategoryAttribute in a PropertyGrid. It would display the property in a separate category, if the property is decorated with it.
AMMAR ALNAJIM wrote: i need to add option in proprieties of performance counter So I'd be guessing you want that attribute on another property - but you can't change the code of the performance-counter class.
Anyway, this[^] might be interesting to you.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hello
I have created a Form in C# and use DataGridView out there. As rows keep getting updated in the DataGridView, it works ok initially. However when I have to scroll down to see the rows added, the software freezes. The rows do get added, but I can't see them. I can only see the rows which get displayed initially in the window (without using scroll).
Where is the problem? Is it with the scroll?
Any help would be appreciated.
Thanks
Deepak
|
|
|
|
|
Member 13419803 wrote: Where is the problem? Most likely somewhere in your code, but it is anyone's guess where. You need to gather some more detailed information about what is happening.
|
|
|
|
|
Member 13419803 wrote: However when I have to scroll down to see the rows added, the software freezes Might be busy getting data, formatting it; which events did you hookup?
I'd also assume you're databinding. That will cause a slight delay when loading the data. If you don't want that delay, you could consider running in virtual mode[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
my code is :
string word = "hello helloworld worldhello";
string[] lis = word.Split(' ');
foreach (string words in lis)
{
Regex rx = new Regex(reg, RegexOptions.IgnoreCase);
Match m = rx.Match(word);
word = word.Replace(words, " ");
}
richTextBox1.Text= word;
I put the "reg" string in my code as a comment to indicate that it doest work as expected
the output was :
world world
but I want :
helloworld worldhello
in other words I want to replace hello as a whole word only any help?
thanks
|
|
|
|
|
|
it doesn't work at all with me
|
|
|
|
|
I think you are getting a bit confused here, you appear to be using two different method to do the same thing, but both of them half-heartedly. You never check if the match worked for example!
The simplest way to do this is to throw away the Regex - since you aren't actually using it at all - and just use the Split:
string words = "hello helloworld worldhello";
string[] lis = word.Split(' ');
List<string> outp = new List<string>();
foreach (string word in lis)
{
if (word != "hello")
{
outp.Add(word);
}
}
words = string.Join(" ", outp);
Console.WriteLine(words);
If you want to use a Regex, then it's not too bad:
string words = "hello helloworld worldhello";
words = Regex.Replace(words, @"\b" + "hello" + "\b", "");
Console.WriteLine(words);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm trying to figure out how to handle multiple exceptions based off my code. I want to have an error for API, one for Tracking Number, and then one for if the XML isn't formatted for the entire XML. Would using try catch for each be the best method and if so how would I code that. The code in bold is what I need assistance with in regards for Tracking Number and API Here is what I have currently:
try
{
if (bool.Parse(WebConfigurationManager.AppSettings["Debug"]) == true)
File.WriteAllText(Path.Combine(Server.MapPath("Log"), DateTime.Now.ToString("MMddyyy_HHmmss") + ".xml"), xmlRequest);
XmlDocument doc = new XmlDocument();
doc.XmlResolver = null;
doc.LoadXml(xmlRequest);
string method = doc.FirstChild.Name;
XmlNode mainNode = doc.FirstChild;
if (method.ToLower() == "xml")
{
method = doc.FirstChild.NextSibling.Name;
mainNode = doc.FirstChild.NextSibling;
}
if (method == "AmazonTrackingRequest")
{
Data.General.Shipment prc = new Data.General.Shipment();
string pronum = mainNode.SelectSingleNode("TrackingNumber")?.InnerText;
prc.GetByProNumber(decimal.Parse(pronum));
rspxml.Root.Add(new XElement("API", "4.0"));
rspxml.Root.Add(new XElement("PackageTrackingInfo"));
rspxml.Root.Element("PackageTrackingInfo").Add(new XElement("TrackingNumber", prc.ProNumber.ToString()));
rspxml.Root.Add(new XElement("PackageDestinationLocation"));
rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("City", prc.Consignee.ToString()));
rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("StateProvince", prc.Consignee.ToString()));
rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("PostalCode", prc.Consignee.ToString()));
rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("CountryCode", prc.Consignee.ToString()));
rspxml.Root.Add(new XElement("PackageDeliveryDate"));
rspxml.Root.Element("PackageDeliveryDate").Add(new XElement("ScheduledDeliveryDate", prc.Consignee.ToString()));
rspxml.Root.Element("PackageDeliveryDate").Add(new XElement("ReScheduledDeliveryDate", prc.Consignee.ToString()));
rspxml.Root.Add(new XElement("TrackingEventHistory"));
foreach (Saia.Data.General.Shipment.HistoryItem itm in prc.History)
{
rspxml.Root.Add(new XElement("TrackingEventDetail"));
rspxml.Root.Element("TrackingEventDetail").Add(new XElement("EventStatus", prc.History.ToString()));
rspxml.Root.Element("TrackingEventDetail").Add(new XElement("EventReason", prc.History.ToString()));
rspxml.Root.Element("TrackingEventDetail").Add(new XElement("EventDateTime", prc.History.ToString()));
rspxml.Root.Add(new XElement("EventLocation"));
}
}
}
catch (CodeException e)
{
Error here
}
catch (Exception e)
{
Error here
}
return rspxml.ToString();
}
}
}
|
|
|
|
|
The code you're using to build the XML can be greatly simplified:
rspxml.Root.Add(
new XElement("API", "4.0"),
new XElement("PackageTrackingInfo",
new XElement("TrackingNumber", prc.ProNumber)
),
new XElement("PackageDestinationLocation",
new XElement("City", prc.Consignee),
new XElement("StateProvince", prc.Consignee),
new XElement("PostalCode", prc.Consignee),
new XElement("CountryCode", prc.Consignee)
),
new XElement("PackageDeliveryDate",
new XElement("ScheduledDeliveryDate", prc.Consignee),
new XElement("ReScheduledDeliveryDate", prc.Consignee)
),
new XElement("TrackingEventHistory",
prc.History.Select(item => new XElement("TrackingEventDetail",
new XElement("EventStatus", item.EventStatus),
new XElement("EventReason", item.EventReason),
new XElement("EventDateTime", item.EventDateTime),
new XElement("EventLocation", item.EventLocation)
)
)
);
This will also fix the problem you currently have where elements are appended to the wrong parent - particularly within the "history" loop.
Now you just need to explain what the problem is, and why you're still mixing XmlDocument and XDocument documents.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
The problem is, not really a problem, I have error codes for the Tracking Number and API Version XElement that I want to using exceptions like this:
rspxml = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><TrackingErrorInfo><TrackingNumber>12345678</TrackingNumber><TrackingErrorDetail><ErrorDetailCode>ERROR_101</ErrorDetailCode><ErrorDetailCodeDesc>INVALID TRACKING NUMBER</ErrorDetailCodeDesc></TrackingErrorDetail></TrackingErrorInfo></AmazonTrackingResponse>");
So for instance when there is an invalid Tracking Number I want the xml to read this error_101 code.
As for XMLDocument and XDocument, what are your suggestions for those?
|
|
|
|
|
Firstly, that's not a valid XML document. You either need to remove the closing </AmazonTrackingResponse> tag, or insert the opening tag.
Secondly, you would read it in the same way as any other XML document:
var rspxml = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><AmazonTrackingResponse><TrackingErrorInfo><TrackingNumber>12345678</TrackingNumber><TrackingErrorDetail><ErrorDetailCode>ERROR_101</ErrorDetailCode><ErrorDetailCodeDesc>INVALID TRACKING NUMBER</ErrorDetailCodeDesc></TrackingErrorDetail></TrackingErrorInfo></AmazonTrackingResponse>");
var errorInfo = rspxml.Descendants("TrackingErrorInfo").FirstOrDefault();
if (errorInfo != null)
{
string trackingNumber = (string)errorInfo.Element("TrackingNumber");
var detail = errorInfo.Element("TrackingErrorDetail");
if (detail != null)
{
string errorCode = (string)detail.Element("ErrorDetailCode");
string errorMessage = (string)detail.Element("ErrorDetailCodeDesc");
}
}
As for the XmlDocument vs XDocument , I'd suggest choosing one and sticking to it. The XDocument is generally easier to work with.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yeah I just post part of that tag. I didn't want to post the entire thing so that's why the beginning tag isn't showing here.
Also how would I check for errors on
rspxml.Root.Add(new XElement("APIVersion", "4.0"));
and I have an error code for if 'XML is not well formed', how would I do that one.
And thanks on using one between XMLDocument or XDocument.
modified 19-Sep-17 12:13pm.
|
|
|
|
|
rspxml.Root.Add(
new XElement("API", "4.0"),
new XElement("PackageTrackingInfo",
new XElement("TrackingNumber", prc.ProNumber)
),
new XElement("PackageDestinationLocation",
new XElement("City", prc.Consignee),
new XElement("StateProvince", prc.Consignee),
new XElement("PostalCode", prc.Consignee),
new XElement("CountryCode", prc.Consignee)
),
new XElement("PackageDeliveryDate",
new XElement("ScheduledDeliveryDate", prc.Consignee),
new XElement("ReScheduledDeliveryDate", prc.Consignee)
),
new XElement("TrackingEventHistory",
prc.History.Select(item => new XElement("TrackingEventDetail",
new XElement("EventStatus", item.EventStatus),
new XElement("EventReason", item.EventReason),
new XElement("EventDateTime", item.EventDateTime),
new XElement("EventLocation", item.EventLocation)
)
)
);
@RichardDeeming, Question about this code. On the
new XElement("TrackingEventHistory",
prc.History.Select(item => new XElement("TrackingEventDetail",
line what should that be because you have .Select but that gives me an error 'Shipment.HistoryCollection' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'Shipment.HistoryCollection' could be found (are you missing a using directive or an assembly reference?)
Is that code I should use and need to add something to eliminate the error. What I was thinking it should be is:
new XElement("TrackingEventHistory",
new XElement("TrackingEventDetail",
new XElement("EventStatus", prc.History),
new XElement("EventReason", prc.History),
new XElement("EventDateTime", prc.History),
new XElement("EventLocation", prc.History)
|
|
|
|
|
Select is a Linq statement.
Try adding the proper using statement to the top of your class.
using System.Linq;
using System.Xml.Linq;
If those do not work you may need to cast your object to something else.
|
|
|
|
|
I already have these
using System.Linq;
using System.Xml.Linq;
You mentioned Cast to something else but not sure what to do there. Anyone else with any suggestions would be appreciated.If no one has any suggestions then I will have to go back to the ToString code I had previously and just go with that.
This is what I have now:
public string ProcessXML(string xmlRequest)
{
XDocument rspxml = null;
try
{
if (bool.Parse(WebConfigurationManager.AppSettings["Debug"]) == true)
File.WriteAllText(Path.Combine(Server.MapPath("Log"), DateTime.Now.ToString("MMddyyy_HHmmss") + ".xml"), xmlRequest);
XmlDocument doc = new XmlDocument();
doc.XmlResolver = null;
doc.LoadXml(xmlRequest);
string method = doc.FirstChild.Name;
XmlNode mainNode = doc.FirstChild;
if (method.ToLower() == "xml")
{
method = doc.FirstChild.NextSibling.Name;
mainNode = doc.FirstChild.NextSibling;
}
if (method == "AmazonTrackingRequest")
{
Saia.Data.General.Shipment prc = new Data.General.Shipment();
string pronum = mainNode.SelectSingleNode("TrackingNumber").InnerText;
prc.GetByProNumber(decimal.Parse(pronum));
if (string.IsNullOrEmpty(pronum))
{
rspxml = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><AmazonTrackingResponse xmlns: xsi = 'http://www.w3.org/2001/XMLSchema-instance'xsi: noNamespaceSchemaLocation='AmazonTrackingResponse.xsd'><APIVersion >4.0</APIVersion><TrackingErrorInfo><TrackingNumber>12345678</TrackingNumber><TrackingErrorDetail><ErrorDetailCode>ERROR_101</ErrorDetailCode><ErrorDetailCodeDesc>INVALID TRACKING NUMBER</ErrorDetailCodeDesc></TrackingErrorDetail></TrackingErrorInfo></AmazonTrackingResponse>");
}
else
{
decimal tn = 0.0m;
if (decimal.TryParse(pronum, out tn))
{
prc.GetByProNumber(tn);
}
else
{
Console.WriteLine("Unable to parse '{0}'.", pronum);
}
}
rspxml = XDocument.Parse("<AmazonTrackingResponse xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='AmazonTrackingResponse.xsd'></AmazonTrackingResponse>");
rspxml.Root.Add(new XElement("APIVersion", "4.0"));
var errorInfo = rspxml.Descendants("TrackingErrorInfo").FirstOrDefault();
if (errorInfo != null)
{
string packtrackinginfo = (string)errorInfo.Element("PackageTrackingInfo");
var detail = errorInfo.Element("TrackingErrorDetail");
if (detail != null)
{
string errorCode = (string)detail.Element("ErrorDetailCode");
string errorMessage = (string)detail.Element("ErrorDetailCodeDesc");
}
}
rspxml.Root.Add(
new XElement("API", "4.0"),
new XElement("PackageTrackingInfo",
new XElement("TrackingNumber", prc.ProNumber)
),
new XElement("PackageDestinationLocation",
new XElement("City", prc.Consignee),
new XElement("StateProvince", prc.Consignee),
new XElement("PostalCode", prc.Consignee),
new XElement("CountryCode", prc.Consignee)
),
new XElement("PackageDeliveryDate",
new XElement("ScheduledDeliveryDate", prc.Consignee),
new XElement("ReScheduledDeliveryDate", prc.Consignee)
),
new XElement("TrackingEventHistory",
new XElement("TrackingEventDetail",
new XElement("EventStatus", prc.History),
new XElement("EventReason", prc.History),
new XElement("EventDateTime", prc.History)
),
new XElement("EventLocation",
new XElement("Street1", prc.History),
new XElement("City", prc.History),
new XElement("StateProvince", prc.History),
new XElement("PostalCode", prc.History),
new XElement("CountryCode", prc.History),
new XElement("SignedForByName", prc.History)
),
new XElement("PickupStoreInfo",
new XElement("PickupDueDateDetails",
new XElement("Date", prc.History),
new XElement("UTCOffset", prc.History)
),
new XElement("PickupID",
new XElement("StoreName",
new XElement("LocationID",
new XElement("StoreLocation",
new XElement("Street1", prc.History),
new XElement("City", prc.History),
new XElement("StateProvince", prc.History),
new XElement("PostalCode", prc.History),
new XElement("CountryCode", prc.History)
)
)
)
)
)
)
);
}
}
catch (CodeException e)
{
rspxml = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><AmazonTrackingResponse xmlns: xsi = 'http://www.w3.org/2001/XMLSchema-instance'xsi: noNamespaceSchemaLocation='AmazonTrackingResponse.xsd'><APIVersion>4.0</APIVersion ><TrackingErrorInfo><TrackingNumber>12345678</TrackingNumber><TrackingErrorDetail><ErrorDetailCode>ERROR_203</ErrorDetailCode><ErrorDetailCodeDesc>XML DOCUMENT NOT WELL FORMED</ErrorDetailCodeDesc></TrackingErrorDetail></TrackingErrorInfo></AmazonTrackingResponse>");
Core.Framework.Debug.CodeException(xmlRequest, e);
}
catch (Exception e)
{
rspxml = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><AmazonTrackingResponse xmlns: xsi = 'http://www.w3.org/2001/XMLSchema-instance'xsi: noNamespaceSchemaLocation='AmazonTrackingResponse.xsd'><APIVersion>4.0</APIVersion ><TrackingErrorInfo><TrackingNumber>12345678</TrackingNumber><TrackingErrorDetail><ErrorDetailCode>ERROR_203</ErrorDetailCode><ErrorDetailCodeDesc>XML DOCUMENT NOT WELL FORMED</ErrorDetailCodeDesc></TrackingErrorDetail></TrackingErrorInfo></AmazonTrackingResponse>");
Core.Framework.Debug.Exception(xmlRequest, e);
}
return rspxml.ToString();
}
public class EventCode
{
public enum EvCodes {Delivered, DispatchedForDelivery, DepartedFromTerminal, ArrivedAtDestinationTerminal, DNF};
public static void Main()
{
string val = "Dispatched For Delivery";
switch ((EvCodes)Enum.Parse(typeof(EvCodes), val.Replace(" ", "")))
{
case EvCodes.DispatchedForDelivery:
case EvCodes.Delivered:
Console.WriteLine("EVENT_301");
break;
case EvCodes.DepartedFromTerminal:
Console.WriteLine("EVENT_202");
break;
case EvCodes.ArrivedAtDestinationTerminal:
Console.WriteLine("EVENT_202");
break;
case EvCodes.DNF:
Console.WriteLine("EVENT_101");
Console.WriteLine("EVENT_102");
break;
default:
break;
}
}
}
}
}
modified 20-Sep-17 9:27am.
|
|
|
|
|
What interfaces are implemented by Shipment.HistoryCollection ?
Presumably it implements IEnumerable , but not IEnumerable<T> ; in which case, you need to insert a call to Cast[^] before the call to Select :
new XElement("TrackingEventHistory",
prc.History.Cast<Saia.Data.General.Shipment.HistoryItem>().Select(item => new XElement("TrackingEventDetail",
new XElement("EventStatus", item.EventStatus),
new XElement("EventReason", item.EventReason),
new XElement("EventDateTime", item.EventDateTime),
new XElement("EventLocation", item.EventLocation)
)
)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|