hello Friends please verify my code
Is it a right way to pass optional parameter to a method.
if any thing wrong please suggest me if there exist any better solution.
protected void Ok_Click(object sender, EventArgs e)
{
try
{
if (Page.IsValid)
{
int course_id = Convert.ToInt32(course.SelectedValue);
int passoutYear = Convert.ToInt32(passout.SelectedValue);
int currentBacklog = Convert.ToInt32(currrentBacklogDDL.SelectedValue);
int sex = Convert.ToInt32(gender.SelectedValue);
int? eGap = null;
int? firstYrPercent = null;
int? secondYrPercent = null;
int? thirdYrPercent = null;
int? finalYearpercent = null;
int? currentDegeePercentage = null;
int? highSchoolPercentge = null;
int? higherSchoolPercentage = null;
int? grauationPercentage = null;
int? diplomaPercentage = null;
int? noOfAtkt = null;
string dateOfBirth = DOB.Text.Trim();
DateTime birthDate = DateTime.ParseExact(dateOfBirth, "dd/mm/yyyy", System.Globalization.CultureInfo.InvariantCulture);
string outPut = birthDate.ToString("mm/dd/YYYY");
DateTime date = Convert.ToDateTime(outPut);
if (!String.IsNullOrEmpty(educationGap.Text))
{
eGap = Convert.ToInt32(educationGap.Text.Trim());
}
if (!string.IsNullOrEmpty(firstYear.Text))
{
firstYrPercent = Convert.ToInt32(firstYear.Text.Trim());
}
if (!string.IsNullOrEmpty(secondYear.Text))
{
secondYrPercent = Convert.ToInt32(secondYear.Text.Trim());
}
if (!string.IsNullOrEmpty(thirdYear.Text))
{
thirdYrPercent = Convert.ToInt32(thirdYear.Text.Trim());
}
if (!string.IsNullOrEmpty(finalyear.Text))
{
finalYearpercent = Convert.ToInt32(finalyear.Text.Trim());
}
if (!string.IsNullOrEmpty(currentDegree.Text))
{
currentDegeePercentage = Convert.ToInt32(currentDegree);
}
if (!string.IsNullOrEmpty(higherSchool.Text.Trim()))
{
higherSchoolPercentage = Convert.ToInt32(higherSchool.Text.Trim());
}
if (!string.IsNullOrEmpty(highSchool.Text))
{
highSchoolPercentge = Convert.ToInt32(highSchool.Text.Trim());
}
if (!string.IsNullOrEmpty(graduation.Text))
{
grauationPercentage = Convert.ToInt32(graduation.Text.Trim());
}
if (!string.IsNullOrEmpty(diploma.Text))
{
diplomaPercentage = Convert.ToInt32(diploma.Text.Trim());
}
if (!string.IsNullOrEmpty(atkt.Text))
{
noOfAtkt = Convert.ToInt32(atkt.Text.Trim());
}
Dictionary<string, object> paramList = new Dictionary<string, object>();
paramList.Add("@courseId", course_id);
paramList.Add("@passoutYear", passoutYear);
paramList.Add("@currentBacklog", currentBacklog);
paramList.Add("@sex", sex);
paramList.Add("@eGap", eGap);
paramList.Add("@firstYrPercent", firstYrPercent);
paramList.Add("@secondYrPercent", secondYrPercent);
paramList.Add("@thirdYrPercent", thirdYrPercent);
paramList.Add("@finalYearpercent", finalYearpercent);
paramList.Add("@currentDegeePercentage", currentDegeePercentage);
paramList.Add("@highSchoolPercentge", highSchoolPercentge);
paramList.Add("@higherSchoolPercentage", higherSchoolPercentage);
paramList.Add("@grauationPercentage", grauationPercentage);
paramList.Add("@diplomaPercentage", diplomaPercentage);
paramList.Add("@noOfAtkt", noOfAtkt);
StringBuilder branchId= new StringBuilder();
foreach (ListItem li in branch.Items)
{
if (li.Selected)
{
branchId.Append(Convert.ToInt32(li.Value));
}
}
DataTable dt = searchManager.GetEligibleStudent(paramList, branchId);
}
}
catch (Exception ex)
{
COMMON.logger.Error("Error On Button click Ok", ex);
}
}
it’s a bit sad that you first convert the values to the correct value, then put it into a list of object such that in GetEligibleStudent, you’ll have to cast those values again.
My guess would be to create a class for this, such that, say, higherSchoolPercentage becomes a field. Since you’ll be getting student records back, it might even be that you can reuse this structure.
hth,
Nic
UPDATE
create a class like
then, in your page, say
Then you can say
Like this,
UPDATE 2
Since the only format in which these fields are required is, in fact, the key/value list that’s built here, to be passed on to an SQL command, I think the code sample is ok. One might still think about refactoring that code into a separate method, but my previous suggestion of creating a class turns out to be, in fact, superfluous.