How to create exe in C# .net framework?

Here you will learn how to create exe file in visual studio. Making and Running an Executable File. You can make an executable file (.exe) from Visual Studio using the following procedure. I'm using visual studio 2013 for this.

Step 1.

Create a New Window Application Form in Microsoft Visual Studio.

Step 2.

Design your Window Application Form like below form. This is design of my application. You can design your application with your own style. Here i am creating application with database. In this application I am storing and retrieving Name and Phone No of any type.

Step 3.

Open your Microsoft SQL Server and Create a Table like as below.
Select Start Menu → All apps → Microsoft SQL Server 2014 →SQL Server 2014 Management Studio. I am using default database as master database or you can create your own database if you like.

Step 4. Coding Part

After creating table in Sql Server move back to your window application form and double click on Save Data button. Now write following code in button click event. First you need to add one namespace. Open the code view form (Form1.cs) and write the below namespace.

       using System.Data.SqlClient;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;
    
        namespace WindowsFormsApplication1
          {
          public partial class Form1 : Form
          {
          public Form1()
          {
          InitializeComponent();
          }

private void button1_Click_1(object sender, EventArgs e) { if (textBox2.Text != "" || textBox1.Text!="") {
SqlConnection con = new SqlConnection("Data Source=NATURE-VAIO, database=mater, uid=sa,pwd=123456"); con.Open(); SqlCommand cmd = new SqlCommand("insert into student values('" + textBox1.Text + "','" + textBox2.Text + "')", con); cmd.ExecuteNonQuery(); MessageBox.Show("Record Succesfully Saved."); con.Close(); textBox1.Text = ""; textBox2.Text = ""; } else { MessageBox.Show("Enter the name & number."); } }

Step 5.

Now create a second Form 2 for retrieving the data from database. Now double click on Show Data button in first form and write the below coding. This code for calling the second form on button2 click event.

private void button2_Click(object sender, EventArgs e)
{
Form2 ff = new Form2();
this.Hide();
ff.Show();
}

Step 6.

Now design your Form 2 like below. Add two control in form 2. One Gridview control and one Button control for calling the Form1. Double click on form.

Step 7.

Add below code on form load event.

private void Form2_Load(object sender, EventArgs e)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select id as Serial, name as Name, number as Mobile_No from student",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
catch(Exception es)
{
MessageBox.Show(es.Message);
}
}

Step 8.

You are done now. Next step is for making exe file. Go to → Solution explorer → Right click on your project → Properties. One Project window will display like below. Click on application tab and set your application name icon etc.

Step 9.

After that go to publish tab in left side of window. Set the path of your application. Then click on Publish Wizard button.

Step 10.

One dialog box will appear. Set your path or just click next. Now set installation method which you want. See picture below. Now click on Finish button.

Now go to your setup file location. Double click on step file and install it. After completing the installation process. Run your Application. See Picure below.

To view the record click on Show Data button and to add the record click on Add Data button.

Advertisement here

Share this Page.