Sunday, February 15, 2009

ASP .net interview question

Questions:
Please write a sample program that parses the string into a series of substrings where the delimiter between the substrings is "^*!%~" and then reassembles the strings and delimiters into a single new string where each of the substrings is in the reverse order from the original string. The method must return the final string.

Original String:
Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E

Output String:
Token E^*!%~Token D^*!%~Token C^*!%~Token B^*!%~Token A

The code sample below shows how to solve the above question:
using System;
using System.Text;
namespace GenericsSample
{
class Program
{
static void Main()
{
string strOriginalString = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
string[] strSeperator = new string[1];
strSeperator[0] = "^*!%~";

string[] strArrayIndividualStrings = strOriginalString.Split(strSeperator, StringSplitOptions.RemoveEmptyEntries);

int intLengthOfStringArray = strArrayIndividualStrings.Length;

StringBuilder sbOutputString = new StringBuilder();
for (int i = (intLengthOfStringArray - 1); i >= 0; i--)
{
sbOutputString.Append(strArrayIndividualStrings[i] + strSeperator[0]);
}
Console.WriteLine("Original String : " + strOriginalString);
Console.WriteLine("Output String : " + sbOutputString.ToString());
Console.ReadLine();
}
}
}

Explanation of the above sample program:
1.
We take the original string into a string variable strOriginalString. I named this variable as strOriginalString. str indicates that the variable is of string datatype. This will give a good impression to the person who reviews your code bcos you are following the coding standards.

2. I then store the string delimiter "^*!%~" in strSeperator variable. strSeperator is of string array data type. This is bcos the split function expects string array or character array as seprator.

3. I then split the strOriginalString into a string array using the split function.

4. I created a variable sbOutputString to store the Output string. sbOutputString data type is StringBuilder.

5. I then loop thru the array from the highest index to 0 and retrieve the individual strings and append to the sbOutputString. As the output string is changing as we loop thru the array it is good to use StringBuilder rather than System.String. Strings of type System.Text.StringBuilder are mutable where as strings of type System.String are immutable. If a string is manipulated many times always use StringBuilder over System.String.

6. Two good things to remember from this example are, follow the coding standards in naming the variables and always use StringBuilder class over Strings where we manipulate a particular string many times.

Question 2:
The XML file below has a list of employees. Your job is to bind the employee IDs and Names to a dropdownlist. ID must be dropdownlist value field and name must be the dropdownlist Text field. Also, only the active employees must be binded to the dropdownlist and the names should be in the ascending order. When I select a name from the dropdownlist, the name and ID of the selected employee must be printed on the webform.

Employees.xml


David
101
true


Tom
102
true


Rick
103
false


Mark
104
true



Code sample:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet DS = new DataSet();
DS.ReadXml(Server.MapPath("Employees.xml"));

DataView DV = DS.Tables["Employee"].DefaultView;
DV.RowFilter = "IsActive='true'";
DV.Sort = "Name asc";

DropDownList1.DataSource = DV;
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Name";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("Name Is : " + DropDownList1.SelectedItem.Text + " and ID is " + DropDownList1.SelectedItem.Value);
}

Code Explanation:
1.
Read the XML data from Employees.xml file into a DataSet. We make use of the ReadXml() method. ReadXml method loads the XML data into the dataset DS. DS.ReadXml(Server.MapPath("Employees.xml"));

2. Now you have the Data in a relational format in the dataset. Create a DataView on the employees table in the DataSet. The DefaultView property of DataTable returns the DataView.
DataView DV = DS.Tables["Employee"].DefaultView;

3. After you have created the DataView, apply the RowFilter, to select only the active employees. You apply the RowFilter as shown below.
DV.RowFilter = "IsActive='true'";

4. Now sort the data in the DataView in ascending order. We sort the data on the Name column. You can apply the sort expression on a dataview as shown below.
DV.Sort = "Name asc";

5. Finally set the DataSource, DataValueField and DataTextField properties of the dropdownlist and call the DataBind() method as shown in the below code.
DropDownList1.DataSource = DV;
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Name";
DropDownList1.DataBind();


Untill now we have seen how to bind an XML file to dropdownlist. We have also seen how to create a DataView on DataTable. DataView is used for sorting and filtering the data. Now we have to get the SelecteValue and SelectedItem Text of a dropdownlist. To achieve this, follow the below steps.

1. Set the autopostback property of the dropdownlist to true. So, when ever a selection in the dropdownlist changes, the webform is posted back to the server automatically.

2. In the DropDownList1_SelectedIndexChanged event handler we can capture the employee name and id using the DropDownList1.SelectedItem.Text and DropDownList1.SelectedItem.Value properties as shown below.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("Name Is : " + DropDownList1.SelectedItem.Text + " and ID is " + DropDownList1.SelectedItem.Value);
}

Note: If you have noticed, we binded the XML file to the dropdownlist only during the initial page load and not during every post back. We do this check by using if(!IsPostBack) property of the page.

1 comment:

Arecamachinery said...

We are much concerned about the quality. The products are made in the most hygienic way.
plates