|
I am not sure what the validation looks like for your situation, but there are a couple of methods.
The most simple is to validate to see if the string exists.
if (string.isNullOrEmpty(apiVariableName)) { return ; }
Since the first two bold items are static text there really isn't anything to validate there unless there is something else we aren't seeing. That said, one of the best methods to validate your data is to set validation attributes on your model class (Data.General.Shipment() and write a method that validates that. For example:
internal class Shipment {
[Required]
[StringLength(12)]
public string ShippingNumber { get; set; }
[Required(ErrorMessage = "City cannot be empty.")]
public string City { get; set; }
[Required(ErrorMessage = "Zip is Required")]
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")]
public string PostalCode { get; set; }
}
The trick here is that you really want to leave your model validating to the model or validator class and not be trying to validate when you are building the XML. It is good defensive programming to cover null possibilities and such, but you really want to uncomplicate things as much as possible if you are crafting an XML by hand.
|
|
|
|
|
So where would the model class go in relation to my code. Would it be outside of the XElement code? Before it? After it?
|
|
|
|
|
Your model class already exists, it is the Data.General.Shipment object. All of this is really outside the scope of your original question of multiple try... catch... blocks, though. If you are able it is always good to step back and ask yourself if this is the right approach. DO you really need XML? It is super wordy and a pain to work with, JSON is way better, but again, outside the scope.
To put an easy stamp on your original question, you can have many try catch blocks but it is going to be a very iterative process for you to figure them out and consider all of the permutations. So you have to ask yourself what the goal is. Is there a recovery path through these exceptions or are you trying to send different error messages to the user? Or maybe just log more detail?
Either way if your try/catch is at the point of building out the XML all you are likely to get is XML formatting exceptions, which do not encapsulate your business rules. A couple notes on generally being defensive in the code you have written:
1. Do not use decimal.Parse. Since you are using the null check operator getting the tracking number, you expose the possibility of errors there. Try this:
var prc = new Data.General.Shipment();
var node = mainNode.SelectSingleNode("TrackingNumber");
if (node == null)
{
return;
}
decimal trackingNumber;
if (!decimal.TryParse(node.InnertText, out trackingNumber))
{
return;
}
prc.GetByProNumber(trackingNumber);
try
{
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()));
}
catch (NullReferenceException n)
{
}
At very least, make sure you validate that the tracking number comes up and hydrates an object.
|
|
|
|
|
I am using XML because I'm outputting it to AWS. So I don't have a choice unfortunately.
|
|
|
|
|
Hi,
I make my question after some research in web without any clear response to my problem.
I have the following XML and I need to apply an XSL transformation to get only nodes which haven't child.
Input :
<Root_Element Att_Name="XXX">
<right Id="100">
<right Id="110" />
<right Id="111">
<right Id="112" />
<right Id="113">
<right Id="114" />
<right Id="115" />
</right>
<right Id="116" />
</right>
<right Id="117">
<right Id="118" />
<right Id="119">
<right Id="120" />
<right Id="121" />
</right>
</right>
</right>
</Root_Element>
Expected output :
<Root_Element Att_Name="XXX">
<right Id="110" />
<right Id="112" />
<right Id="114" />
<right Id="115" />
<right Id="116" />
<right Id="118" />
<right Id="120" />
<right Id="121" />
</Root_Element>
|
|
|
|
|
What does this have to do with C#? BTW, when you post a question, it helps if you show us what you've already tried. That way, we won't duplicate things you've already ruled out.
This space for rent
|
|
|
|
|
sorry for the misunderstood , i will move it
|
|
|
|
|
hi
i need Visual Studio .NET 2002 prerequisites cd
or all Visual Studio .NET 2002
Because I have a program using Crystal Reports 9.1.3300.0
I have searched everywhere on the internet and I can not find this version, can you help me?
p.s
i cant use another version for Visual Studio the program i use refuses
|
|
|
|
|
We can't really help you here: VS2002 is fifteen years old, and did not come in an Express edition - so it's a paid-for product and any download version would be pirated.
So you are going to have to scratch around on the internet and see if you can find a copy for sale - good luck with that - we can;t do that for you.
But ... that's probably not going to be your worst problem! VS2002 was a 32bit product that will apparently not install on 64 bit systems at all - so you are probably going to need a 32bit version of XP running to actually install and use it ...
Me? I'd look at the program source, or the person you got it from, and see if you can get an updated version that is a bit more modern...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Message Closed
modified 20-Sep-17 3:58am.
|
|
|
|
|
No.
One of them doesn't compile.
SqlTransaction does not expose public constructors: you create a transaction via the SqlConnection object.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
sir please explain i am confusing between begintransaction and sqltransaction objcet.
|
|
|
|
|
can i do something like this?
SqlConnection conn = new SqlConnection();
SqlCommand command1 = new SqlCommand();
SqlCommand command2 = new SqlCommand();
SqlTransaction trx = conn.BeginTransaction();
command1.Transaction = trx;
command2.Transaction = trx;
try
{
command1.ExecuteNonQuery();
command2.ExecuteNonQuery();
trx.Commit();
}
catch (SqlException ex)
{
trx.Rollback();
}
finally
{
}
|
|
|
|
|
Yes. But I'd use the SqlCommand overload that includes the Connection and Transaction objects.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
you mean:
SqlCommand cmd=new SqlCommand("Command Text",Conncetion, Transaction)
|
|
|
|
|
Yes. Don't forget to use parameterized queries though!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ok Sir.
Thanks You, Now i understood.
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am new to programming (very new and super confused lol) and I am stuck on a section of my programming assignment
To sum it up , I have to make a cookie calorie calculator. one cookie is 75 calories
This is what I have
int cookies;
int caloriesPercookie = 75;
int totalCalories;
totalCalories = (cookies * caloriesPercookie);
I want to multiple the cookie by the calories. In c# its saying " use of unassigned local variable 'cookies'"
I thought I assigned it properly. How would I correct it? thanks for any help
modified 16-Sep-17 20:07pm.
|
|
|
|
|
Quote: I thought I assigned it properly. No, you declared it properly, you did not assign it at all. There is a difference in declaration and assigning, and that concept is similar in C#, C++ and other similar C-family of languages.
The problem persists, because, you cannot expect C# to run like C++, in C#, you need to assign a default value (which differs in structs and class types and I do not want to confuse you on that topic), whereas in C++ you can leave that part and something good (or bad) comes there at that memory address. For the time being, let's just say you want to have a default value of the cookies (which can be, 0, 1, 2,... 10 or whatever), so you do the following,
int cookies = 2;
Now when you will move onwards you will not get an error on the cookies variable.
But since the multiplication has to work with totalCalories as well, I would leave that upto you to fill and proceed further in the program. Try giving it a value and running the program.
Secondly, I also hope you are not expected to ask for inputs, also you have not shown that attempt so I assume you are allowed to hardcode the amount of these variables.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Member 13413476 wrote: use of unassigned local variable You will also need to learn to research your problems. The first step should be to plug the error message into a search engine. A Google search comes up with an excellent selection of answers.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
I am developing a Web Application with Asp.NET MVC5. As I dont have much experience, I am stuck with a functionality using JQuery and Partial View.
I have a Side Menu as a partial view in a page. This partial view contains a DropdownList which is populated from model, and it works fine. However, I want to display a list of items from the db when the dropdown is selected, based on the selection, which is not working.
Partial View (_UserPartialView)
$('#dropdown').change(function(){
var item = $(this).val();
$.get("/Controller/GetItems/" + item, function(data)
{
('#target').html(data);
});
Controller
public ActionResult (int id)
{
var test = db.Users.Where(e => e.cId = id);
return PartialView(" _UserPartialView", test);
}
Please help.
Thanks
|
|
|
|
|
sunsher wrote: I am developing a Web Application with Asp.NET MVC5 Then please post your question in the ASP.NET forum.
|
|
|
|
|
hi,
how can i play all type of video in my application.
i m using Microsoft.DirectX.AudioVideoPlayback but all video format not play by this.
thanksin advance.
|
|
|
|
|