-This assignment is about creating the Rolodex application that contains the con
ID: 3853421 • Letter: #
Question
-This assignment is about creating the Rolodex application that contains the contact information. Each contact information includes:
First name
Last name
Middle name (optional)
Phone number
-The Rolodex application will use SharedPreferences for data persistent operations:
-Retrieve Rolodex records
-Add a new Rolodex record
-Update the existing Rolodex record
-Delete the selected Rolodex record
-The Rolodex application uses Fragment and DialogFragment. In addition, the Rolodex records are displayed in the ListView view. The ListView view is set to be single-selection mode.
-Each row in the ListView view is another layout. The layout for each row contains two TextView views.
-The top TextView view contains the name of the contact, starting the last name, then the comma, and followed by the first name and middle name.
-The bottom TextView view contains the phone number.
Explanation / Answer
//This is the HTML code that have the 2 row views
<b>Contact Information:</b>
<br /><br />
You are viewing
<asp:Label ID="lblView" runat="server" Text="" />
<table>
<tr>
<td valign="top" width="420px">
<asp:DataList ID="dlContacts" runat="server" RepeatLayout="Table" RepeatColumns="2"
CellPadding="2" CellSpacing="2">
<ItemTemplate>
<table cellpadding="2" cellspacing="0">
<tr>
<td colspan="2">
<%# Eval("lastname") %>,
<%# Eval("firstname") %>
<%# Eval("middlename") %>
</td>
</tr>
<tr>
<td>
Phone:
</td>
<td>
<%# Eval("phno")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
<td valign="top">
<asp:Repeater ID="rptAlphabets" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkAlphabet" runat="server" Text='<%#Eval("Value")%>'
Enabled='<%# Eval("isNotSelected")%>' />
<br />
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
//I have used an ASP.Net DataList and ASP.Net Repeater control
//The DataList will be used to display the Contacts while the Repeater control will be used to create the Alphabetical list.
//There are two parts.
//1. Populating Alphabet list
//2. Populating the contacts from database.
//Populating Alphabet list
//To populate the list of alphabets I have created a property class that will store the alphabets
public class Alphabet
{
private string _value;
private bool _isNotSelected;
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
public bool isNotSelected
{
get
{
return _isNotSelected;
}
set
{
_isNotSelected = value;
}
}
}
private void GenerateAlphabets()
{
List<Alphabet> alphabets = new List<Alphabet>();
Alphabet alphabet = new Alphabet();
alphabet.Value = "ALL";
alphabet.isNotSelected = !alphabet.Value
.Equals(ViewState["CurrentAlphabet"]);
alphabets.Add(alphabet);
for (int i = 65; i <= 90; i++)
{
alphabet = new Alphabet();
alphabet.Value = Char.ConvertFromUtf32(i);
alphabet.isNotSelected = !alphabet.Value
.Equals(ViewState["CurrentAlphabet"]);
alphabets.Add(alphabet);
}
rptAlphabets.DataSource = alphabets;
rptAlphabets.DataBind();
}
//Now we have a table named ‘customers’ which has four columns i.e firstname,lastname,middlename & phno.We create a stored procedure for the sql query.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spx_GetContacts]
@Alphabet VARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
IF @Alphabet = 'ALL'
BEGIN
SELECT *
FROM Customers
END
ELSE
BEGIN
SELECT *
FROM Customers
WHERE lastname LIKE @Alphabet + '%'
END
END
// Populating the contacts from database
private void BindDataList()
{
string conStr = ConfigurationManager .ConnectionStrings["conStr"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("spx_GetContacts");
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Alphabet", ViewState["CurrentAlphabet"]);
con.Open();
dlContacts.DataSource = cmd.ExecuteReader();
dlContacts.DataBind();
con.Close();
if (ViewState["CurrentAlphabet"].ToString().Equals("ALL"))
lblView.Text = "all Contacts.";
else
lblView.Text = "Contacts whose name starts with "+ ViewState["CurrentAlphabet"].ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["CurrentAlphabet"] = "ALL";
this.GenerateAlphabets();
this.BindDataList();
}
}
// Finally we call the following event when the LinkButtons in the ASP.Net repeater controls are clicked
protected void Alphabet_Click(object sender, EventArgs e)
{
LinkButton lnkAlphabet = (LinkButton)sender;
ViewState["CurrentAlphabet"] = lnkAlphabet.Text;
this.GenerateAlphabets();
this.BindDataList();
}
// a new HTML page for inserting into database
<b>Provide the following details:</b>
<table>
<tr><td>First Name</td><td> <asp:TextBox ID="txt1" runat="server" /></td></tr>
<tr><td>Middle Name</td><td> <asp:TextBox ID="txt2" runat="server" /></td></tr>
<tr><td>Last Name</td><td> <asp:TextBox ID="txt3" runat="server" /></td></tr>
<tr><td>Contact</td><td> <asp:TextBox ID="txt4" runat="server" /></td></tr>
</table>
<asp:LinkButton ID="lnkAlphabet" runat="server" Text=''Insert”/>
// a new HTML page for deleting from database
<b>Provide the contact no:</b>
<table>
<tr><td>Contact</td><td> <asp:TextBox ID="txt5" runat="server" /></td></tr>
</table>
<asp:LinkButton ID="lnkAlphabet" runat="server" Text=''Delete”/>
// a new HTML page for updating database
<b>Provide the contact no:</b>
<table>
<tr><td>Contact</td><td> <asp:TextBox ID="txt6" runat="server" /></td></tr> // you can also use a dropdownlist to populate the contact no..Here we are just taking a textbox to enter the contactno against which updation will be done
<tr><td>First Name</td><td> <asp:TextBox ID="txt7" runat="server" /></td></tr>
<tr><td>Middle Name</td><td> <asp:TextBox ID="txt8" runat="server" /></td></tr>
<tr><td>Last Name</td><td> <asp:TextBox ID="txt9" runat="server" /></td></tr>
</table>
<asp:LinkButton ID="lnkAlphabet" runat="server" Text=''Update”/>
// server side coding for insert & delete
protected void Insert(object sender, EventArgs e)
{
string conStr = ConfigurationManager .ConnectionStrings["conStr"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd1 = new SqlCommand("insert into customers(firstname,middlename,lastname,phno)values (‘””+txt1+””’,’”+txt2”’,’”+txt3+”’,’”+txt4+”’)",con);
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
protected void Delete(object sender, EventArgs e)
{
string conStr = ConfigurationManager .ConnectionStrings["conStr"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd2 = new SqlCommand("delete from customers where phno=’”+txt5+”’",con);
con.Open();
cmd2.ExecuteNonQuery();
con.Close();
}
protected void Update(object sender, EventArgs e)
{
string conStr = ConfigurationManager .ConnectionStrings["conStr"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd3 = new SqlCommand("update customers set firstname=’”+txt7+”’, middlename=’”+txt8+”’,lastname=’”+txt9+”’ where phno=’”+txt6+”’ ",con);
con.Open();
cmd3.ExecuteNonQuery();
con.Close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.