Introduction
Life is like a connection of strings. We stitch each and every thread to make the small bits and bytes of our life. Now and then, we all like to keep in contact with our friends, families, colleagues, partners and least of all, our enemies.
From the very beginning, many people have discovered numerous ways to remember contacts. Some used brains, some used paper and pencil and some used computers to do this simple task. Many contact management software are available out in the net that you have already figured out what there will be in this project. But do not stop reading. You might find something useful.
I thought it would be nice to share some useful tricks and trade for developers. So, I came up with a very simple contact management program in C#. I started with the design, development and last of all, implementation.
Everyone likes to invent. Some people copy, some people use previous experience, some people like to start with something to get something out of something. I used all of them.
It is a way of many programmers to get things done quickly. But doing something quickly ends up in more and more debugging and more errors come out from all over the program.
I tried to show a simple way of getting things done in an easy and understandable way. For these, I have three kinds of classes:
1. Info Classes
These classes hold the general information and properties about the classes.
2. Data Access Classes
The Objects of these Classes deal with databases which use Info & Business Objects to do a specific work.
3. Business Classes
The objects of these classes define what Info Objects are supposed to do. Maybe Insert Some Data. It does not know how to deal with databases.
So, what's the point of making the above classes. The answer is to make like easier. There is a common sentence that is used is to KISS which means, Keep It Simple Stupid.
The basic thing is to define some info classes. Make a list of works that these can do and make some business classes and let the data classes define how to deal with the databases to set and get data.
Point of Interests
- Using Business Objects/Data Access/User Interface Layer
- Using the Reporting with parameter values
- Implementing to restrict users from using the software for specified days
- Sending Emails via POP
Interesting and Useful
Invoking a Business class to populate an ArrayList
:
public void PopulateContacts(ListView lvw, int contactType)
{
lvw.Items.Clear();
BusinessLayerContacts b = new BusinessLayerContacts();
ArrayList al = new ArrayList();
Contacts c = new Contacts();
c.ContactType = contactType;
c.UserID = this.UserID;
al = b.GetContacts(c);lvw.Columns.Add("Name", 300);
lvwChoosen.Columns.Add("Name", 300);
for (int i = 0; i < al.Count; i++)
{
Contacts ct = new Contacts();
ct.ID = ((Contacts)al[i]).ID;
ct.Name = ((Contacts)al[i]).Name;
ct.Telephone = ((Contacts)al[i]).Telephone;
ct.UserID = this.UserID;
ListViewItem lvi = new ListViewItem();
lvi.Tag = ct.ID.ToString();
lvi.Text = ct.Name;
lvw.Items.Add(lvi);
}
}
Sending Parameters to the Report
Many times, new programmers find difficulties to send parameters to the report or bind the report at runtime. Here is a way to do it.
...
Envelope ex = new Envelope();
CrystalDecisions.CrystalReports.Engine.ReportDocument r =
new CrystalDecisions.CrystalReports.Engine.ReportDocument();
TableLogOnInfo t = new TableLogOnInfo();
t.ConnectionInfo.ServerName = Application.StartupPath + "\\ZSContacts.mdb";
r.FileName = Application.StartupPath + "\\" + "Envelope.rpt";
r.Database.Tables[0].ApplyLogOnInfo(t);
ex.SetDataSource(dt);
crvReport.ReportSource = r;
ex.VerifyDatabase();
ParameterDiscreteValue d = new ParameterDiscreteValue();
d.Value = this.userid;
crvReport.ParameterFieldInfo["userid"].CurrentValues.Add(d);
ParameterValues currentParameterValues = new ParameterValues();
for (int i = 0; i < al.Count; i++)
{
ParameterDiscreteValue dd = new ParameterDiscreteValue();
dd.Value = ct.ID = ((Contacts)al[i]).ID; ;
currentParameterValues.Add(dd);
}
crvReport.ParameterFieldInfo["contactId"].CurrentValues = currentParameterValues;
...
Implementing a Simple License Trick
Simple registry entry can help you do the licensing if you want to make your product run as a shareware.
...
ArrayList al = new ArrayList();
al.Add("AV32 FGHG RE23 FGHT ASW34");
al.Add("AV32 FDFG RE23 FEHT ASW34");
al.Add("AV32 FFHH RE43 FFHT ASW34");
al.Add("AV32 FFHG RER3 FGHT ASW34");
al.Add("AVS2 FGSG RFD3 FFHT ASWD4");
bool registerd = false;
for (int i = 0; i < al.Count; i++)
{
if (txtLicense.Text == al[i].ToString())
{
RegistryKey rk =
Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("ZakaSoft", true);
if (rk == null)
{
RegistryKey main = Registry.LocalMachine.OpenSubKey("Software", true);
RegistryKey x = main.CreateSubKey("ZakaSoft");
x.SetValue("STATUS", txtName.Text);
}
else
{
rk.SetValue("STATUS", txtName.Text.ToString());
MessageBox.Show("Thanks for Registering!");
frm.btnRegister.Visible = false;
frm.lblUsed.Text = "This Software is Registerd to " + txtName.Text;
registerd = true;
this.Dispose();
}
break;
}
}
if (registerd == false)
{
MessageBox.Show("License key not valid!");
txtLicense.Focus();
}
...
Sending Emails from Application
Here is a small snippet on how to send emails from your application:
...
using System.Net.Mail;
...
private void SendMail()
{
MailAddress from = new MailAddress("sales@zakasoft.com");
MailAddress to = new MailAddress("zakaria7@gmail.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = @"Using this feature,
you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient("mail.zakasoft.com");
System.Net.NetworkCredential basicAuthenticationInfo =
new System.Net.NetworkCredential("sales@zakasoft.com", "1234567");
client.UseDefaultCredentials = false;
client.Credentials = basicAuthenticationInfo;
client.Send(message);
}
Conclusion
I hope you liked this very simple project. I emphasized on using some basic methods and tricks that will help you write and design useful software.
History
- 1st March, 2007: Initial post