Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 2nd, 2004, 05:20 AM
Authorized User
 
Join Date: Jul 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with listbox_selectedindexchanged

Hi,
  I am using a listbox and a textbox in my form.I displayed one particular column value in the listbox.Now i want that when selecting a value from the listbox, my textbox should display another field from the same table depending on the selected item.For this i wrote the following lines of code in Listbox_SelectedIndexChanged event.But it is not working.I dont know why. Please help me.

Dim conn As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrProducts As SqlDataReader
        Dim ds As DataSet

        conn = New SqlConnection("Server=cabin61;UID=sa;PWD=;Database =pubs")
        conn.Open()
        cmdSelect = New SqlCommand("Select au_lname From authors where au_id='" & ListBox1.SelectedItem.Value & "'", conn)
        Dim topic As New SqlDataAdapter(cmdSelect)
        topic.Fill(ds)
        TextBox1.Text = ds.Tables(0).Rows(0).Item("au_lname")
        conn.Close()

 
Old September 2nd, 2004, 07:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I suggest u to bind ur ListBox & textBox! then when u change ur Listbox item the textBox Text will be changed too!
much easy & ...

HTH.

Always:),
Hovik Melkomian.
 
Old September 3rd, 2004, 02:10 AM
Authorized User
 
Join Date: Jul 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I could not get you properly. Can you tell me where i should bind the textbox ? It will be helpful for me if you give me the steps.Thanks

 
Old September 4th, 2004, 12:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Ok, sorry if I were not clear to help u.
See these links, Im sure they will be much helpful than mine;) (First one is much better)

http://msdn.microsoft.com/library/de...classtopic.asp

http://msdn.microsoft.com/library/de...boxcontrol.asp

http://msdn.microsoft.com/library/de...boxcontrol.asp

http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Always:),
Hovik Melkomian.
 
Old September 6th, 2004, 01:32 AM
Authorized User
 
Join Date: Jul 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the links you sent. Actually, i found that something in my form is missing, thats why my application is not giving me the result i desire. Let me explain my application. I have a listbox control whose items are bound to a datatable. I have also a textbox in my form, which is initially blank.Then i select an item from the listbox, and then i want to display the selected item in the textbox.So, i wrote the following code.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack Then
            Dim conn As SqlConnection
            Dim cmdSelect As SqlCommand
            Dim dtr As SqlDataReader
            Dim ds As DataSet

            conn = New SqlConnection("Server=cabin61;UID=sa;PWD=;Database =ab")
            conn.Open()

            cmdSelect = New SqlCommand("Select rollno From chkbtry", conn)
            dtr = cmdSelect.ExecuteReader()
            ListBox1.DataSource = dtr
            ListBox1.DataTextField = "rollno"
            ListBox1.DataBind()
            dtr.Close()
            conn.Close()

        End If

    End Sub


    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        TextBox1.Text = ListBox1.SelectedItem.Value
End Sub

<asp:listbox id="ListBox1" style="Z-INDEX: 102; LEFT: 345px; POSITION: absolute; TOP: 101px" runat="server" Width="158px" Height="35px" AutoPostBack="True"></asp:listbox>
<asp:textbox id="TextBox1" style="Z-INDEX: 103; LEFT: 347px; POSITION: absolute; TOP: 167px" runat="server"></asp:textbox>


But it is not working.Is there anything mising in the code ? Should some properties of the listbox have to be changed ? Please advice











Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.