Image

A simple Registration Form using Windows Form C# or VB with MS Access !!

Hey Guys,

It’s long time to see you again with this post. I am in new team and i would like to share my experience that i have gained in my previous team, where i work mostly with windows Form, SQL, Controls, ASP.NET, JS and lot more interesting…

Let’s start this with a simple registration form using Windows Form C# or VB with back end MS Access to store the data. Programmer have come across creating a simple registration Form in there life, either in there work or to learn as an example !! Its looks like simple but it has complication too !!! Let me make it easy to make programmer who haven’t tried so far or those who are interested to learn !!

Window Form journey in designing the registration form !! 

1. Windows Form can be created using Visual Studio and this example can be created either from Visual Studio 2005 to 2015 as your wish !!

2. If  you dont have visual Studio , Please download Visual studio express for free from the link.

3. Once installed , Open the Visual studio IDE and click on New project in the getting started page or File -> New Project. A page with project list will be opened.

Getting Started !!!

Getting Started !!!

4. Click okay to create an Empty Windows Form Application.

Designing the Simple Registration Form in Windows Form !! 

1. Use the toolbar like Label , Text box , button to design the form as given below.

    • Login Page
Login Page

Login Page

    • Main Page – Panel 1
Main Page

Main Page

    • Data Grid View Page – Panel 2
Data Grid View

Data Grid View

2. How to design the registration Form in Single pages and that will provide vision like navigation from screen to screen. I have used the panel upon the designed content to provided the visuals.

3. I suggest you to design the Login Form First then apply a Panel – 1 on it and design the Registration form and the same for Data Grid View Page.

4. When form loads , hide the panels and make the flow visible. Please refer the below code for your reference.

Here is the C# Code:

public partial class Form1 : Form
    {
        OleDbConnection connect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='../../sample2.mdb'");
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "admin" && textBox2.Text == "adminpass")
            {
                this.panel1.Show();
                this.panel2.Hide();
                this.textBox1.Clear();
                this.textBox2.Clear();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" || textBox2.Text != "")
            {
                textBox1.Clear();
                textBox2.Clear();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.panel1.Hide();

            //Open the connection.
            connect.Open();

            //Use a variable to hold the SQL statement.
            string selectString = "SELECT ID,Name,Department FROM Sample";

            //Create an OleDbCommand object.
            //Notice that this line passes in the SQL statement and the OleDbConnection object
            OleDbCommand cmd = new OleDbCommand(selectString, connect);
            OleDbDataReader reader = cmd.ExecuteReader();
            if (!(reader.HasRows))
            {
                textBox4.Text = "1";
            }
            else
            {
                while (reader.Read())
                {
                    this.label11.Text = reader["ID"].ToString();
                    textBox4.Text = (Convert.ToInt16(reader["ID"]) + 1).ToString();
                }
            }
            reader.Close();
            connect.Close();
            ClearAll();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.panel1.Hide();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ClearAll();
        }

        private void ClearAll()
        {
            //Clear
            textBox3.Clear();
            textBox7.Clear();
            textBox5.Clear();
            textBox6.Clear();
            comboBox1.SelectedItem = this.comboBox1.Items[0];
            comboBox2.SelectedItem = this.comboBox2.Items[0];
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Register
            try
            {

                int id = Convert.ToInt16(textBox4.Text);
                string name = textBox3.Text;
                string college = textBox7.Text;
                string department = comboBox2.SelectedItem.ToString();
                int passedOut = Convert.ToInt32(comboBox1.SelectedItem.ToString());
                string mailId = textBox5.Text;

                double contactNo = Convert.ToDouble(textBox6.Text);


                OleDbCommand cmd = new OleDbCommand("INSERT into sample (ID,Name,Department,College,PassedOut,MailId,ContactNo) Values(@id,@name,@department,@college,@passedOut,@mailId,@contactNo)", connect);

                connect.Open();
                if (connect.State == ConnectionState.Open)
                {
                    cmd.Parameters.Add("@id", OleDbType.Integer).Value = id;
                    cmd.Parameters.Add("@name", OleDbType.VarChar).Value = name;
                    cmd.Parameters.Add("@department", OleDbType.VarChar).Value = department;
                    cmd.Parameters.Add("@college", OleDbType.VarChar).Value = college;
                    cmd.Parameters.Add("@passedOut", OleDbType.Integer).Value = passedOut;
                    cmd.Parameters.Add("@mailId", OleDbType.VarChar).Value = mailId;
                    cmd.Parameters.Add("@contactNo", OleDbType.Double).Value = contactNo;
                    try
                    {
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Data Registered !!");
                        connect.Close();
                        UpdateView(id);
                        ClearAll();
                    }
                    catch (OleDbException ex)
                    {
                        MessageBox.Show(ex.Source);
                        connect.Close();
                    }
                    //Clear All
                }
                else
                {
                    MessageBox.Show("Connection Failed");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + "Provide Correct details");
            }
        }

        private void UpdateView(int id)
        {
            this.label11.Text = textBox4.Text;
            textBox4.Text = (Convert.ToInt16(textBox4.Text) + 1).ToString();
            this.panel1.Refresh();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            connect.Open();

            //Use a variable to hold the SQL statement.
            string selectString = "SELECT * FROM Sample";

            //Create an OleDbCommand object.
            //Notice that this line passes in the SQL statement and the OleDbConnection object
            OleDbCommand cmd = new OleDbCommand(selectString, connect);
            OleDbDataAdapter adp = new OleDbDataAdapter(selectString, connect);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            this.dataGridView1.DataSource = dt;
            panel2.Show();
            connect.Close();

        }

        private void button7_Click(object sender, EventArgs e)
        {
            panel2.Hide();
        }
    }

Here is the VB code :

 Partial Public Class Form1
	 Inherits Form
		Private connect As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='../../sample2.mdb'")
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
			If textBox1.Text = "admin" AndAlso textBox2.Text = "adminpass" Then
				Me.panel1.Show()
				Me.panel2.Hide()
				Me.textBox1.Clear()
				Me.textBox2.Clear()
			End If
		End Sub

		Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
			If textBox1.Text <> "" OrElse textBox2.Text <> "" Then
				textBox1.Clear()
				textBox2.Clear()
			End If
		End Sub

		Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
			Me.panel1.Hide()

			'Open the connection.
			connect.Open()

			'Use a variable to hold the SQL statement.
			Dim selectString As String = "SELECT ID,Name,Department FROM Sample"

			'Create an OleDbCommand object.
			'Notice that this line passes in the SQL statement and the OleDbConnection object
			Dim cmd As New OleDbCommand(selectString, connect)
			Dim reader As OleDbDataReader = cmd.ExecuteReader()
			If Not(reader.HasRows) Then
				textBox4.Text = "1"
			Else
				Do While reader.Read()
					Me.label11.Text = reader("ID").ToString()
					textBox4.Text = (Convert.ToInt16(reader("ID")) + 1).ToString()
				Loop
			End If
			reader.Close()
			connect.Close()
			ClearAll()
		End Sub

		Private Sub button5_Click(ByVal sender As Object, ByVal e As EventArgs)
			Me.panel1.Hide()
		End Sub

		Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs)
			ClearAll()
		End Sub

		Private Sub ClearAll()
			'Clear
			textBox3.Clear()
			textBox7.Clear()
			textBox5.Clear()
			textBox6.Clear()
			comboBox1.SelectedItem = Me.comboBox1.Items(0)
			comboBox2.SelectedItem = Me.comboBox2.Items(0)
		End Sub

		Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs)
			'Register
			Try

				Dim id As Integer = Convert.ToInt16(textBox4.Text)
				Dim name As String = textBox3.Text
				Dim college As String = textBox7.Text
				Dim department As String = comboBox2.SelectedItem.ToString()
				Dim passedOut As Integer = Convert.ToInt32(comboBox1.SelectedItem.ToString())
				Dim mailId As String = textBox5.Text

				Dim contactNo As Double = Convert.ToDouble(textBox6.Text)


				Dim cmd As New OleDbCommand("INSERT into sample (ID,Name,Department,College,PassedOut,MailId,ContactNo) Values(@id,@name,@department,@college,@passedOut,@mailId,@contactNo)", connect)

				connect.Open()
				If connect.State = ConnectionState.Open Then
					cmd.Parameters.Add("@id", OleDbType.Integer).Value = id
					cmd.Parameters.Add("@name", OleDbType.VarChar).Value = name
					cmd.Parameters.Add("@department", OleDbType.VarChar).Value = department
					cmd.Parameters.Add("@college", OleDbType.VarChar).Value = college
					cmd.Parameters.Add("@passedOut", OleDbType.Integer).Value = passedOut
					cmd.Parameters.Add("@mailId", OleDbType.VarChar).Value = mailId
					cmd.Parameters.Add("@contactNo", OleDbType.Double).Value = contactNo
					Try
						cmd.ExecuteNonQuery()
						MessageBox.Show("Data Registered !!")
						connect.Close()
						UpdateView(id)
						ClearAll()
					Catch ex As OleDbException
						MessageBox.Show(ex.Source)
						connect.Close()
					End Try
					'Clear All
				Else
					MessageBox.Show("Connection Failed")
				End If
			Catch ex As Exception
				MessageBox.Show(ex.ToString() & "Provide Correct details")
			End Try
		End Sub

		Private Sub UpdateView(ByVal id As Integer)
			Me.label11.Text = textBox4.Text
			textBox4.Text = (Convert.ToInt16(textBox4.Text) + 1).ToString()
			Me.panel1.Refresh()
		End Sub

		Private Sub button6_Click(ByVal sender As Object, ByVal e As EventArgs)
			connect.Open()

			'Use a variable to hold the SQL statement.
			Dim selectString As String = "SELECT * FROM Sample"

			'Create an OleDbCommand object.
			'Notice that this line passes in the SQL statement and the OleDbConnection object
			Dim cmd As New OleDbCommand(selectString, connect)
			Dim adp As New OleDbDataAdapter(selectString, connect)
			Dim dt As New DataTable()
			adp.Fill(dt)
			Me.dataGridView1.DataSource = dt
			panel2.Show()
			connect.Close()

		End Sub

		Private Sub button7_Click(ByVal sender As Object, ByVal e As EventArgs)
			panel2.Hide()
		End Sub
 End Class

Here is the Screen short of the MS Access :

MS Access Design View

MS Access Design View

5. Also i also like to mention the method to find the connection string of the database (MDF,SDF,,mdb) using server explorer.

6. Click View -> Server Explorer (Ctrl + Alt+S) open the server explorer.

7. Right click on the Data Connection -> Add Connection -> Change -> Microsoft Access Database File -> Click Ok.

67

8. Click Browse to select the (mdf, sdf,mdb) file and Test the connection and it will return Success. This will provide result that the database is linked with server explorer.

89

9. After click OK then the Database can be accessed using the server explorer and we can view the database , data , modification can be done here !!  We can see the connection string of the database in the property panel, Which is used to connect the application to the DB !!!

10

10. Here is the sample which i coded to write this post , this will certainly help you to understand better !!

Feel free to post you comment and suggestions , this will help me to provide a detail blog in the future 🙂 Meet you with one more interesting codes and concepts !!!

Leave a comment