Click here to Skip to main content

MuhammadUsman1 - Professional Profile

Summary

333
Authority
1
Debator
4
Editor
3
Enquirer
54
Organiser
407
Participant
0
Author
No Biography provided
Member since Wednesday, October 31, 2012 (6 months)

Contributions

Articles 0
Tech Blogs 0
Messages 1 (Lurker)
Q&A Questions 8
Q&A Answers 19
Tips/Tricks 0
Comments 41

Links

Reputation

For more information on Reputation please see the FAQ.

Privileges

Members need to achieve at least one of the given member levels in the given reputation categories in order to perform a given action. For example, to store personal files in your account area you will need to achieve Platinum level in either the Author or Authority category. The "If Owner" column means that owners of an item automatically have the privilege, and the given member types also gain the privilege regardless of their reputation level.

ActionAuthorAuthorityDebatorEditorEnquirerOrganiserParticipantIf OwnerMember Types
Have no restrictions on voting frequencysilversilversilversilverAdmin
Store personal files in your account areaplatinumplatinumSitebuilder, Subeditor, Supporter, Editor, Staff
Have live hyperlinks in your biographybronzebronzebronzebronzebronzebronzesilverSubeditor, Protector, Editor, Staff, Admin
Edit a Question in Q&AsilversilversilversilverYesSubeditor, Protector, Editor, Admin
Edit an Answer in Q&AsilversilversilversilverYesSubeditor, Protector, Editor, Admin
Delete a Question in Q&AYesSubeditor, Protector, Editor, Admin
Delete an Answer in Q&AYesSubeditor, Protector, Editor, Admin
Report an Articlesilversilversilversilver
Approve/Disapprove a pending ArticlegoldgoldgoldgoldSubeditor, Mentor, Protector, Editor, Staff, Admin
Edit other members' articlesSubeditor, Protector, Editor, Admin
Create an article without requiring moderationplatinumSubeditor, Mentor, Protector, Editor, Staff, Admin
Report a forum messagesilversilverbronzeProtector, Editor, Admin
Create a new tagsilversilversilversilverAdmin
Modify a tagsilversilversilversilverAdmin

Actions with a green tick can be performed by this member.


 
You must Sign In to use this message board.
Search this forum  
GeneralSave Contacts Using C# Pin
Wednesday, November 28, 2012 6:34 PM by MuhammadUsman1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
 
namespace Personal_Contact_Information
{
public partial class Form1 : Form
{
private string strConString = "";//@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=I:\M USman\PROJECT\New folder\Contacts DataBase M USMAN(4164).accdb;Persist Security Info=False";
private OleDbConnection oleConn;
private OleDbCommand oleCmd;
private OleDbDataAdapter oleDA;
private DataTable dtContact;
private bool blnNew = false;
private bool blnSave = false;
private bool blnCancel = false;
private bool bNotLoad = false;
private bool blnUpdate = false;
private bool blnFileNotFound = false;
public Form1()
{
InitializeComponent();
}
 
private void CreatConn(string sConString)
{
try
{
if (strConString.Contains("MSAccessDataBase"))
{
oleConn = new OleDbConnection(sConString);
if (oleConn.State == ConnectionState.Closed || oleConn.State == ConnectionState.Broken)
{
oleConn.Open();
}
}
else
{
throw new Exception("Data Base File Name is Invalid\nOR Data Base File Dose Not Exist");
}
}
catch (Exception exp)
{
throw;
}
}
 
private void eixtToolStripButto_Click(object sender, EventArgs e)
{
this.Close();
}
 
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (blnSave == true)
{
if ((MessageBox.Show("Do You Want To Save Changes Befor Exit", "Save Personal Contact Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
{
EventArgs ea = new EventArgs();
saveToolStripButton_Click(sender, ea);
return;
}
else
{
if (MessageBox.Show("Are You Sure You Want To Close/Exit", "Pers0nal Contact Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
blnSave = false;
blnCancel = true;
e.Cancel = false;
}
}
}
else if (blnFileNotFound == true)
{
blnCancel = true;
blnSave = false;
e.Cancel = false;
}
else
{
if (MessageBox.Show("Are You Sure You Want To Close/Exit", "Pers0nal Contact Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
blnCancel = true;
e.Cancel = false;
}
}
}
catch (Exception)
{

throw;
}
}
 
private void newToolStripButton_Click(object sender, EventArgs e)
{
try
{
SetControls("NEW");
int Id = 0;
Id = GeneratID();
txtID.Text = Id.ToString();
}
catch (Exception)
{

throw;
}
}
 
private bool IsValid()
{
try
{
if (txtFname.Text.Trim().Length == 0)
{
MessageBox.Show("Please Insert First Name", "First Name Empty", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtFname.Focus();
return false;
}
if (txtLname.Text.Trim().Length == 0)
{
MessageBox.Show("Please Insert Last Name", "Last Name Empty", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtLname.Focus();
return false;
}
if (txtPhoneNo.Text.Trim().Length == 0 && txtLocalNo.Text.Trim().Length == 0)
{
MessageBox.Show("Please Insert Phone No Or Local No", "Contact Cann't be Empty", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (cmbCatagory.Text.Trim().Length == 0)
{
MessageBox.Show("Please Insert Contact Catagory\n(Personal,Offical,Others)", "Catagory Can't be Empty", MessageBoxButtons.OK, MessageBoxIcon.Information);
cmbCatagory.Focus();
return false;
}
}
catch (Exception)
{

throw;
}
return true;
}
 
private bool ExecuteQury(bool blnInsert,string sID, string sFName, string sLName, string sCatagory,string sPhoneNo,string sLocalNo,string sOtherContact, string sAddress)
{
try
{
string sQuery="";
if (blnInsert == true)
{
sQuery = "INSERT INTO ContactInformation VALUES(" + sID + ",'" + sFName + "','" + sLName + "','" + sPhoneNo + "','" + sLocalNo + "','" + sOtherContact + "','" + sAddress + "','" + sCatagory + "')";
}
else if(blnInsert == false)
{
sQuery = "UPDATE ContactInformation set First_Name='" + sFName + "', Last_Name='" + sLName + "', Address='" + sAddress + "', Phone_Number='" + sPhoneNo + "', Local_Number='" + sLocalNo + "', Other_Contact='" + sOtherContact + "',Catagory='" + sCatagory + "' where S_NO=" + sID;
}
if (oleConn.State == ConnectionState.Closed)
{
oleConn.Open();
}
oleCmd = new OleDbCommand(sQuery, oleConn);
oleCmd.CommandType = CommandType.Text;
oleCmd.ExecuteNonQuery();
return true;
}
catch (Exception)
{
throw;
}
return false;
}
 
private bool Delete(string sID)
{
try
{
string sQuery = "Delete From ContactInformation where S_No = " + sID;
if (oleConn.State == ConnectionState.Closed)
{
oleConn.Open();
}
oleCmd = new OleDbCommand(sQuery, oleConn);
oleCmd.CommandType = CommandType.Text;
oleCmd.ExecuteNonQuery();
return true;
}
catch (Exception)
{
throw;
}
return false;
}
 
private int GeneratID()
{
int ID = 0;
try
{
if (oleConn.State == ConnectionState.Closed)
{
oleConn.Open();
}
oleCmd = new OleDbCommand("Select MAX(S_NO) FROM ContactInformation", oleConn);
object obj = oleCmd.ExecuteScalar();
if (obj == null)
{
ID = 100;
}
if (obj == System.DBNull.Value)
{
ID = 100;
}
else
{
ID = Convert.ToInt32(obj);
}
}
catch (Exception)
{
throw;
}
return (++ID);
}
 
private void Clear()
{
txtID.Text = "";
txtFname.Text = "";
cmbS_No.Text = "";
txtLname.Text = "";
cmbCatagory.Text = "";
txtPhoneNo.Text = "";
txtLocalNo.Text = "";
txtOtherContact.Text = "";
txtAddress.Text = "";
}
 
private void updateToolStripButton_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
SetControls("UPDATE");
SelectData();
this.Cursor = Cursors.Arrow;
}
 
private void saveToolStripButton_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
if (blnNew == true&&IsValid())
{
if (ExecuteQury(true,txtID.Text.Trim(), txtFname.Text.Trim(), txtLname.Text.Trim(), cmbCatagory.Text.Trim(), txtPhoneNo.Text.Trim(), txtLocalNo.Text.Trim(), txtOtherContact.Text.Trim(), txtAddress.Text.Trim()))
{
 
MessageBox.Show("Record Inserted SuccessFully", "Personal Record Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Clear();
SetControls("SAVE");
}
else
{
MessageBox.Show("Record Can't Insert SuccessFully", "Error In Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else if ( blnNew == false)
{
if (IsValid())
{
if(cmbS_No.Text.Trim().Length==0)
{
MessageBox.Show("Please Select S.No", "S.No Empty", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (ExecuteQury(false, cmbS_No.Text, txtFname.Text.Trim(), txtLname.Text.Trim(), cmbCatagory.Text.Trim(), txtPhoneNo.Text.Trim(), txtLocalNo.Text.Trim(), txtOtherContact.Text.Trim(), txtAddress.Text.Trim()))
{
MessageBox.Show("Record Updated SuccessFully", "Personal Record Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Clear();
SetControls("SAVE");
}
}
}
this.Cursor = Cursors.Arrow;
}
 
private void SelectData()
{
try
{
bNotLoad = true;
string sQuery = "";
dtContact = new DataTable();
sQuery = "SELECT * FROM ContactInformation";
if (oleConn.State == ConnectionState.Closed)
{
oleConn.Open();
}
oleCmd = new OleDbCommand(sQuery, oleConn);
oleDA = new OleDbDataAdapter(oleCmd);
oleDA.Fill(dtContact);
List listCmbValue = new List();
for (int i = 0; i < dtContact.Rows.Count; i++)
{
DataRow dr = dtContact.Rows[i];
listCmbValue.Add(dr["S_NO"].ToString());
}
if (listCmbValue.Count == 0 && blnUpdate == true)
{
MessageBox.Show("No Record Found To Update\nPlease Insert New Record", "No Record Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
panel1.Enabled = false;
updateToolStripButton.Enabled = true;
blnSave = false;
newToolStripButton.Enabled = true;
searchToolStripButton.Enabled = true;
saveToolStripButton.Enabled = false;
deleteToolStripButton.Enabled = true;
}
{
cmbS_No.DataSource = listCmbValue;
}
}
catch (Exception)
{

throw;
}
}
 
private void Form1_Load(object sender, EventArgs e)
{
try
{
if (File.Exists(Application.StartupPath.ToString() + "\\MSAccessDataBase.accdb"))
{
strConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath.ToString() + "\\MSAccessDataBase.accdb;Persist Security Info=False";
 
if (strConString.Contains("MSAccessDataBase"))
{
CreatConn(strConString);
}
SetControls("LOAD");
}
else
{
MessageBox.Show("Data Base File Not Found\nOR Name of Data Base File is not match with Application Data Base File Name\nPlease Place File With Name ('MSAccessDataBase.accdb') \n\nAt *'" + Application.StartupPath.ToString() + "'*\n\n =-=> And Restart Application ", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
blnFileNotFound = true;
Application.Exit();
}
}
catch (Exception)
{

throw;
}
}
 
private void SetControls(string sAction)
{
switch (sAction.ToUpper())
{
case "LOAD":
{
panel1.Enabled = false;
cmbS_No.Visible = false;
saveToolStripButton.Enabled = false;
toolStripButton1.Enabled = false;
tabControle.SelectedTab = tabNewUpdate;
}
break;
case "NEW":
{
panel1.Enabled = true;
cmbS_No.Visible = false;
txtID.Visible = true;
blnNew = true;
txtID.BackColor = Color.White;
newToolStripButton.Enabled = false;
updateToolStripButton.Enabled = false;
saveToolStripButton.Enabled = true;
toolStripButton1.Enabled = true;
searchToolStripButton.Enabled = false;
blnSave = true;
blnUpdate = false;
deleteToolStripButton.Enabled = false;
Clear();
tabControle.SelectedTab = tabNewUpdate;
}
break;
case "UPDATE":
{
panel1.Enabled = true;
cmbS_No.Visible = true;
txtID.Visible = false;
blnNew = false;
updateToolStripButton.Enabled = false;
newToolStripButton.Enabled = false;
saveToolStripButton.Enabled = true;
toolStripButton1.Enabled = true;
blnUpdate = true;
searchToolStripButton.Enabled = false;
blnSave = true;
Clear();
deleteToolStripButton.Enabled = false;
tabControle.SelectedTab = tabNewUpdate;
}
break;
case "SAVE":
{
panel1.Enabled = false;
saveToolStripButton.Enabled = false;
updateToolStripButton.Enabled = true;
newToolStripButton.Enabled = true;
cmbS_No.Visible = false;
txtID.Visible = true;
txtID.BackColor = Color.FromArgb(255, 240, 240, 240);
toolStripButton1.Enabled = false;
searchToolStripButton.Enabled = true;
blnSave = false;
blnUpdate = false;
SelectData();
deleteToolStripButton.Enabled = true;
tabControle.SelectedTab = tabNewUpdate;
}
break;
case "CANCEL":
{
panel1.Enabled = false;
newToolStripButton.Enabled = true;
updateToolStripButton.Enabled = true;
cmbS_No.Visible = false;
txtID.Visible = true;
txtID.BackColor = Color.FromArgb(255, 240, 240, 240);
saveToolStripButton.Enabled = false;
searchToolStripButton.Enabled = true;
blnNew = false;
blnSave = false;
blnUpdate = false;
Clear();
toolStripButton1.Enabled = false;
deleteToolStripButton.Enabled = true;
tabControle.SelectedTab = tabNewUpdate;
}
break;
case "SEARCH":
{
panel1.Enabled = true;
deleteToolStripButton.Enabled = true;
searchToolStripButton.Enabled = false;
SelectData();
toolStripButton1.Enabled = false;
tabControle.SelectedTab = tabSearch;
}
break;
case "DELETE":
{
searchToolStripButton.Enabled = true;
}
break;
default: break;
}
}
 
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (blnNew || blnUpdate)
{
if (MessageBox.Show("Do You Want To Cancel Operation", "Cancel Editing", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SetControls("CANCEL");
}
}
}
 
private void searchToolStripButton_Click(object sender, EventArgs e)
{
try
{
blnUpdate = false;
SetControls("Search");
if (bNotLoad == false) SelectData();
if (dgvSearch.Rows.Count == 0)
{
DataRow[] drTemp = dtContact.Select("First_Name like '%" + txtFirstName.Text.Trim() + "%' And Last_Name Like '%" + txtLastName.Text.Trim() + "%'");
dgvSearch.Rows.Clear();
foreach (DataRow dr in drTemp)
{
dgvSearch.Rows.Add(dr["S_no"].ToString(), dr["First_Name"].ToString(), dr["Last_Name"].ToString(), dr["Phone_Number"].ToString(), dr["Local_Number"].ToString(), dr["Other_Contact"].ToString(), dr["Catagory"].ToString(), dr["Address"].ToString());
}
}
}
catch (Exception)
{

throw;
}
}
 
private void cmbS_No_SelectedIndexChanged_1(object sender, EventArgs e)
{
try
{
DataRow[] dr = dtContact.Select("s_no=" + cmbS_No.Text);
if (dr.Length > 0)
{
txtFname.Text = dr[0]["First_Name"].ToString();
txtLname.Text = dr[0]["Last_Name"].ToString();
cmbCatagory.Text = dr[0]["Catagory"].ToString();
txtPhoneNo.Text = dr[0]["Phone_Number"].ToString();
txtLocalNo.Text = dr[0]["Local_Number"].ToString();
txtOtherContact.Text = dr[0]["Other_Contact"].ToString();
txtAddress.Text = dr[0]["Address"].ToString();
}
else
{
Clear();
}
}
catch (Exception)
{

throw;
}
}
 
private void tabControle_Click(object sender, EventArgs e)
{
if (blnNew == true && blnSave == true)
{
this.tabControle.SelectedTab = tabNewUpdate;
}
}
 
private void tabControle_Selecting(object sender, TabControlCancelEventArgs e)
{
try
{
if ((blnNew && blnSave)||(blnSave&&blnUpdate))
{
if (e.TabPage == tabSearch)
e.Cancel = true;
}
else if ((!blnSave && !blnNew)||(!blnSave&&blnUpdate))
{
if (e.TabPage == tabNewUpdate&!blnUpdate)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}

}
catch
{
throw;
}
}
 
private void txtFirstName_KeyUp(object sender, KeyEventArgs e)
{
if (dtContact != null)
{
DataRow[] drTemp = dtContact.Select("First_Name like '%" + txtFirstName.Text.Trim() + "%' And Last_Name Like '%" + txtLastName.Text.Trim() + "%'");
dgvSearch.Rows.Clear();
foreach (DataRow dr in drTemp)
{
dgvSearch.Rows.Add(dr["S_no"].ToString(), dr["First_Name"].ToString(), dr["Last_Name"].ToString(), dr["Phone_Number"].ToString(), dr["Local_Number"].ToString(), dr["Other_Contact"].ToString(), dr["Catagory"].ToString(), dr["Address"].ToString());
}
}
}
 
private void txtLastName_KeyUp(object sender, KeyEventArgs e)
{
if (dtContact != null)
{
DataRow[] drTemp = dtContact.Select("First_Name like '%" + txtFirstName.Text.Trim() + "%' And Last_Name Like '%" + txtLastName.Text.Trim() + "%'");
dgvSearch.Rows.Clear();
foreach (DataRow dr in drTemp)
{
dgvSearch.Rows.Add(dr["S_no"].ToString(), dr["First_Name"].ToString(), dr["Last_Name"].ToString(), dr["Phone_Number"].ToString(), dr["Local_Number"].ToString(), dr["Other_Contact"].ToString(), dr["Catagory"].ToString(), dr["Address"].ToString());
}
}
}
 
private void deleteToolStripButton_Click(object sender, EventArgs e)
{
if (dgvSearch.SelectedRows.Count > 0)
{
string sID = dgvSearch.SelectedRows[0].Cells[0].Value.ToString();
if (MessageBox.Show("Are You Sure You Want To Delete Value of " + sID + " Person ", "Delete Contact of " + sID, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (Delete(sID))
{
MessageBox.Show("Record S.No ='" + sID + "' Deleted ", "Contact Deleted Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
else
{
MessageBox.Show("No Record Selected To Delete\nPlease Select Record if any exist", "No Record Selected", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid