Upload File in C#.Net Windows Application.

In this tutorial you will learn how to upload file using C# .NET Window Application in database. Like pdf, doc, excel etc. We are saving only file path in database and actual file saving in folder.

Step 1.

Create a New window form application C# in Microsoft Visual Studio.

new form

Step 2.

Now design your window form application like below form.

upload document

Step 3.

Now create a folder in solution explorer with any name. My folder name is Document. To create a folder simply move to solution explorer window or press Ctrl + Alt + L. Right click on your project, then click on Add and then click on New Folder. Give the folder name as you want.

solution document folder

Step 4.

Open your Microsoft SQL Server and create a table like as below. My default database is master.

document table

Step 5.

Now move to window form application (Form1.cs) and add following namespace in coding window. Simply double click on window form.

	
	using System.Data.SqlClient;
	
namespace

Step 6.

Now create a connection string to connect with database. Make your Connection globally like below so that you can access it anywhere in the form.

The connection string that includes the source database name, and other parameters needed to establish the initial connection.

	
	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;

	namespace Intsert
	{
    	public partial class Form1 : Form
    	{
        	public Form1()
        	{
            	InitializeComponent();
        	}

        	//globaly define conection
        	SqlConnection con = new SqlConnection("data source=DESKTOP-F3B3CCJ\SQLEXPRESS; database=master; uid=sa; password=123456;");
        	
    	}
	}
	

You will find your connection details in Sql Server. Open your Microsoft SQL Server.

Go to file menu → Connect Object Explorer….

connection

Step 7.

Now move to window form design view and double click on browse button. Add following code on your browse button click.

	
	private void button1_Click(object sender, EventArgs e)
        {
           //To where your opendialog box get starting location. My initial directory location is desktop.
            openFileDialog1.InitialDirectory = "C://Desktop";
            //Your opendialog box title name.
            openFileDialog1.Title = "Select file to be upload.";
            //which type file format you want to upload in database. just add them.
            openFileDialog1.Filter = "Select Valid Document(*.pdf; *.doc; *.xlsx; *.html)|*.pdf; *.docx; *.xlsx; *.html";
            //FilterIndex property represents the index of the filter currently selected in the file dialog box.
            openFileDialog1.FilterIndex = 1;
            try
            {
                if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if(openFileDialog1.CheckFileExists)
                    {                        
                        string path = System.IO.Path.GetFullPath(openFileDialog1.FileName);
                        label1.Text = path;
                    }
                }
                else
                {
                    MessageBox.Show("Please Upload document.");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
	

Step 8. Final Step

Now move to window form design view and double click on upload button. Add following code on your upload button click.

	
	private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string filename = System.IO.Path.GetFileName(openFileDialog1.FileName);
                if (filename == null)
                {
                    MessageBox.Show("Please select a valid document.");
                }
                else
                {
            //we already define our connection globaly. We are just calling the object of connection.
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into doc (document)values('\\Document\\" + filename + "')",con);
                    string path = Application.StartupPath.Substring(0, (Application.StartupPath.Length - 10));
                    System.IO.File.Copy(openFileDialog1.FileName, path + "\\Document\\" + filename);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Document uploaded.");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
	

Output:

Now run you window form application. Press f5 key from keyword or from start button in Visual Studio.

Now browse the file and click on upload button to upload the file in database.

upload document

Now move to Sql Server and check file is uploaded. Also check the path folder where actual file is saving.

sql document

Watch Tutorial Video on YouTube

Watch and Subscribe Now

Post Your Comment