Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / Win32

Address Book and Events Reminder

Rate me:
Please Sign up or sign in to vote.
4.74/5 (37 votes)
13 Feb 2009CPOL16 min read 105.1K   10.7K   106  
Allows to maintain and backup your contacts and also maintains a reminder. You can store data in any of the three different databases like Microsoft SQL Server, MySql, Microsoft Access
using System;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AddressBook
{
    public partial class frmSQLSetting : Form
    {
        SqlConnection ConnectDB;

        public frmSQLSetting()
        {
            InitializeComponent();
            lstAuthenticationType.SelectedIndex = 0;
            if (((ConnectionTypes)DataConnection.ConnectionType) == ConnectionTypes.SQLConnection)
            {
                txtDataSource.Text = DataConnection.DataSource;                
                txtUserName.Text = DataConnection.UserName;
                txtPassword.Text = DataConnection.Password;
                lstAuthenticationType.SelectedIndex = DataConnection.SQLAuthenticationType;
                GetDBNames();
                lstDatabaseList.SelectedItem = DataConnection.Database;
            }
        }

        void Authentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstAuthenticationType.SelectedIndex == 0)
                txtUserName.Enabled = txtPassword.Enabled = false;
            else txtUserName.Enabled = txtPassword.Enabled = true;
        }

        void GetDBNames(object sender, EventArgs e)
        {
            GetDBNames();
        }

        void GetDBNames()
        {

            lstDatabaseList.Items.Clear();
            ConnectDB = new SqlConnection(GetConnectionString());
            try
            {
                ConnectDB.Open();
            }
            catch (Exception) { return; }
            SqlCommand ExecCmd = new SqlCommand("SELECT NAME FROM SYSDATABASES", ConnectDB);
            SqlDataReader Reader = ExecCmd.ExecuteReader();
            while (Reader.Read())
            {
                lstDatabaseList.Items.Add(Reader.GetString(0));
            }
            ConnectDB.Close();
        }

        string GetConnectionString()
        {
            if (lstAuthenticationType.SelectedIndex == 1)
                return "Data Source=" + txtDataSource.Text + ";Initial Catalog=master;User ID=" + txtUserName.Text + ";Password=" + txtPassword.Text;
            else return "Data Source=" + txtDataSource.Text + ";Initial Catalog=master;Integrated Security=True";
        }

        void SaveChanges(object sender, EventArgs e)
        {
            if (lstDatabaseList.SelectedItem == null) { MessageBox.Show("Please select a valid connection configuration.", "Invalid Connection"); return; }            
            DataConnection.ConnectionType = (int)ConnectionTypes.SQLConnection;
            DataConnection.SQLAuthenticationType = lstAuthenticationType.SelectedIndex;
            DataConnection.DataSource = txtDataSource.Text;
            DataConnection.Database = lstDatabaseList.SelectedItem.ToString();
            DataConnection.UserName = txtUserName.Text;
            DataConnection.Password = txtPassword.Text;
            DataConnection.SaveSettings();
        }

        void ChangesMade(object sender, EventArgs e)
        {
            lstDatabaseList.Items.Clear();
        }

        void Login_Cancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India


Completed B.Com(CS) at DGVC and GNIIT Software Engineering at NIIT. Resident at Chennai and working as a Software Engineer.



 Language / Technology :

C#, ADO.NET, ASP.NET, MVC, WCF, ASP, PHP, XML, Java, J2EE, HTML, JavaScript, JQuery, AngularJS, VB Script, C++, MS SQL Server, SSRS, MySql, Oracle, Oracle Forms Development, Windows, Linux.



Click here to view other articles.


Mail Me at:  shridhar_tl@ymail.com


Visit my Site:  www.iCodeIt.in


Comments and Discussions