| | |
Reuse your .net code - Part 1
Please support our VB.NET advertiser: Intel Parallel Studio Home
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:
Now write code for filling dropdownlist:
Now all you need to do to fill dropdownlist is to call this sub like:
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.
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)
Public Module DataOperations End Module
Now write code for filling dropdownlist:
VB.NET Syntax (Toggle Plain Text)
Imports System.Data.OleDb Imports System.Configuration.ConfigurationManager Public Module DataOperations Public Sub FillDropDown(ByVal ddl As DropDownList, ByVal tbl As String, ByVal txt As String, ByVal val As String) Dim cn As New OleDbConnection(ConnectionStrings("Data").ConnectionString.ToString) Dim cmd As New OleDbCommand cmd.CommandType = Data.CommandType.Text cmd.Connection = cn cmd.CommandText = "SELECT " & val & ", " & txt & " FROM " & tbl & " ORDER BY " & txt & " ASC" Dim dr As OleDbDataReader cn.Open() dr = cmd.ExecuteReader(Data.CommandBehavior.CloseConnection) While dr.Read ddl.Items.Add(New ListItem(dr(txt).ToString.Trim, dr(val))) End While dr.Close() End Sub End Module
Now all you need to do to fill dropdownlist is to call this sub like:
VB.NET Syntax (Toggle Plain Text)
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.
Similar Threads
- hide/show part of form using client side code for ASP.NET (ASP.NET)
- can somebody explain a part of my C++ code to me? (C++)
- Help with php and this part of the code...? (PHP)
- Adding a part to code (C++)
- are C#, VB.NET, ASP.NET part of new VS.NET? (ASP.NET)
| Thread Tools | Search this Thread |
access adult advertising analytics api apple application array basic blog blogger blogging browser browsers business c# cheap code combo crime data database design development directory domain domains drive dropdownlist eu europe firefox forms fraud free gdata google government hack hard hardware hitachi hosting html ie8 india intel internet iphone java legal linux malware marketing microsoft mobile module mozilla msn net news opera php ppc privacy problem programming python reseller rss search security seo service sex shared shopping site software source storage survey teenagers terabyte uk vb vb.net vb6 video virus visual w3c web website wiki wikipedia windows xml yahoo youtube




