| | |
Problems retrieving from database
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 13
Reputation:
Solved Threads: 0
Im doing an application that runs on vb.net using oracle database.
The function that i did is to select 3 combo boxes for the information to come out.
The problem is i can only retrieve the records from the first selectedindex=0 of the ifelse statement and not from the others.
That means if i choose finance & administration, the next combobox will show controlling and accounting and the last combobox will show the first record of the IDM_IDEA_DATA table. When i changed the sec combobox to procurement & logistics, there are no changes.
The form is at my attachments.
Here are my codes:
I really need help on this. Hope u guys can help me out. Thx
The function that i did is to select 3 combo boxes for the information to come out.
The problem is i can only retrieve the records from the first selectedindex=0 of the ifelse statement and not from the others.
That means if i choose finance & administration, the next combobox will show controlling and accounting and the last combobox will show the first record of the IDM_IDEA_DATA table. When i changed the sec combobox to procurement & logistics, there are no changes.
The form is at my attachments.
Here are my codes:
Private Sub cboDept_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDept.SelectedIndexChanged
cboSect.Items.Clear()
If Me.cboDept.SelectedIndex = 0 Then
Me.cboSect.Items.Add("CONTROLLING & ACCOUNTING")
Me.cboSect.Items.Add("PROCUREMENT & LOGISTICS")
Me.cboSect.Items.Add("INFORMATION TECHNOLOGY")
cboSect.SelectedIndex = 0
If Me.cboSect.SelectedIndex = 0 Then
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim da1 As OleDbDataAdapter
Try
'cmd = New OleDbCommand("Select Distinct * From IDM_IDEA_DATA Where SECT='CONTROLLING & ACCOUNTING'", conn)
'Dim dr As OleDbDataReader = cmd.ExecuteReader
'dr.Read()
'Me.lblId.Text = dr("ID").ToString
conn = New OleDbConnection(My.Settings.ConnectionString)
da = New OleDbDataAdapter("Select Distinct SECT, TEAMNAME From IDM_IDEA_DATA Where SECT='CONTROLLING & ACCOUNTING'", conn)
da.Fill(ds, "IDM_IDEA_DATA")
Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA")
Me.cboTeamName.DisplayMember = "TEAMNAME"
da1 = New OleDbDataAdapter("Select Distinct * From IDM_IDEA_DATA Where SECT='CONTROLLING & ACCOUNTING'", conn)
da1.Fill(DataSet1, "IDM_IDEA_DATA")
Catch ex As Exception
End Try
ElseIf Me.cboSect.SelectedIndex = 1 Then
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim da1 As OleDbDataAdapter
Dim ds As New DataSet
Try
conn = New OleDbConnection(My.Settings.ConnectionString)
da = New OleDbDataAdapter("Select Distinct SECT, TEAMNAME From IDM_IDEA_DATA Where SECT='PROCUREMENT & LOGISTICS'", conn)
da.Fill(ds, "IDM_IDEA_DATA")
Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA")
Me.cboTeamName.DisplayMember = "TEAMNAME"
da1 = New OleDbDataAdapter("Select Distinct * From IDM_IDEA_DATA Where SECT='PROCUREMENT & LOGISTICS'", conn)
da1.Fill(DataSet1, "IDM_IDEA_DATA")
Catch ex As Exception
End Try
End IfI really need help on this. Hope u guys can help me out. Thx
Last edited by MadAxel; Sep 16th, 2008 at 4:39 am.
You should handle SelectedIndexChanged event for cboDept and cboTeamNames too. If I understand this right, you have an organization hierarchy (dept -> sect -> team)
So, I would use something like this (in pseudo-code):
So, I would use something like this (in pseudo-code):
VB.NET Syntax (Toggle Plain Text)
Private Sub cboDept_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDept.SelectedIndexChanged << User selected Dept, retrieve and fill Sect >> Select Case cboDept.SelectedIndex Case 0 ' Finance & Administration << retrieve and fill Sect with Controlling & Accounting >> ... Case Else ' SelectedIndex = -1, no Dept selected clear/disable cboSect and cboTeamName End Select End Sub Private Sub cboSect_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSect.SelectedIndexChanged << User selected Sect, retrieve and fill TeamName >> Select Case cboSect.SelectedIndex Case 0 ' Controlling & Accounting << retrieve and fill Team with Fico >> ... Case Else ' SelectedIndex = -1, no Sect selected clear/disable cboTeamName End Select End Sub Private Sub cboTeamName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTeamName.SelectedIndexChanged << User selected TeamName, retrieve and show PDesc and IDesc information >> End Sub
Teme64 @ Windows Developer Blog
It seems to me that you do have your team names id DB (field TEAMNAME). So, after user selects Sect, get team names for that Sect from DB and fill combobox. I assume that this is static data and also without knowing how you organized your organization structure to DB.
Suppose you have following (simplified) DB structure:
Departments-table:
DeptID DeptName
1 "dept a"
2 "dept b"
Section-table:
SectID DeptID SectName
1 1 "sect a"
2 1 "sect b"
3 2 "sect c"
Team-table:
TeamID SectID TeamName
1 1 "team a"
2 2 "team b"
3 3 "team c"
4 3 "team d"
First you fill cboDept. When user selects "dept b" (DeptID = 2), you fill cboSect with "sect c", in SQL: "SELECT * FROM Section WHERE DeptID=2". Now when user selects "sect c" (SectID=3), you fill team names with "team c" and "team d", in SQL: "SELECT * FROM Team WHERE SectID=3".
This is just a suggestion, how I would tackle this problem. If you don't have this kind of DB structure it might not work. There's also some "optimization" possibilities, like fetch data only once. And in this scenario you don't hardcode any team names, just map combobox indexes to tables's ID values.
I hope this gives you started.
Suppose you have following (simplified) DB structure:
Departments-table:
DeptID DeptName
1 "dept a"
2 "dept b"
Section-table:
SectID DeptID SectName
1 1 "sect a"
2 1 "sect b"
3 2 "sect c"
Team-table:
TeamID SectID TeamName
1 1 "team a"
2 2 "team b"
3 3 "team c"
4 3 "team d"
First you fill cboDept. When user selects "dept b" (DeptID = 2), you fill cboSect with "sect c", in SQL: "SELECT * FROM Section WHERE DeptID=2". Now when user selects "sect c" (SectID=3), you fill team names with "team c" and "team d", in SQL: "SELECT * FROM Team WHERE SectID=3".
This is just a suggestion, how I would tackle this problem. If you don't have this kind of DB structure it might not work. There's also some "optimization" possibilities, like fetch data only once. And in this scenario you don't hardcode any team names, just map combobox indexes to tables's ID values.
I hope this gives you started.
Teme64 @ Windows Developer Blog
•
•
Join Date: Sep 2008
Posts: 13
Reputation:
Solved Threads: 0
thx..ive solved the problem but now i have a new problem.
AFter doing all that,i need to display information in the textboxes.What i did is i binded the textboxes to the values in the databases.but the thing is,i cannot sort it according to the TEAMNAME.That means everything from the database is displayed.
This is the code that ive used:
i hope u guys can help me..thx alot
AFter doing all that,i need to display information in the textboxes.What i did is i binded the textboxes to the values in the databases.but the thing is,i cannot sort it according to the TEAMNAME.That means everything from the database is displayed.
This is the code that ive used:
Private Sub cboSect_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSect.SelectedIndexChanged
'cboTeamName.Items.Clear()
If cboDept.SelectedIndex = 0 Then
If cboSect.SelectedIndex = 0 Then
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim da1 As OleDbDataAdapter
Dim ds As New DataSet
conn = New OleDbConnection(My.Settings.ConnectionString)
da = New OleDbDataAdapter("Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='CONTROLLING & ACCOUNTING'", conn)
da.Fill(ds, "IDM_IDEA_DATA")
Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA")
Me.cboTeamName.DisplayMember = "TEAMNAME"
da1 = New OleDbDataAdapter("Select Distinct * From IDM_IDEA_DATA Where Sect='CONTROLLING & ACCOUNTING'", conn)
da1.Fill(DataSet1, "IDM_IDEA_DATA")
ElseIf cboSect.SelectedIndex = 1 Then
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim da1 As OleDbDataAdapter
Dim ds As New DataSet
conn = New OleDbConnection(My.Settings.ConnectionString)
da = New OleDbDataAdapter("Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='PROCUREMENT & LOGISTICS'", conn)
da.Fill(ds, "IDM_IDEA_DATA")
Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA")
Me.cboTeamName.DisplayMember = "TEAMNAME"
da1 = New OleDbDataAdapter("Select Distinct * From IDM_IDEA_DATA Where Sect='CONTROLLING & ACCOUNTING'", conn)
da1.Fill(DataSet1, "IDM_IDEA_DATA")
Else
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim da1 As OleDbDataAdapter
Dim ds As New DataSet
conn = New OleDbConnection(My.Settings.ConnectionString)
da = New OleDbDataAdapter("Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='INFORMATION TECHNOLOGY'", conn)
da.Fill(ds, "IDM_IDEA_DATA")
Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA")
Me.cboTeamName.DisplayMember = "TEAMNAME"
da1 = New OleDbDataAdapter("Select Distinct * From IDM_IDEA_DATA Where Sect='CONTROLLING & ACCOUNTING'", conn)
da1.Fill(DataSet1, "IDM_IDEA_DATA")
End If
ElseIf cboDept.SelectedIndex = 1 Then
If cboSect.SelectedIndex = 0 Then
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As New DataSet
conn = New OleDbConnection(My.Settings.ConnectionString)
da = New OleDbDataAdapter("Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='HUMAN RESOURCES'", conn)
da.Fill(ds, "IDM_IDEA_DATA")
Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA")
Me.cboTeamName.DisplayMember = "TEAMNAME"
End If
ElseIf cboDept.SelectedIndex = 2 Then
If cboSect.SelectedIndex = 0 Then
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As New DataSet
conn = New OleDbConnection(My.Settings.ConnectionString)
da = New OleDbDataAdapter("Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='WAFERING & HEAT TREAT'", conn)
da.Fill(ds, "IDM_IDEA_DATA")
Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA")
Me.cboTeamName.DisplayMember = "TEAMNAME"
Else
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As New DataSet
conn = New OleDbConnection(My.Settings.ConnectionString)
da = New OleDbDataAdapter("Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='POLISHING & EPITAXY'", conn)
da.Fill(ds, "IDM_IDEA_DATA")
Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA")
Me.cboTeamName.DisplayMember = "TEAMNAME"
End Ifi hope u guys can help me..thx alot
Ok. Your organization structure seems to be "flat" in DB:
Dept Sect TeamName
FINANCE & ADMINISTRATION CONTROLLING & ACCOUNTING FICO
FINANCE & ADMINISTRATION PROCUREMENT & LOGISTICS FICO
am I right?
The sorting problem. Use "ORDER BY":
this should solve sorting problem. "Order By" syntax is (taken from above sample) "Order By TEAMNAME Asc" (ascending order) or "Order By TEAMNAME Desc" (descending order).
I didn't quite understand You're using "Select Distinct" so atleast there shouldn't be any duplicate entries.
I would also suggest few things to clarify your code:
1) set common Dims to start so that code gets shorter. Atleast following objects could be dimmed at the beginning of the sub:
2) use Select...Case...End Select rather than nested If...ElseIf...Then...End If. You'll get more readable code eg. easier to maintain when you get more Depts, Sects, Teams or something else changes.
Dept Sect TeamName
FINANCE & ADMINISTRATION CONTROLLING & ACCOUNTING FICO
FINANCE & ADMINISTRATION PROCUREMENT & LOGISTICS FICO
am I right?
The sorting problem. Use "ORDER BY":
VB.NET Syntax (Toggle Plain Text)
Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='POLISHING & EPITAXY' Order By TEAMNAME
I didn't quite understand
•
•
•
•
That means everything from the database is displayed.
I would also suggest few things to clarify your code:
1) set common Dims to start so that code gets shorter. Atleast following objects could be dimmed at the beginning of the sub:
VB.NET Syntax (Toggle Plain Text)
Dim conn As OleDbConnection Dim da As OleDbDataAdapter Dim da1 As OleDbDataAdapter Dim ds As New DataSet
2) use Select...Case...End Select rather than nested If...ElseIf...Then...End If. You'll get more readable code eg. easier to maintain when you get more Depts, Sects, Teams or something else changes.
Teme64 @ Windows Developer Blog
•
•
Join Date: Sep 2008
Posts: 13
Reputation:
Solved Threads: 0
What i mean is that every section has different teams. Fico should only belong to 'Controlling & Accounting' and it must not appear at 'Procurement & Logistics'.
So for example,when i select Controlling & Accounting,only teams for this section must appear.But the problem is, when i chose section 'Controlling & Accounting', all teams from the 'Finance & Administration' dept information appeared that means information from 'Controlling & Accounting', 'Procurement & Logistics' & 'Information Technology'.
Can anibody help me with this?Thx alot
So for example,when i select Controlling & Accounting,only teams for this section must appear.But the problem is, when i chose section 'Controlling & Accounting', all teams from the 'Finance & Administration' dept information appeared that means information from 'Controlling & Accounting', 'Procurement & Logistics' & 'Information Technology'.
Can anibody help me with this?Thx alot
Last edited by MadAxel; Sep 18th, 2008 at 10:19 pm.
One more time
I realized that you can't use SelectedIndex directly because two Depts have both SelectedIndex = 0. So use Text property.
Handle Section selection:
Handle TeamName selection:
Try this approach. Now you shouldn get TeamNames only for selected Sect (and Dept). If this doesn't work then I'll be damn...
I realized that you can't use SelectedIndex directly because two Depts have both SelectedIndex = 0. So use Text property.
VB.NET Syntax (Toggle Plain Text)
' User selects a Department, set Sections Private Sub cboDept_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDept.SelectedIndexChanged ' ' Clear Sect info cboSect.Items.Clear ' Which Dept Select Case cboDept.SelectedIndex Case 0 ' Finance & Administration Me.cboSect.Items.Add("CONTROLLING & ACCOUNTING") Me.cboSect.Items.Add("PROCUREMENT & LOGISTICS") Me.cboSect.Items.Add("INFORMATION TECHNOLOGY") ' Show (select 1st) Me.cboSect.SelectedIndex = 0 Case 1 ' Dept next ' Some sections here ' More Cases if needed Case Else ' Dept unknown = error End Select End Sub
Handle Section selection:
VB.NET Syntax (Toggle Plain Text)
' User selected some Section, set TeamNames for selected Section Private Sub cboSect_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSect.SelectedIndexChanged ' Dim conn As OleDbConnection Dim da As OleDbDataAdapter Dim da1 As OleDbDataAdapter Dim ds As New DataSet ' Clear Team info cboTeamName.Items.Clear ' Set conn conn = New OleDbConnection(My.Settings.ConnectionString) ' Which Sect Select Case cboSect.Text Case "CONTROLLING & ACCOUNTING" Try da = New OleDbDataAdapter("Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='CONTROLLING & ACCOUNTING' Order By TEAMNAME", conn) da.Fill(ds, "IDM_IDEA_DATA") Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA") Me.cboTeamName.DisplayMember = "TEAMNAME" da1 = New OleDbDataAdapter("Select Distinct * From IDM_IDEA_DATA Where SECT='CONTROLLING & ACCOUNTING'", conn) da1.Fill(DataSet1, "IDM_IDEA_DATA") Catch ex As Exception End Try ' Show (select 1st) Me.cboTeamName.SelectedIndex = 0 Case "PROCUREMENT & LOGISTICS" Try da = New OleDbDataAdapter("Select Distinct TEAMNAME From IDM_IDEA_DATA Where SECT='PROCUREMENT & LOGISTICS' Order By TEAMNAME", conn) da.Fill(ds, "IDM_IDEA_DATA") Me.cboTeamName.DataSource = ds.Tables("IDM_IDEA_DATA") Me.cboTeamName.DisplayMember = "TEAMNAME" da1 = New OleDbDataAdapter("Select Distinct * From IDM_IDEA_DATA Where SECT='PROCUREMENT & LOGISTICS'", conn) da1.Fill(DataSet1, "IDM_IDEA_DATA") Catch ex As Exception End Try ' Show (select 1st) Me.cboTeamName.SelectedIndex = 0 Case "INFORMATION TECHNOLOGY" ' And similar code here too Case "" ' <- Title of Dept #2, Sect #1 ' And more code here too. NOTICE: Here you are handling Sects for Dept #2 Case "" ' <- Title of Dept #2, Sect #2 ' And code here too ' Add more Cases here, one for each possible Sect name Case Else ' Sect unknown = error End Select End Sub
Handle TeamName selection:
VB.NET Syntax (Toggle Plain Text)
Private Sub cboTeamName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTeamName.SelectedIndexChanged ' Dim conn As OleDbConnection Dim da As OleDbDataAdapter Dim da1 As OleDbDataAdapter Dim ds As New DataSet ' Which TeamName Select Case cboTeamName.Text Case "FIDO" ' Now you have selected Dept #1, Sect #1 and Team FIDO ' And code here Case "" ' <- Title of Dept #1, Sect #1 and Team #2 ' And code here ' Add more Cases here, one for each possible TeamName Case Else ' TeamName unknown = error End Select End Sub
Try this approach. Now you shouldn get TeamNames only for selected Sect (and Dept). If this doesn't work then I'll be damn...
Teme64 @ Windows Developer Blog
In this way, yes. It depends totally, what you do after a TeamName is selected. If the TeamName in itself doesn't matter eg. you use it only to get something from DB, you won't need Select Case... End Select in cboTeamName_SelectedIndexChanged event. What I mean is, if your code is just simple:
then you can just use:
It all depends on what you need to do at this point.
VB.NET Syntax (Toggle Plain Text)
da = New OleDbDataAdapter("Select * From IDM_IDEA_DATA Where TEAMNAME='selected teamname'", conn)
then you can just use:
VB.NET Syntax (Toggle Plain Text)
da = New OleDbDataAdapter("Select * From IDM_IDEA_DATA Where TEAMNAME='" & cboTeamName.Text & "'", conn)
It all depends on what you need to do at this point.
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Setting up a form in PHP (PHP)
- Problem with database connection (JSP)
- uploading Images into Database (ASP.NET)
- PHP Mail with Sender ID (PHP)
- Simple sql questions (broken commands and retrieving columns) (Database Design)
Other Threads in the VB.NET Forum
- Previous Thread: Editing windows registry
- Next Thread: How to store date into the DB using datetimepicker
Views: 1082 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
"crystal .net .net2005 30minutes 2008 access add application array assignment basic binary box button buttons center code connectionstring convert cpu data database databasesearch datagrid datagridview design designer dissertation dissertations dissertationthesis dll dosconsolevb.net editvb.net employees error excel exists firewall folder function image images isnumericfuntioncall listview login math memory mobile module msaccess mssqlbackend mysql navigate net opacity page pan peertopeervideostreaming picturebox plugin port print printing printpreview problem record refresh reports" reuse save savedialog search serial sorting sql sqldatbase storedprocedure string structures studio temp textbox timer upload useraccounts usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet vista visual visualbasic visualbasic.net visualstudio2008 web wpf xml





