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

Recommended Answers

All 2 Replies

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:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
  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
End If
'Dp2.Visible = False
End Sub

Google for "ASP.NET page life cycle" lots of handy stuff.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.