In The DropDownList..If I have Five Names..when I click each name its index is 0

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 13
Reputation: B.H is an unknown quantity at this point 
Solved Threads: 0
B.H B.H is offline Offline
Newbie Poster

In The DropDownList..If I have Five Names..when I click each name its index is 0

 
0
  #1
Aug 22nd, 2006
My doubt is

Am using Microsoft SQLSERVER as the back end with Visual Web Developer Express Edition as the front end.

In one of the Forms,I do have the DropDownList which contains the names of the Employees.
When I click each name from the dropdownlist..the corresponding datas of that Employee should appear in my form
eg: when I click Tom Alex,his address,job,city etc should appear on the form.
For this i gave the code in the SelectedIndexedchanged. and in the property window I set the AutoPostBack=True

My problem is that...When I click any names from the DropDownList..its index value is 0
ie suppose I have a list of names in the dropdownlist like Arun,krishna,Shabin,Binnesh.
so when I click on Arun..its index value is 0..ie is ok.
but when i click on Krishna...its index value should be 1....but here its index value is 0
likewise when i click Shabin or binesh...its index vallue is 0.
since its index value is 0 always...when i click kris,shabin,binnesh..always pops up mine...

So anyone..can u help me to let me know where am wrong...I recently studied ASP.NET...but am not too deep.
I know this problem is very easy ...i will attach the code below.

it would be great if u could help me out.

Code to Connect to SQL SERVER
---------------------------
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
With cnn
cnn.Open("DSN=Telephone;uid=sa;pswd=sa;")
End With
sql = "SELECT * FROM Emps ORDER BY Emp_Name"
rs = cnn.Execute(sql)
Txt_EmpName.Focus()
Dp3.Items.Clear()
If rs.EOF Then
MsgBox("Database is Empty", , "Telephone")
Else
Call loadme()
End If
'Dp2.Visible = False
End Sub
Private Sub loadme()
Dim cnn As New ADODB.Connection
Dim rsLoadme As New ADODB.Recordset
cnn.Open("DSN=Telephone;uid=sa;pswd=sa;")
rsLoadme.Open("Select Emp_Id,Emp_Name + ' ' +Emp_Surname as Name FROM Emps", cnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
Do While Not rsLoadme.EOF
Dp3.Items.Add(rsLoadme.Fields("Name").Value)
'rsLoadme.MoveNext()
Loop
End Sub

My Code in The DropDownList
---------------------------
Private Sub Dp3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Dp3.SelectedIndexChanged
Dim cnn As New ADODB.Connection
Dim rsSelect As New ADODB.Recordset
With cnn
cnn.Open("DSN=Telephone;uid=sa;pswd=sa;")
End With
'value = instance.SelectedItem
'instance.SelectedIndex = value
If Dp3.SelectedIndex > -1 Then
sqlSelect = "SELECT Title,Address1,Address2,Address3,City FROM Emps WHERE Emp_NS='" & Trim(Dp3.Text) & "'"
rsSelect = cnn.Execute(sqlSelect)
Dp1.Text = rsSelect.Fields("Title").Value
Txt_Address1.Text = rsSelect.Fields("Address1").Value
Txt_Address2.Text = rsSelect.Fields("Address2").Value
Txt_Address3.Text = rsSelect.Fields("Address3").Value
Txt_City.Text = rsSelect.Fields("City").Value
Dp3.Dispose()
End If

Note:
I have done all the coding in the Code Behind and not on the HTML PAge with the ASP syntax.

Am new to ASP.NET......any help would be greatly appreciatable.

Thanks so much
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: In The DropDownList..If I have Five Names..when I click each name its index is 0

 
0
  #2
Aug 22nd, 2006
This gets em every time he he!

Page_Load fires every request aand it fires B4 selected index changed, so you are RELOADING your drop down list B4 then retreiving the employee information at which point the selected index is back to the default "0".

You need to use the Page.IsPostBack value and only bind your drop down list if it returns false eg:

  1. Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2. If Not Page.IsPostBack Then
  3. With cnn
  4. cnn.Open("DSN=Telephone;uid=sa;pswd=sa;")
  5. End With
  6. sql = "SELECT * FROM Emps ORDER BY Emp_Name"
  7. rs = cnn.Execute(sql)
  8. Txt_EmpName.Focus()
  9. Dp3.Items.Clear()
  10. If rs.EOF Then
  11. MsgBox("Database is Empty", , "Telephone")
  12. Else
  13. Call loadme()
  14. End If
  15. End If
  16. 'Dp2.Visible = False
  17. End Sub


Google for "ASP.NET page life cycle" lots of handy stuff.
Last edited by hollystyles; Aug 22nd, 2006 at 11:25 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: B.H is an unknown quantity at this point 
Solved Threads: 0
B.H B.H is offline Offline
Newbie Poster

Re: In The DropDownList..If I have Five Names..when I click each name its index is 0

 
0
  #3
Aug 23rd, 2006
Thanks So Much For ur bit piece of code and it really worked...I really appreciate ur effort in finding time to have a glimpse over my code. Am new to this ASP.NET.

Thanks bro once again and keep well

cheers
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC