How to create setup file(.exe) with Database in Visual studio 2010

By
Today,I am going to show how to create setup file(.exe) with SQL Database in visual studio 2010.In our previous tutorial I have created a setup file(.exe) without including Database in Setup File.This method  is purely different to the previous setup creation method ,But it is very easy .Remember one things ,when you install this setup to your desktop ,first install .NET Framework 4.0 or 4.5 in  your machine otherwise it will give error.
There are some steps to create setup file with SQL Database, which are given below:
Step 1 : First open your visual studio -->File -->New-->Project-->Select Windows Forms Application -->OK-->and drag and drop Label,TextBox & Button control on the Form from toolbox as shown below:



Step 2 : Now open Solution Explorer -->Right Click on project-->Add-->New Items-->Service-based Database-->Click Add.

Note:-
If you are facing problem to Add .mdf file in your solution Explorer then read following links:-
Step 3 : Now Double click on Database.mdf file-->Add New Table as shown below:



Step 4 : Now Enter your table column name-->and press save button as shown below:



Step 5 : Now go left window of your visual studio-->click Data sources (if not available then click  Data Button on top-->Click show Data sources)-->Add New Data source --> Select Data Source -->Next-->Select Dataset -->Next-->Choose connection string -->Next



Now continue with Next Button then you will see as shown below-->Next.


Step 6 :  Now open Solution Explorer -->Add New Web Form --> drag and drop DataGridView control from toolbox-->and configure it as before.



Step 7 : Now open Solution Explore-->Right Click on Project -->Add New Reference-->.NET --> add Two Namespace as given below-->OK
  • System.Configuration
  • System.Configuration.install

    Step 8 : Now Double Click on Submit and Show Table Data Button and write the following codes as given below.
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Configuration;
    namespace winsetup
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString);
                con.Open();
                String str = "insert into student(Name,mobile,city) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
                SqlCommand cmd = new SqlCommand(str, con);
                cmd.ExecuteNonQuery();
                con.Close();
                label4.Text = "Data has been successfully inserted";
            }
            private void button2_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                this.Hide();
                frm2.Show();
            }
        }
    }
    


    Step 9 :  Now open App.config file from Solution Explorer --> change name and add same code in your connection code as shown below:



    Note:- Now i am going to create setup file with SQL Database as shown  below . 

    Step 10 :  Now open Solution Explorer -->Right click on the project -->Properties -->click publish Button From left window as shown below:



    Step 11 : Now First Create New folder (mysetup) on your Desktop .

    Step 12 : Now Click Publishing Folder Location --> Select Your Folder(mysetup) from Desktop as shown below-->click Open.



    Step 13 : Now click Prerequisites Button-->Select following options as shown below-->OK.



    Step 14 : Now click Options -->Write the required field as shown below-->OK



    Step 15 : Now click Publish Wizard.. from bottom --> click Next as shown below.



    Step 16 : Now proceed with Next Button --> click  Finish .



    Step 17 : Now click Publish Now  Button from Bottom --> Your setup will be created in your Desktop folder (mysetup).
    see it:



    Step 18 : Now First install .NET Framework 4.0 or 4.5 in your system,If you are using visual studio 2010 or 2012.After that you can install this setup.It will take few minutes.

    Step 19 :  Now Double click  on setup Icon on the  Desktop .You will see.


    Step 20 : Now Enter the required field and click  Submit Button.You will see.



    Step 21 : Now Click Show Table Data Button.You will see.



    Step 22 : If you want you can create setup file in visual studio 2012 and visual studio 2013 also.
    For More:-
    1. Ado.Net Application
    2. How to Build Real Form Filling application in asp.net like IBPS
    3. Xml Application
    4. E-Post System Project
    5. Take Print receipt in Windows Form Application
    6. oops concepts
    7. Data Integrity in Sql 
    8. File Handling Real Application
    9. Overview of C#
    10. How to use WCF Services in asp.net application
    11. How to use different connection strings in asp.net
    12. Operator overloading in c#
    13. Web Forms controls
     Download whole Attached file 

    Application                                  Setup File    

    63 comments:

    1. Application not formetted well this error occured. please solve it

      ReplyDelete
    2. hi Tej Parikh ! when i have installed this setup on my system then it has not shown any error.i have shown installation on my desktop in above tutorials. pl follow each step very carefully again which is shown as tutorials.first install .net framework 4.0/4.5 in your system.then create setup and install them.it will work..

      ReplyDelete
    3. Can i create a setup file for a website?
      if yes please tell me the procedure to build it otherwise what is the other option to do this?

      ReplyDelete
      Replies
      1. yes, you can create web setup for website also.see it-
        http://4.bp.blogspot.com/-fs9rst_yaGw/Uo9fe5Zf_YI/AAAAAAAACOo/SZu0AzwMVLg/s1600/websetup.PNG

        Delete
    4. hi, nice post but there is a problem that i could find the data in my table offcourse the data is displaying on second form but when i close the application then restart there is no data not even this data is stored in the database. could you please help me.
      Thanks in advance

      ReplyDelete
    5. you can improve the setup, before installing the setup check the .net version if its not 4/4.5 then tell user to download it

      ReplyDelete
    6. hi, when i clik the save button their is not showing any connection error but data is not saved in table. plz help me

      ReplyDelete
    7. yes,, correct @Sukant

      hi RAMASHANKER !!

      we can add the details to table and it is visible in gridview like this,

      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString);
      con.Open();
      String str = "insert into student(name,roll,class) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
      SqlCommand cmd = new SqlCommand(str, con);
      int a=cmd.ExecuteNonQuery();

      SqlDataAdapter ada = new SqlDataAdapter("SELECT *from student", con);

      DataTable dt = new DataTable();
      ada.Fill(dt);
      dataGridView1.DataSource = dt;

      con.Close();
      if (a==1)
      label7.Text = "Data has been successfully inserted";
      else
      label7.Text = "not inserted";



      but when i go
      server Explorer > show table data
      and all values are NULL
      :(

      Please give me a solution.

      cannot we setup a file directly with SQL database ?

      ReplyDelete
      Replies
      1. Use this code instead of DataTable dt = new DataTable();,otherwise it will show null value.
        ada.Fill(dt);
        dataGridView1.DataSource = dt;
        Dataset ds = new Dataset();
        ada.Fill(ds);
        dataGridView1.DataSource = ds;

        Delete
      2. I am facing the same problem. Data is not reflected in database. It shows in the grid view but when we go to server explorer -> show table data the value is not reflected. Plz Help Soon

        Delete
      3. hi akshay ! first tell me what error is showing....

        Delete
      4. well, this is what i did
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet mydataset;

        string sql = null;
        sql = "Select id,name,dates from temp";
        con = new SqlConnection(connectionString);
        con.Open();
        cmd = new SqlCommand(sql, con);
        da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        mydataset = new DataSet();
        da.Fill(mydataset, "data");
        dataGridView1.DataSource = mydataset;
        dataGridView1.DataMember = mydataset.Tables["data"].ToString();
        label5.Text = "Data refreshed";
        con.Close();


        and now i have repeat what Akshay said before "It shows in the grid view but when we go to server explorer -> show table data the value is not reflected"
        And see there is no error at all.

        Delete
    8. hi i want the exact conversion bit for a dot net application, can any you help me

      ReplyDelete
    9. Is this db is always attached with application on various systems? I mean this db will be a local copy whwrever I will install it right?

      ReplyDelete
    10. Ok almost every thing is same in visual studio 2010 and visual studio 2012....
      I follow your steps of
      "How to create setup file(.exe) with Database in Visual studio 2010"
      in visual studio 2012

      But when i click on Publish wizard button...and after that when i click on Finish button.it gives me the following error.
      Error 1 Cannot publish because a project failed to build
      Error 2 Unable to copy file "App.config" to "bin\Debug\app.publish\Application Files\Finding Mac Address of the system_1_0_0_0\Finding Mac Address of the system.exe.config.deploy". The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. Finding Mac Address of the system


      ...please help me what is the problem

      ReplyDelete
      Replies
      1. This is a problem with Visual Studio that can occur when you have add-ins installed. Instead of using the Publish button in the Publish tab, use Build/Publish from the menu on the top of Visual Studio.

        If you use the Publish button, it runs through the add-ins before doing the build (or something like that). If you use Build from the menu instead, it goes straight to msbuild and ignores any add-ins you have installed.

        Hope this helps u..

        Delete
    11. hello sir. I have an error after the publish wizard the error is "Cannot publish because a project failed to build."

      Plz help me...

      ReplyDelete
      Replies
      1. hi chetan, follow each steps carefully,some times it will happened.create new application ,don't use previous application to create the setup.it will definitely work,if any problem then you can contact me through Contact Me form.

        Delete
      2. hello sir, thanx for reply. I solved the above issue. I have another query.

        I want to set the installation path of the my setup file. sir tell me how to do that ? Also i want to know where is database is stored after completion of installation ?


        Plz help me sir it's urgent. thnaxx..

        Delete
      3. Hello sir, Thanx for reply.. i solved the above issue. I have another query.

        I want to set the installation path in setup file and also i want to know where is database is storing after the installation how can i take backup of database ?

        sir plz help me it's urgent..

        Thank you..

        Delete
      4. hi chetan,first create a folder on Your Desktop or any drive --> give that folder path (i have explained it in above tutorial)-->all database file(sql server) + application will stored in this folder --> when you install it any client machine---> all database is stored on installation path (your drive, where you install it c,d or other) ..

        Delete
    12. Sir i already do the above process but it is not working. When i m seeing the path of the project it is "C:\Users\hp\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Chetan\setupfile". I want to install the setup in c drive and program files and also the database in that folder..

      Thank you.

      ReplyDelete
    13. hi RAMSHANKAR

      I want to add framework to my setup file.
      means when i click on setup file then first it check that there is framework installed on my machine or not...
      nd if it is not installed then first it install that nd then my project will install.......................
      plz help me

      ReplyDelete
    14. Hello sir,
      I want to make setup file of my windows application ,I am using entity framework model,C#.NET,Winform ,Database I am using is sql server 2008,What is process of making setup file & setup should work anywhere .... ,I am getting JIT debugger error on installing & ruining application.
      Please help me I am getting JIT debugger error....

      ReplyDelete
      Replies
      1. hi suraj ! i have already mentioned 'How to create setup file with sql database' in above tutorial.follow each step and install .net framework 4.0/4.5 before creating the setup file. jit compiler error is coming because you have not create setup file correctly.

        Delete
    15. I have followed your above Information(Tutorial) for make setup for C# Windows Application which has database in
      SQLExpress2008 but following error is occurred when i open that setup application
      "Unhandled exception has occurred in your application.
      An attempt to attach an auto-named database for file C:\Document and Setting\Rachit-41\LocalSettings\Apps\2.0\YBO9521P.Y3J\db\payrolldatabase.mdf failed.
      A database with the same name exists, or specified file cannot be opened.,or it is located on UNC share."

      ReplyDelete
      Replies
      1. hi Prajakta ! change your database name because one Database.mdf file is exist in your c dive.
        change Database.mdf --> mydb.mdf or another name.then it will work.
        I hope you can do this...

        Delete
      2. Sorry Sir But i have changed the database with another name like mydb1.mdf
        but it shows also above error
        "An attempt to attach an auto-named database for file D:\emppayrollsystems\1 may setup\PayrollManagementSystem\PayrollManagementSystem\mydb1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

        Delete
      3. Hi Prajakta ! First read below link .it may be helpful to solve this problem.
        http://www.msdotnet.co.in/2013/04/how-to-solve-problem-to-add-sql.html

        read this carefully. first create new application and perform this steps.if it will work.then perform this steps in your original project.

        Delete
      4. i have changed database name like mydbpay.mdf. but it gives error when i build the program code.
        It shows following error:-
        "An attempt to attach an auto-named database for file D:\emppayrollsystems\1 may setup\PayrollManagementSystem\PayrollManagementSystem\mydb1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
        What can i do for this?

        Delete
    16. i have a problem that i am creating printable application so please tell me how to design winform so that that page data should be show on a4 size paper bottom.....

      ReplyDelete
      Replies
      1. hi Ankur ! Read below link.It will be helpful for you......
        http://www.msdotnet.co.in/2012/06/how-can-take-print-receipt-and-save.html

        Delete
    17. Thank You for this tutorial. It was GREAT!!!

      ReplyDelete
    18. Sir When i m seeing the path of the project it is "C:\Users\hp\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Chetan\setupfile". I want to install the setup in c drive and program files and also the database in that folder.. How to backup data from this setup after installation

      Pls help me Thank you.

      ReplyDelete
    19. Is there any idea to hide our database files which is stored in C drive....to be safe from unknown user.

      ReplyDelete
    20. Setup is not asking for installation path ???
      Please Help !!!

      ReplyDelete
    21. Sir RAMASHANKER VERMA when i insert data into student tbl then object reference not set to an instance of object error is genrate,how to solve it??

      ReplyDelete
      Replies
      1. hi ,
        check your connection string codes whether you are inserting values in same columns which are created in your table or don't change order of your column and data types,see it carefully.this error basically comes due to change order of values or data types in your connection string.

        Delete
      2. sir its connection string problem,connection method set on form load,when form is run that error is generate object reference not set to an instance of object ,please help

        Delete
    22. will this exe file work on client machine?which have not sql server

      ReplyDelete
      Replies
      1. yes , you can install it on other system .

        Delete
    23. Hello sir,
      Your tutorial was really helpful and easy to understand so that I was able to create my first setup using DB. Now I am developing a C# windows Application which has a third party Database that is sql server management studio 2008.Will it be possible to create a setup for it in the similar way described above with its database having .dbo extension instead of .mdf? pleas help me.

      ReplyDelete
    24. Hi Sir,
      My file.exe created properly.It working fine on my machine but when i am Installing on client machine database module not working
      please reply me

      ReplyDelete
    25. I am using an oracle sql database with my website in visual studio 2012. While creating the .exe file..how is the database getting copied to the client's machine? Is it automatically attaching the database along with the .exe file?

      ReplyDelete
    26. how to add installation wizard,agreement,icon in this type of exe???
      and why this exe is not displayed in program files??

      ReplyDelete
    27. my app.config code is








      but in my code file throwing error object reference is null and if use
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
      is throw error and use another way
      SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
      its working

      ReplyDelete
    28. my App.config file code is as bellow but i am getting error I explain bellow












      but in my code file throwing error object reference is null and if use
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
      is throw error and use another way
      SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
      its working

      ReplyDelete

    29. when i publish my application this will showing this error.

      To enable 'Download prerequisites from the same location as my application' in the Prerequisites dialog box, you must download file 'DotNetFX45\dotNetFx45_Full_x86_x64.exe' for item 'Microsoft .NET Framework 4.5 (x86 and x64)' to your local machine. For more information, see http://go.microsoft.com/fwlink/?LinkId=239883.

      please help me to solve this problem.

      ReplyDelete
      Replies
      1. Hi Ankit,
        Read step 18, first install latest .net framework 4.0 or 4.5 or 4.5.1 on your system, then it will work...

        Delete
    30. hi ramshanker sir
      I check my folder and sqlexpress is not showing in my folder
      please help me to rectify the issue

      ReplyDelete
    31. hi ramshankar sir
      in my folder that is on desktop sqlexpress2008 is not showing
      please help

      ReplyDelete
    32. hi sir
      when i trying to install this exe on other machine
      getting an sql connection error
      i installed .net framework 4.0 on that machine
      please help

      ReplyDelete
    33. Can I implement backup and restore in desktop application, that can be work on any desktop machine, please help me
      . I develop one Invoice application, but backup and restore not work with local database it find sql server & database

      ReplyDelete
    34. How to attach database in set up file

      ReplyDelete
    35. hello sir ,after creating setup it is working on my pc and if i use other pc where vs and sql server not installed and run this setup it showing Network instance error means service based database not working

      ReplyDelete
      Replies
      1. hi Manvendra, your sql database is not running on client machine,first install your setup file on client machine after that turn on it from below file..
        https://www.dropbox.com/s/u58q9k3ncj8w54c/problems.docx?dl=0

        Delete
    36. Is there a way to provide an option to user to just download the setup to user selected folder so that user can later install when we select 'from a website' option while publishing the code..

      ReplyDelete
    37. i want solution for crystal report. whenever i run exe on another system it gives error like failed to open connection

      ReplyDelete
    38. unable to satisfy all prerequisites for WindowsFormApplication2. Setup cannot continue until all system component have been successfully installed

      ReplyDelete
    39. I have an error "Publication not possible due to a failure in generating the project" .
      to activate 'Download the required components from
      the location of my application 'in the Prerequisites dialog box,
      you must download the file ' DotNetFX452 \ NDP452 - KB2901907 -x86 -x64- ENU.exe - Allos ' for
      the item ' Microsoft .NET Framework 4.5.2 (x86 and x64) ' on the local computer

      Ps: I use visual studio 2015

      ReplyDelete
    40. hello,
      I have created setup file and its is created successfully.
      But my query is what should i do for database backup?
      and how to restore the database backup?
      Please suggest me.
      Thanks.

      ReplyDelete

    Powered by Blogger.