Click here to Skip to main content
15,898,987 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi plz help me to correct this code....

This code is showing just first result in texboxes...how to get rest plz help...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cn.Open()
bindDp()
cn.Close()
End Sub
Public Sub bindDp()

cmd.Connection = cn
cmd.CommandText = "Select * From DP_Table"
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
DropDownList1.DataSource = dt
DropDownList1.DataTextField = ("P_id").ToString()
DropDownList1.DataValueField = ("P_id").ToString()
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, "Select Name")
End Sub

Public Sub txtDp()
Dim stateTable As New DataTable
stateTable.Clear()
Dim sqlcmd As New SqlCommand("select P_id,P_Name From DP_Table Where P_id = '" & DropDownList1.SelectedItem.Value & "'", cn)
dr = cmd.ExecuteReader()
If dr.HasRows Then
dr.Read()
TB1.Text = (dr.Item("P_id"))
TB2.Text = (dr.Item("P_Name"))
End If

End Sub
Posted
Comments
[no name] 6-Jul-13 12:56pm    
Because you are only reading from your data reader once?
Aman.Jen 6-Jul-13 13:19pm    
plz eleborate...

thnx
Aman.Jen 7-Jul-13 5:54am    
Hi,
I am using while loop in here..but it still showing just last record..

If dr.HasRows Then

While dr.Read()

TB1.Text = dr.Item("P_id").ToString
TB2.Text = dr.Item("P_Name").ToString
End While
End If

Plz suggest a solution

Thnx

1 solution

Not sure what you're trying to achieve. If your query is returning multiple records, is a textbox really the appropriate output control? You've got 2 textboxes, and you're writing two fields from a single record in those fields. After the fix based on ThePhantomUpvoter's suggestion, you're now looping through all your records, but you're still only entering the values for the current record in the textboxes each time through your loop. Perhaps you want this?:
VB
While dr.Read()
                TB1.Text &= dr.Item("P_id").ToString
                TB2.Text &= dr.Item("P_Name").ToString
End While
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900