How to create dll file in .Net windows application and reuse that dll code?
In this article I have explained about how to create dll file using class library control and reuse that dll code in form controls. In this article I have wrote common code for connecting access databases, inserting/ retrieving values from that database using class library code.
Description
DLL is nothing but it is dynamic link library, once we create dynamic library file then we can use many project. In this project I have create one class library with database access code and build that class library create dll. For example we need common code for access database in every project so I create that code as dll and reuse that dll in my all windows application project.
First create class library file New->Project -> select Visual C# (Windows left side) and choose template as class library and put it name click ok.
Write below code in that class library file
using System.Data.OleDb;
using System.Data;
using System.Configuration;
using System.Windows.Forms;
namespace ClassLibrary1
{
public class Class1
{
static string s = Application.StartupPath + "\\Data\\test.mdb";
static string strconnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + s + ";";
OleDbConnection con = new OleDbConnection(strconnection);
OleDbCommand cmd=new OleDbCommand();
OleDbDataAdapter da=new OleDbDataAdapter();
DataTable dt=new DataTable();
//Creat common method for insert/update/delete records
public void exeCommand(string query)
{
try
{
//Open connection
con.Open();
cmd = new OleDbCommand(query, con);
cmd.CommandType = CommandType.Text;
//Execute command
cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
con.Close();
}
}
//Creat common method for retrieve records
public DataTable ReturnData(string query)
{
try
{
//Open connection
con.Open();
cmd=new OleDbCommand(query,con);
da = new OleDbDataAdapter(cmd);
dt.Clear();
//Bind values in data table using data adpapter
da.Fill(dt);
con.Close();
}
catch
{
}
finally
{
con.Close();
}
return dt;
}
}
}
Note: If you don't have "Windows.Forms" then add reference to the class library. This namespace is include "Application.StartupPath"
After write code go to your project property i.e. select your project right click choose properties. Below screen appear.
In application tab click "Assembly Information" then new dialogue box appear select Make assembly com-visible check box. Because we need to implement this dll in other projects, so we need to check to access methods and variables only com selected.
Then go to next tab Build (windows 7 tab as Compile) and check the "Register for COM Interop" check box.
After complete code and changes properties just build it your class library file
Now check the bin folder dll is automatically created and available in that folder.
Now we can create one new windows form application. Create New->Project -> select Visual C# (Windows left side) and choose "WindowsFormsApplication" template and put it name "DLLImplement" then click ok.
Now first add your dll file as reference. Right click of your project name choose Add Reference new dialog box appear in that dialogue box browse your dll file location and add it to your project like below
Check that reference add it your project
Now place one data grid view control in form.
Write server side code to access dll class and its methods
//Import your dll class namespace
using ClassLibrary1;
namespace DLLImplement
{
public partial class Form1 : Form
{
//Create object for that dll class
Class1 obj = new Class1();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//during form load i have call my employee table data using that dll like below
dataGridView1.DataSource = obj.ReturnData("select * from emp");
}
}
}
Output:
Output of the above code look like below
Source code:
Download the attached source code and try to create dll files.
Front End: windows forms
Code Behind: C#
Conclusion:
I hope this article is help to know about dll creation in windows form application.
Thanks for you post!