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*Emphasized Text Here*

thnx

I think your problem might be here:

If dr.HasRows Then
    dr.Read()
    TB1.Text = (dr.Item("P_id"))
    TB2.Text = (dr.Item("P_Name"))
End If 

This only reads one row. You'll probably need For Each or a While loop and use the .AppendText method of the TextBox's.

Hi,
If I use while or do while loop...it shows just last value at any selection

   If dr.HasRows Then
            While dr.Read()
                TB1.Text = dr("P_id").ToString
                TB2.Text = dr("P_Name").ToString
            End While

Plz help thnx

Try something like this:

While dr.Read()
    TB1.AppendText = dr("P_id").ToString + vbNewLine
    TB2.AppendText = dr("P_Name").ToString + vbNewLine
End While

Not working

Hi ,

Ihave modified the code as below...but now it is saying : There is no row at position 0.

Dim sqlcmd As String = ("select * From DP_Table Where P_id = '" & DropDownList1.SelectedItem.Value & "'")
Dim ad As New SqlDataAdapter(sqlcmd, cn)

Dim ds As New DataSet
ad.Fill(ds, "DP_Table")

TB1.Text = ds.Tables("DP_Table").Rows(0)("P_id").ToString
TB2.Text = ds.Tables("DP_Table").Rows(0)("P_Name").ToString

the "P_id" is already binded on your DropDownList, so i think, you no need to read it again.
use scalar, because you just need to pass the "P_Name"

try this :

TB1.Text = DropDownList1.SelectedItem.Value

Dim sqlcmd As New SqlCommand ("select P_Name from DP_Table where P_id = '" & DropDownList1.SelectedItem.Value & "', cn)
TB2.Text = sqlcmd.ExecuteScalar
sqlcmd.Dispose()

let me know if there is an error

btw, it's .NET sub forum not ASP

zzzzz sorry, forget to add double quote on

.....SelectedItem.Value & "'", cn)

my bad

Not working...Now just showing 0 in second textbox...

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")
    DropDownList1.DataValueField = ("P_id")
    DropDownList1.DataBind()
     DropDownList1.Items.Insert(0, New ListItem("select Item", "0"))
    DropDownList1.SelectedIndex = 0

End Sub

Public Sub txtDp()
    cn.Open()
     TB1.Text = DropDownList1.SelectedItem.Value
    Dim sqlcmd As New SqlCommand("select P_Name from DP_Table where P_id = '" & DropDownList1.SelectedItem.Value & "'", cn)
    TB2.Text = sqlcmd.ExecuteScalar
    sqlcmd.Dispose()
    cn.Close()
End Sub

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

    txtDp()


End Sub

it's work fine with me here :)

check your db, is "P_Name" empty?

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.