473,839 Members | 1,461 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 1472

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

Similar topics

2
2385
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 FillList(Object e) {
3
1672
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
4234
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
2380
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
9697
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10587
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10295
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7829
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7018
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4064
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.