i have a method say for student fee collection, in that there are three parameter studid,feeamount, collection date. method defination is like below.
public string CollectFee(string studentid, decimal feeamount, DateTime collectiondate)
{
}
In this while calling this method from presentation layer..user must have to have enter all the parameter if any of the parameter is missing he will get message like....studentid and feeamount and collection date is required. i want to check these while impleting the CollectFee method, since the method parameter having string, decimal, datetime datatype, how can i check empty or not for these in one if condition.
To achieve this i have written this code i am not sure whether its correct or not:
public class Student
{
public string FeesCollection(string studentid, decimal feesamount, DateTime collectiondate)
{
string Result = string.Empty;
if (studentid == string.Empty || feesamount == 0 || collectiondate == DateTime.MinV开发者_开发知识库alue)
{
Result = "Required fields are empty.";
}
else
{
Result = "Success.";
}
return Result;
}
}
while calling this from GUI layer i have written this code:
protected void BtnAdd_Click(object sender, EventArgs e)
{
Student obj = new Student();
string studentid = TxtStdId.Text;
decimal collectionamount = 0.0m;
if (txtCollectionAmount.Text !=null)
{
collectionamount = Convert.ToDecimal(txtCollectionAmount.Text);
}
DateTime collectiondate = Convert.ToDateTime(txtCollectionDate.Text);
lblResult.Text=obj.FeesCollection(studentid, collectionamount, collectiondate);
}
But while testing the invalid condtion like keeping amounttextbox or datetimetextbox empty getting exception like input string is not in correct format. any help pls.
I would not implement this in the method.
Validation like this is better to do in the GUI layer and you should there return the exact items that was missing, not just failed or success.
Putting the validation in the method makes it harder to report back a constructive error message and for future modifications you will have hardcoded GUI information within bussiness logic.
If you check for missing information directly in code behind you can highlight the items missing.
But the method could of cause have its on error checking, specifically for a valid studentid and that the amount is correct or likewize.
That way you get more error checking and the method could be used from different forms.
If you have the validation in the page there are some very good tools to use that also add the benefit of client side validation as a first line, then serverside to catch attempted hacks.
Microsoft hase a few articles on this: http://msdn.microsoft.com/en-us/library/ms229603.aspx
This is because the Text
property returns an empty string (string.Empty
) when it is blank, not a null
, so:
if (txtCollectionAmount.Text != string.Empty)
{
collectionamount = Convert.ToDecimal(txtCollectionAmount.Text);
}
..should do the trick.
Note also, the user could type anything here and you'll get an exception. Consider using Decimal.TryParse
instead.
txtCollectionAmount.Text is not a valid decimal. Try
decimal dec;
decimal.TryParse(txtCollectionAmount.Text, out dec);
This will ensure that txtCollectionAmount.Text is indeed a decimal.
The TextBox control can have both numeric and non-numeric data. You need to code for such possibilities like this:
protected void BtnAdd_Click(object sender, EventArgs e)
{
Student obj = new Student();
string studentid = TxtStdId.Text;
decimal collectionamount = 0.0m;
if (txtCollectionAmount.Text !=null)
{
bool canConvert = decimal.TryParse(txtCollectionAmount.Text, out collectionAmount);
if (!canConvert)
{
// ... obviously the text in the textbox was invalid for some reason...
// put the handler for the invalid data here.
}
}
DateTime collectiondate = Convert.ToDateTime(txtCollectionDate.Text);
lblResult.Text=obj.FeesCollection(studentid, collectionamount, collectiondate);
}
精彩评论