//
you're reading...
ASP.NET, IT-Center, SQL Server, Web Based

ASP.NET DropDownList with table data

Step by step assign table data value into dropdownlist using ASP.NET Framework studio. This example using Visual Studio 2005 and Microsoft SQL Server 2008
1. Create Table

USE [dbname]
GO

/****** Object: Table [dbo].[tbl_level] Script Date: 12/02/2011 01:11:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_level](
[lvl_id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[lvl_name] [varchar](50) NULL,
[lvl_description] [varchar](100) NULL,
[lvl_status] [varchar](10) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

2. List of Data Table
Using Stored Procedure to getting list data table

USE [dbname]
GO
/****** Object: StoredProcedure [dbo].[sp_getListOfLevelActive] Script Date: 12/02/2011 01:13:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[sp_getListOfLevelActive]
@parLvlId as varchar(200) = ''
/* Param List */
AS

IF @parLvlId <> ''
SELECT [lvl_id]
,[lvl_name]
,[lvl_description]
,[lvl_status]
FROM [tbl_level]
WHERE [lvl_id] = @parLvlId
and lvl_status='true'
ORDER BY [lvl_name] asc
ELSE
SELECT [lvl_id]
,[lvl_name]
,[lvl_description]
,[lvl_status]
FROM [tbl_level]
where lvl_status='true'
ORDER BY [lvl_name] asc

3. Assign data table
assign data table into dropdownlist using Sqldatareader sqlclient

public sub ComboLevel()

Dim myDb As New globalFunction            'open connection, using your script
Dim myConn As SqlConnection                'open connection, using your script
myConn = myDb.sql_conn                        'open connection, using your script
Dim cmdA As New SqlCommand("sp_getListOfLevelActive", myConn)
cmdA.CommandType = CommandType.StoredProcedure
If myConn.State = ConnectionState.Closed Then myConn.Open()
Dim rdr As SqlDataReader
rdr = cmdA.ExecuteReader(CommandBehavior.CloseConnection)

dropdownlist.Items.Clear()
dropdownlist.Items.Add(New ListItem("-please select one-", ""))

Do While rdr.Read
             dropdownlist.Items.Add(New ListItem(rdr.GetString(rdr.GetOrdinal("LVL_NAME")), rdr.GetString(rdr.GetOrdinal("LVL_ID"))))
Loop
End With

End Function

After finish load the dropdownlist, the way to handle selected option can use this script:

If dropdownlist.Items.FindByValue(ValueData) IsNot Nothing Then
       dropdownlist.SelectedIndex = dropdownlist.Items.IndexOf(ddlBranch.Items.FindByValue(ValueData))
End If

Change “ValueData” with value on selected

About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

December 2011
M T W T F S S
« Nov   Jan »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Web Statistic

Blog Stats

  • 149,580 hits
%d bloggers like this: