Hello everyone
This is my problem- I am a C# student and I wrote a program for a address phone book. So when my teacher offered extra credit- I accepted- but the catch is he's not going to help at all. He said we can use any source but him.
This is what I want my program to do--- find information by partial entry.
----I have two tabs (this is my design)
1) to enter company information
2) to find company information
Both tabs have contact name, company name, address, city state, zip, phone, fax, email address
On the enter company information I just want to build my database(add entries)
example- John Smith, Daniweb, 1236 Drive, Memphis, TN, 38111,
901-222-2222, 901-222-1212 (fax), jsmith@aol.com
On the find company information
I want to search by partial information:
example ---- contact name if I enter Jim my program will pull up Jimmy, Jimone, Jimlly
and the same for company name, address, city, state, zip, phone, fax, email address
I also have a enter/clear/exit buttons on my design
I thought this was an easy task but it's turning out to be a NIGHTMARE!
This is my code I wrote for a address book. I am trying to use this as a guide: it not working
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Odbc;
namespace Lab_7__Reading_and_Writing_Database
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Lab_7_Reading_and_Writing_DatabaseData: System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.Label label1Name;
private System.Windows.Forms.Label label2Add;
private System.Windows.Forms.Label label3City;
private System.Windows.Forms.TextBox textBox3City;
private System.Windows.Forms.Label label4State;
private System.Windows.Forms.Label label5Zip;
private System.Windows.Forms.TextBox textBox5Zip;
private System.Windows.Forms.Label label6Phone;
private System.Windows.Forms.Button buttonExit;
private System.Windows.Forms.TextBox textBox2Address;
private System.Windows.Forms.TextBox textBox4State;
private System.Windows.Forms.TextBox textBox6Phone;
private System.Windows.Forms.Button button1Enter;
private System.Windows.Forms.Button button2Clear;
private System.Windows.Forms.TabPage tabPage1LookUpAddress;
private System.Windows.Forms.TabPage tabPage2NewAddress;
private System.Windows.Forms.Label label1NameLook;
private System.Windows.Forms.Label label2AddressLook;
private System.Windows.Forms.Label label3CityLook;
private System.Windows.Forms.Label label4StateLook;
private System.Windows.Forms.Label label5PhoneLook;
private System.Windows.Forms.TextBox textBox2AddLook;
private System.Windows.Forms.Label label6ZipLook;
private System.Windows.Forms.TextBox textBox3CityLook;
private System.Windows.Forms.TextBox textBox4StateLook;
private System.Windows.Forms.TextBox textBox5PhoneLook;
private System.Windows.Forms.TextBox textBox6ZipLook;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private static string conn = @"DriverId=281;DBQ=C:\TEMP\AddressBook.mdb;"+
"UserCommitSync=Yes;FIL=MS Access;UID=admin;"+
"Driver={Microsoft Access Driver (*.mdb)};"+
"MaxBufferSize=2048;Threads=3;SafeTransactions=0";
// Creates an ODBC connection object
private OdbcConnection dbConn=new OdbcConnection(conn);
private OdbcCommand cmd=new OdbcCommand();
private OdbcDataAdapter dataAdapter=new OdbcDataAdapter();
private System.Windows.Forms.ComboBox comboBox1Name;
private System.Windows.Forms.TextBox textBox1NameLook;
private DataSet ds=new DataSet();
public Lab_7_Reading_and_Writing_DatabaseData()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1LookUpAddress = new System.Windows.Forms.TabPage();
this.comboBox1Name = new System.Windows.Forms.ComboBox();
this.textBox6ZipLook = new System.Windows.Forms.TextBox();
this.label6ZipLook = new System.Windows.Forms.Label();
this.textBox5PhoneLook = new System.Windows.Forms.TextBox();
this.textBox4StateLook = new System.Windows.Forms.TextBox();
this.textBox3CityLook = new System.Windows.Forms.TextBox();
this.textBox2AddLook = new System.Windows.Forms.TextBox();
this.label5PhoneLook = new System.Windows.Forms.Label();
this.label4StateLook = new System.Windows.Forms.Label();
this.label3CityLook = new System.Windows.Forms.Label();
this.label2AddressLook = new System.Windows.Forms.Label();
this.label1NameLook = new System.Windows.Forms.Label();
this.tabPage2NewAddress = new System.Windows.Forms.TabPage();
this.textBox1NameLook = new System.Windows.Forms.TextBox();
this.textBox6Phone = new System.Windows.Forms.TextBox();
this.label6Phone = new System.Windows.Forms.Label();
this.textBox5Zip = new System.Windows.Forms.TextBox();
this.label5Zip = new System.Windows.Forms.Label();
this.textBox4State = new System.Windows.Forms.TextBox();
this.label4State = new System.Windows.Forms.Label();
this.textBox3City = new System.Windows.Forms.TextBox();
this.label3City = new System.Windows.Forms.Label();
this.textBox2Address = new System.Windows.Forms.TextBox();
this.label2Add = new System.Windows.Forms.Label();
this.label1Name = new System.Windows.Forms.Label();
this.button1Enter = new System.Windows.Forms.Button();
this.button2Clear = new System.Windows.Forms.Button();
this.buttonExit = new System.Windows.Forms.Button();
this.tabControl1.SuspendLayout();
this.tabPage1LookUpAddress.SuspendLayout();
this.tabPage2NewAddress.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1LookUpAddress);
this.tabControl1.Controls.Add(this.tabPage2NewAddress);
this.tabControl1.Location = new System.Drawing.Point(24, 24);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(576, 304);
this.tabControl1.TabIndex = 0;
//
// tabPage1LookUpAddress
//
this.tabPage1LookUpAddress.Controls.Add(this.comboBox1Name);
this.tabPage1LookUpAddress.Controls.Add(this.textBox6ZipLook);
this.tabPage1LookUpAddress.Controls.Add(this.label6ZipLook);
this.tabPage1LookUpAddress.Controls.Add(this.textBox5PhoneLook);
this.tabPage1LookUpAddress.Controls.Add(this.textBox4StateLook);
this.tabPage1LookUpAddress.Controls.Add(this.textBox3CityLook);
this.tabPage1LookUpAddress.Controls.Add(this.textBox2AddLook);
this.tabPage1LookUpAddress.Controls.Add(this.label5PhoneLook);
this.tabPage1LookUpAddress.Controls.Add(this.label4StateLook);
this.tabPage1LookUpAddress.Controls.Add(this.label3CityLook);
this.tabPage1LookUpAddress.Controls.Add(this.label2AddressLook);
this.tabPage1LookUpAddress.Controls.Add(this.label1NameLook);
this.tabPage1LookUpAddress.Location = new System.Drawing.Point(4, 22);
this.tabPage1LookUpAddress.Name = "tabPage1LookUpAddress";
this.tabPage1LookUpAddress.Size = new System.Drawing.Size(568, 278);
this.tabPage1LookUpAddress.TabIndex = 0;
this.tabPage1LookUpAddress.Text = "Look Up Address";
//
// comboBox1Name
//
this.comboBox1Name.Location = new System.Drawing.Point(168, 24);
this.comboBox1Name.Name = "comboBox1Name";
this.comboBox1Name.Size = new System.Drawing.Size(232, 21);
this.comboBox1Name.TabIndex = 13;
this.comboBox1Name.SelectedIndexChanged += new System.EventHandler(this.comboBox1Name_SelectedIndexChanged);
//
// textBox6ZipLook
//
this.textBox6ZipLook.Location = new System.Drawing.Point(376, 160);
this.textBox6ZipLook.Name = "textBox6ZipLook";
this.textBox6ZipLook.ReadOnly = true;
this.textBox6ZipLook.Size = new System.Drawing.Size(136, 20);
this.textBox6ZipLook.TabIndex = 11;
this.textBox6ZipLook.Text = "";
//
// label6ZipLook
//
this.label6ZipLook.Location = new System.Drawing.Point(344, 160);
this.label6ZipLook.Name = "label6ZipLook";
this.label6ZipLook.Size = new System.Drawing.Size(24, 23);
this.label6ZipLook.TabIndex = 10;
this.label6ZipLook.Text = "ZIP";
//
// textBox5PhoneLook
//
this.textBox5PhoneLook.Location = new System.Drawing.Point(216, 200);
this.textBox5PhoneLook.Name = "textBox5PhoneLook";
this.textBox5PhoneLook.ReadOnly = true;
this.textBox5PhoneLook.Size = new System.Drawing.Size(144, 20);
this.textBox5PhoneLook.TabIndex = 9;
this.textBox5PhoneLook.Text = "";
//
// textBox4StateLook
//
this.textBox4StateLook.Location = new System.Drawing.Point(216, 160);
this.textBox4StateLook.Name = "textBox4StateLook";
this.textBox4StateLook.ReadOnly = true;
this.textBox4StateLook.Size = new System.Drawing.Size(72, 20);
this.textBox4StateLook.TabIndex = 8;
this.textBox4StateLook.Text = "";
//
// textBox3CityLook
//
this.textBox3CityLook.Location = new System.Drawing.Point(216, 120);
this.textBox3CityLook.Name = "textBox3CityLook";
this.textBox3CityLook.ReadOnly = true;
this.textBox3CityLook.Size = new System.Drawing.Size(176, 20);
this.textBox3CityLook.TabIndex = 7;
this.textBox3CityLook.Text = "";
//
// textBox2AddLook
//
this.textBox2AddLook.Location = new System.Drawing.Point(216, 64);
this.textBox2AddLook.Name = "textBox2AddLook";
this.textBox2AddLook.ReadOnly = true;
this.textBox2AddLook.Size = new System.Drawing.Size(224, 20);
this.textBox2AddLook.TabIndex = 6;
this.textBox2AddLook.Text = "";
//
// label5PhoneLook
//
this.label5PhoneLook.Location = new System.Drawing.Point(88, 200);
this.label5PhoneLook.Name = "label5PhoneLook";
this.label5PhoneLook.Size = new System.Drawing.Size(48, 23);
this.label5PhoneLook.TabIndex = 4;
this.label5PhoneLook.Text = "PHONE";
//
// label4StateLook
//
this.label4StateLook.Location = new System.Drawing.Point(96, 152);
this.label4StateLook.Name = "label4StateLook";
this.label4StateLook.Size = new System.Drawing.Size(56, 23);
this.label4StateLook.TabIndex = 3;
this.label4StateLook.Text = "STATE";
//
// label3CityLook
//
this.label3CityLook.Location = new System.Drawing.Point(88, 112);
this.label3CityLook.Name = "label3CityLook";
this.label3CityLook.Size = new System.Drawing.Size(48, 23);
this.label3CityLook.TabIndex = 2;
this.label3CityLook.Text = "CITY";
//
// label2AddressLook
//
this.label2AddressLook.Location = new System.Drawing.Point(80, 64);
this.label2AddressLook.Name = "label2AddressLook";
this.label2AddressLook.TabIndex = 1;
this.label2AddressLook.Text = "ADDRESS";
//
// label1NameLook
//
this.label1NameLook.Location = new System.Drawing.Point(104, 24);
this.label1NameLook.Name = "label1NameLook";
this.label1NameLook.TabIndex = 0;
this.label1NameLook.Text = "NAME";
//
// tabPage2NewAddress
//
this.tabPage2NewAddress.Controls.Add(this.textBox1NameLook);
this.tabPage2NewAddress.Controls.Add(this.textBox6Phone);
this.tabPage2NewAddress.Controls.Add(this.label6Phone);
this.tabPage2NewAddress.Controls.Add(this.textBox5Zip);
this.tabPage2NewAddress.Controls.Add(this.label5Zip);
this.tabPage2NewAddress.Controls.Add(this.textBox4State);
this.tabPage2NewAddress.Controls.Add(this.label4State);
this.tabPage2NewAddress.Controls.Add(this.textBox3City);
this.tabPage2NewAddress.Controls.Add(this.label3City);
this.tabPage2NewAddress.Controls.Add(this.textBox2Address);
this.tabPage2NewAddress.Controls.Add(this.label2Add);
this.tabPage2NewAddress.Controls.Add(this.label1Name);
this.tabPage2NewAddress.Controls.Add(this.button1Enter);
this.tabPage2NewAddress.Controls.Add(this.button2Clear);
this.tabPage2NewAddress.Location = new System.Drawing.Point(4, 22);
this.tabPage2NewAddress.Name = "tabPage2NewAddress";
this.tabPage2NewAddress.Size = new System.Drawing.Size(568, 278);
this.tabPage2NewAddress.TabIndex = 1;
this.tabPage2NewAddress.Text = "New Address";
//
// textBox1NameLook
//
this.textBox1NameLook.AllowDrop = true;
this.textBox1NameLook.Location = new System.Drawing.Point(88, 16);
this.textBox1NameLook.Name = "textBox1NameLook";
this.textBox1NameLook.Size = new System.Drawing.Size(176, 20);
this.textBox1NameLook.TabIndex = 12;
this.textBox1NameLook.Text = "";
//
// textBox6Phone
//
this.textBox6Phone.Location = new System.Drawing.Point(80, 200);
this.textBox6Phone.Name = "textBox6Phone";
this.textBox6Phone.Size = new System.Drawing.Size(184, 20);
this.textBox6Phone.TabIndex = 11;
this.textBox6Phone.Text = "";
//
// label6Phone
//
this.label6Phone.Location = new System.Drawing.Point(8, 200);
this.label6Phone.Name = "label6Phone";
this.label6Phone.Size = new System.Drawing.Size(64, 16);
this.label6Phone.TabIndex = 10;
this.label6Phone.Text = "PHONE";
//
// textBox5Zip
//
this.textBox5Zip.Location = new System.Drawing.Point(248, 160);
this.textBox5Zip.Name = "textBox5Zip";
this.textBox5Zip.Size = new System.Drawing.Size(192, 20);
this.textBox5Zip.TabIndex = 9;
this.textBox5Zip.Text = "";
//
// label5Zip
//
this.label5Zip.Location = new System.Drawing.Point(200, 160);
this.label5Zip.Name = "label5Zip";
this.label5Zip.Size = new System.Drawing.Size(40, 16);
this.label5Zip.TabIndex = 8;
this.label5Zip.Text = "ZIP";
//
// textBox4State
//
this.textBox4State.Location = new System.Drawing.Point(80, 152);
this.textBox4State.Name = "textBox4State";
this.textBox4State.Size = new System.Drawing.Size(72, 20);
this.textBox4State.TabIndex = 7;
this.textBox4State.Text = "";
//
// label4State
//
this.label4State.Location = new System.Drawing.Point(16, 160);
this.label4State.Name = "label4State";
this.label4State.Size = new System.Drawing.Size(48, 23);
this.label4State.TabIndex = 6;
this.label4State.Text = "STATE";
//
// textBox3City
//
this.textBox3City.Location = new System.Drawing.Point(88, 104);
this.textBox3City.Name = "textBox3City";
this.textBox3City.Size = new System.Drawing.Size(352, 20);
this.textBox3City.TabIndex = 5;
this.textBox3City.Text = "";
//
// label3City
//
this.label3City.Location = new System.Drawing.Point(16, 112);
this.label3City.Name = "label3City";
this.label3City.Size = new System.Drawing.Size(40, 16);
this.label3City.TabIndex = 4;
this.label3City.Text = "CITY";
//
// textBox2Address
//
this.textBox2Address.Location = new System.Drawing.Point(88, 64);
this.textBox2Address.Name = "textBox2Address";
this.textBox2Address.Size = new System.Drawing.Size(352, 20);
this.textBox2Address.TabIndex = 3;
this.textBox2Address.Text = "";
//
// label2Add
//
this.label2Add.Location = new System.Drawing.Point(16, 64);
this.label2Add.Name = "label2Add";
this.label2Add.Size = new System.Drawing.Size(64, 23);
this.label2Add.TabIndex = 2;
this.label2Add.Text = "ADDRESS";
//
// label1Name
//
this.label1Name.Location = new System.Drawing.Point(32, 16);
this.label1Name.Name = "label1Name";
this.label1Name.Size = new System.Drawing.Size(40, 23);
this.label1Name.TabIndex = 1;
this.label1Name.Text = "NAME";
//
// button1Enter
//
this.button1Enter.Location = new System.Drawing.Point(160, 232);
this.button1Enter.Name = "button1Enter";
this.button1Enter.Size = new System.Drawing.Size(75, 32);
this.button1Enter.TabIndex = 2;
this.button1Enter.Text = "ENTER";
this.button1Enter.Click += new System.EventHandler(this.button1Enter_Click_1);
//
// button2Clear
//
this.button2Clear.Location = new System.Drawing.Point(264, 240);
this.button2Clear.Name = "button2Clear";
this.button2Clear.Size = new System.Drawing.Size(75, 24);
this.button2Clear.TabIndex = 3;
this.button2Clear.Text = "CLEAR";
this.button2Clear.Click += new System.EventHandler(this.button2Clear_Click_1);
//
// buttonExit
//
this.buttonExit.Location = new System.Drawing.Point(192, 360);
this.buttonExit.Name = "buttonExit";
this.buttonExit.Size = new System.Drawing.Size(160, 23);
this.buttonExit.TabIndex = 1;
this.buttonExit.Text = "EXIT";
this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
//
// Lab_7_Reading_and_Writing_DatabaseData
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(672, 402);
this.Controls.Add(this.buttonExit);
this.Controls.Add(this.tabControl1);
this.Name = "Lab_7_Reading_and_Writing_DatabaseData";
this.Text = "My Address Book";
this.Load += new System.EventHandler(this.Form1_Load);
this.tabControl1.ResumeLayout(false);
this.tabPage1LookUpAddress.ResumeLayout(false);
this.tabPage2NewAddress.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void main()
{
Application.Run(new Lab_7_Reading_and_Writing_DatabaseData());
}
private void Form1_Load(object sender, System.EventArgs e)
{
LoadAddresses();
}
private void LoadAddresses()
{
comboBox1Name.Items.Clear();
string sqlGetAddresses="SELECT * FROM addresses";
string strError="";
cmd.CommandType=CommandType.Text;
cmd.CommandText=sqlGetAddresses;
cmd.Connection=dbConn;
dataAdapter.SelectCommand=cmd;
ds.Clear();
try
{
dbConn.Open();
dataAdapter.Fill(ds, "addressEntry");
}
catch(System.Data.Odbc.OdbcException ex)
{
strError=ex.Message.ToString();
MessageBox.Show(strError, "My Address Book", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
finally
{
dbConn.Close();
}
int totalRecords=ds.Tables["addressEntry"].Rows.Count;
if(totalRecords !=0)
{
for(int f=0;f<totalRecords;f++)
{
comboBox1Name.Items.Add(ds.Tables["addressEntry"].Rows[f][0].ToString());
}
comboBox1Name.SelectedIndex = 0;
}
else
{
MessageBox.Show("There is no data in the database!","My Address Book",
MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
private void LoadDataIntoDatabase()
{
string strError="";
string sqlUpdateMemo="INSERT INTO addresses VALUES ("+
"'"+textBox1NameLook.Text + "'," +
"'"+textBox2Address.Text + "'," +
"'"+textBox3City.Text + "'," +
"'"+textBox4State.Text + "'," +
"'"+textBox5Zip.Text + "'," +
"'"+textBox6Phone.Text + "')";
cmd.CommandType=CommandType.Text;
cmd.CommandText=sqlUpdateMemo;
cmd.Connection=dbConn;
dataAdapter.SelectCommand=cmd;
try
{
dbConn.Open();
dataAdapter.Fill(ds, "addAddress");
}
catch(System.Data.Odbc.OdbcException ex)
{
strError=ex.Message.ToString();
MessageBox.Show(strError,"My Address Book", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
finally
{
//close the connection
dbConn.Close();
}
LoadAddresses();
}
public void GetAddresses()
{
string sqlGetAddresses = "SELECT address, city, state, zip, phone FROM addresses WHERE name = '" + comboBox1Name.SelectedItem + "'";
string strError=""; //Clear the data set
ds.Clear();
cmd.CommandType=CommandType.Text;
cmd.CommandText=sqlGetAddresses;
cmd.Connection=dbConn;
dataAdapter.SelectCommand=cmd;
try
{
dbConn.Open();
dataAdapter.Fill(ds, "addresses");
}
catch(System.Data.Odbc.OdbcException ex)
{
strError=ex.Message.ToString();
MessageBox.Show(strError,"My Address Book",MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
finally
{
//Close the connection
dbConn.Close();
}
int totalRecords = ds.Tables["addresses"].Rows.Count;
if(totalRecords !=0)
{
textBox2AddLook.Text=ds.Tables["addresses"].Rows[0][0].ToString();
textBox3CityLook.Text=ds.Tables["addresses"].Rows[0][1].ToString();
textBox4StateLook.Text=ds.Tables["addresses"].Rows[0][2].ToString();
textBox6ZipLook.Text=ds.Tables["addresses"].Rows[0][3].ToString();
textBox5PhoneLook.Text=ds.Tables["addresses"].Rows[0][4].ToString();
}
}
private void comboBox1Name_SelectedIndexChanged(object sender, System.EventArgs e)
{
GetAddresses();
}
private void buttonExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button1Enter_Click_1(object sender, System.EventArgs e)
{
if((!textBox1NameLook.Text.Equals(""))&&(!textBox2Address.Text.Equals(""))&&
(!textBox3City.Text.Equals(""))&&(!textBox4State.Text.Equals(""))&&
(!textBox5Zip.Text.Equals(""))&&(!textBox6Phone.Text.Equals("")))
{
LoadDataIntoDatabase(); // load the data into the database
}
else
{
string strError = "You must enter all fields!";
MessageBox.Show(strError, "My Address Book", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2Clear_Click_1(object sender, System.EventArgs e)
{
textBox1NameLook.Text="";
textBox2Address.Text="";
textBox3City.Text="";
textBox4State.Text="";
textBox5Zip.Text="";
textBox6Phone.Text="";
}
}
}