1,105,331 Community Members

How to Retrive data to textboxes on selected indexchenge event of Dropdownl

Aman.jen
Newbie Poster
5 posts since Jul 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

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.

Aman.jen
Newbie Poster
5 posts since Jul 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

Try something like this:

While dr.Read()
    TB1.AppendText = dr("P_id").ToString + vbNewLine
    TB2.AppendText = dr("P_Name").ToString + vbNewLine
End While
Aman.jen
Newbie Poster
5 posts since Jul 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

Not working

Aman.jen
Newbie Poster
5 posts since Jul 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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

Member Avatar
artemix22
Junior Poster
111 posts since Jan 2012
Reputation Points: 4 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
artemix22
Junior Poster
111 posts since Jan 2012
Reputation Points: 4 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

zzzzz sorry, forget to add double quote on

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

my bad

Aman.jen
Newbie Poster
5 posts since Jul 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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
Member Avatar
artemix22
Junior Poster
111 posts since Jan 2012
Reputation Points: 4 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

it's work fine with me here :)

check your db, is "P_Name" empty?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article