943,336 Members | Top Members by Rank

Ad:
1

Reuse your .net code - Part 1

by on Sep 11th, 2006, 3:21 am
When you are programming web pages it is very important to optimize your code for better and faster performance. In this part I will show you how to optimize code for filling multiple dropdownlists with data from Microsoft Access. The same thing is with SQL Server. I'm using asp.net 2.0 and Microsoft Visual Studio 2005.

Add new Module to your project and name it DataOperations (Right Click Project - Add New Item - Class. Change class to module, like this:

VB.NET Syntax (Toggle Plain Text)
  1. Public Module DataOperations
  2.  
  3. End Module

Now write code for filling dropdownlist:

VB.NET Syntax (Toggle Plain Text)
  1. Imports System.Data.OleDb
  2. Imports System.Configuration.ConfigurationManager
  3.  
  4. Public Module DataOperations
  5.  
  6. Public Sub FillDropDown(ByVal ddl As DropDownList, ByVal tbl As String, ByVal txt As String, ByVal val As String)
  7. Dim cn As New OleDbConnection(ConnectionStrings("Data").ConnectionString.ToString)
  8. Dim cmd As New OleDbCommand
  9. cmd.CommandType = Data.CommandType.Text
  10. cmd.Connection = cn
  11. cmd.CommandText = "SELECT " & val & ", " & txt & " FROM " & tbl & " ORDER BY " & txt & " ASC"
  12. Dim dr As OleDbDataReader
  13. cn.Open()
  14. dr = cmd.ExecuteReader(Data.CommandBehavior.CloseConnection)
  15. While dr.Read
  16. ddl.Items.Add(New ListItem(dr(txt).ToString.Trim, dr(val)))
  17. End While
  18. dr.Close()
  19. End Sub
  20.  
  21. End Module

Now all you need to do to fill dropdownlist is to call this sub like:

VB.NET Syntax (Toggle Plain Text)
  1. FillDropDown(Me.cmbCategory, "Categories", "Category", "ID")

You need to pass parameters: dropdown name, table or query for fill, table column for text and table column for value.
If you need you can expand this sub by adding where and order by parameters.

Hope this helps.
News Story Tags: access, code, combo, data, dropdownlist, module, net, reuse, web
 
 
Comments on this News Story
Oct 12th, 2010
0

hi-new user

i'm sorry, i'm new here. i just wanna ask two question...we can develop vb.net as online system?it is because i have one project that i wanna develop. online system using vb.net.i just want a confirmation about this.....

2) if i can develop vb.net online system, can i send message from system to mobile ( like sms system )

tq...plzzz :' (help and give ur opinion...thanks
Newbie Poster
one_ni is offline Offline
1 posts
since Oct 2010
Nov 23rd, 2010
0

Re: Reuse your .net code - Part 1

2)yes you can send messages from system to mobile using a GSM device and creating the appropriate functions to use the GSM device.

1) i don't think that is possible, you should try asp.net which i hear is not that different from vb.net( although it is hearsay)
Light Poster
Mariandi is offline Offline
43 posts
since Nov 2009
Message:
Previous Thread in VB.NET Forum Timeline: protecting .xml data
Next Thread in VB.NET Forum Timeline: Listview





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC