473,566 Members | 3,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Filling drop down list from database

20 New Member
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 GetdistrictUser s()
Dim sqlconn As New Data.SqlClient. SqlConnection

sqlconn.Connect ionString = "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.CreateC ommand
Dim cmdgetDistrictU sers As Data.SqlClient. SqlCommand = sqlconn.CreateC ommand


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

cmdgetDistrictU sers.CommandTyp e = Data.CommandTyp e.StoredProcedu re

'This tells Sql server the name of your stored procedure

cmdgetDistrictU sers.CommandTex t = "GetDistrictUse rs"

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

cmdgetDistrictU sers.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 cmdGetDistrictI nfo.Parameters

'*** .Add("@district id", 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

'cmdgetDistrict Users.Parameter s("@districtID" ).Value = ddlDistrict.Sel ectedValue

cmdgetDistrictU sers.Parameters ("@districtID") .Value = ddlDistrict.Sel ectedValue

'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.SelectComman d = cmdgetDistrictU sers
'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.DataSou rce = "Users"
ddlUser.DataMem ber = "UserFileNa me"
ddlUser.DataVal ueField = "UserFileId "
ddlUser.Selecte dIndex = ds.Tables("user s").Rows.Cou nt - 1


ddlApproveBy.Da taSource = "Users"
ddlApproveBy.Se lectedIndex = 0
ddlApproveBy.Da taMember = "UserFileNa me"
ddlApproveBy.Da taValueField = "UserFileId "
ddlApproveBy.Se lectedIndex = ds.Tables("Appr overs").Rows.Co unt - 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 1460

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

Similar topics

2
2364
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 list. I know how to fill the data in the drop-down list. But i don't know how to pass this control to Com+. Do we need to pass like private...
3
1656
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()) { ddlCompany.Items.Add(new ListItem(myReader.ToString(), myReader.ToString())) }
5
4212
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); System.Data.SqlClient.SqlCommand dbCommand1 = new System.Data.SqlClient.SqlCommand();
13
2329
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 HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.