473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Filling drop down list from database

I have a two drop down list box.
The first contains district name(district id is the value)
and the the second will contain the user name based on the district selected in the first drop down list.

I am trying to code this in vb.net. It is a web based application.
I've gotten the district info to load, but I can't figure out how to get the users to load. I've created a stored procedure to get user...

Stored procedure info.
SELECT UserFileID, UserFileName
FROM UserFile
WHERE UserFileStatus = 1 and DistrictID = @districtID
GO

Public Sub GetdistrictUsers()
Dim sqlconn As New Data.SqlClient.SqlConnection

sqlconn.ConnectionString = "Server=" & ServerName & ";User id=sa;Password=" & pswd & "; Initial Catalog=" & DBName & ";"

'Here you are creating a command object (cmd) to go with the connection object (sqlconn)

'Dim cmdGetDistricts As SqlCommand = sqlconn.CreateCommand
Dim cmdgetDistrictUsers As Data.SqlClient.SqlCommand = sqlconn.CreateCommand


'this tells SQL Server that the command is a stored procedure

cmdgetDistrictUsers.CommandType = Data.CommandType.StoredProcedure

'This tells Sql server the name of your stored procedure

cmdgetDistrictUsers.CommandText = "GetDistrictUsers"

'Since the stored procedure takes an input parameter you add the parameter here

cmdgetDistrictUsers.Parameters.Add(New Data.SqlClient.SqlParameter("@districtID", Data.SqlDbType.Int, 4))

'or you can add it like you did before using the "With" statement - you can add all of these with "With"

'With cmdGetDistrictInfo.Parameters

'*** .Add("@districtid", SqlDbType.Int, 4)


'Now supply a value for your input parameter - it will be the "VALUE" of the selected

'District in the District drop down

'cmdgetDistrictUsers.Parameters("@districtID").Val ue = ddlDistrict.SelectedValue

cmdgetDistrictUsers.Parameters("@districtID").Valu e = ddlDistrict.SelectedValue

'Now you're read to execute the command - first create a data adapter

Dim da As Data.SqlClient.SqlDataAdapter = New Data.SqlClient.SqlDataAdapter

'Put the command in the data adapter

da.SelectCommand = cmdgetDistrictUsers
'Create a dataset to hold your data coming back

Dim ds As Data.DataSet = New Data.DataSet
'Dim ds1 As DataSet = New DataSet

'Then fill the dataset putting your returned data from the data adapter in a table that you might call "Users"
Try


ddlUser.DataSource = "Users"
ddlUser.DataMember = "UserFileName"
ddlUser.DataValueField = "UserFileId"
ddlUser.SelectedIndex = ds.Tables("users").Rows.Count - 1


ddlApproveBy.DataSource = "Users"
ddlApproveBy.SelectedIndex = 0
ddlApproveBy.DataMember = "UserFileName"
ddlApproveBy.DataValueField = "UserFileId"
ddlApproveBy.SelectedIndex = ds.Tables("Approvers").Rows.Count - 1

Catch esystem As Exception

Debug.WriteLine("An exception of type " & esystem.Message.ToString & " was encountered while attempting to access database.")
'MessageBox("An exception has occurred.", _
'"Source: " & esystem.Source _
'& esystem.Message)
End Try

End Sub

Thanks in advance.
May 29 '07 #1
0 1446

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ramesh | last post by:
hi, I am using Com+ in my application. It will have InsertRecords,selectRecords,updateRecords function. In the Web Form i have Drop-down list. I want to select records from SQL and add it to this...
3
by: Russ | last post by:
I have a drop down list on a web page that I pre-fill onload from a SQL table using a SqlDataReader. The line of code that fills the drop down looks like this while(myReader.Read()) {...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
13
by: Oliver Hauger | last post by:
Hello, In my html form I show a select-element and if this element is clicked I fill it per JavaScript/DOM with option-elements. I use the following code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.