Thursday, December 15, 2011

How to optimize code using .dll (data link library) file in asp.net

Here is the method how you can reduce code using .dll file
This also use for code re-usability.

Phase 1:

Step 1: Open project in Visual Studio
            (File->New Project-> Project)

Step2: Select window tab -> Class library














Step3: In Class1.vb
           Create function or method, i am creating method in which i will insert record in table.


Imports System.Data.SqlClient
Public Class Class1


    Public Sub insert(ByVal sqlstr As String,ByVal constring As String)

''''sqlstr contain sqlquery while constring contain connectionstring


        Dim con As New SqlConnection(constring)
        Dim cmd As New SqlCommand(sqlstr, con)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Sub
End Class


Step4: Now Run the program
           it will not run but show dialogue box, just ignore it.

Step5: Open the destination folder where this application is save.
  
Step6: Copy the ClassLibrary1.dll file from (ClassLibrary1\bin\Debug) and paste anywhere in your pc.


Phase 2: 


Step1: Open Website in Visual Studio
            (File->New Website->Asp.net Website)

Step2: Now add ClassLibrary1.dll file, which you have created in phase1 in your application,
           by following way
         .
           
        

    

           














Press ok.
ClassLibrary1.dll get added in bin folder within solution explorer.

Step3: Now import the ClassLibrary1.dll file

Import ClassLibrary1

write the code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       Dim sqlstr,constring as String
       sqlstr="Insert into test values('ani')"
constring="write database connectionstring"
      Dim c As New Class1()
                          '' it will display method used in Class1"
       c1.insert(sqlstr,constring)
    End Sub


Step4: Now run the program
           it will insert record in test table by calling insert method from ClassLibrary1.dll


You can use this .dll file anywhere such as in different application,application  in different pc






No comments:

Post a Comment