Hi there, Does anyone know how to retrieve and display data from database using combo list? What I mean is this..in a form there are a combo list and two textfields..the user need to choose their company using that combo list. Once they have chose their company, the address and the contact number of the company will be displayed in the next two textfields called CAddress and CContact. For example, if the user chose company ABC, how can I display the company ABC address and contact number in those textfields? Need help. Codes examples are much appreciated. Thank you.
Hi this would be something to do with ASP and JS. Populate the combo item 'value' with the results that you want to show in the text boxes. You can seperate them with any character. Then write a JS to update the text value of the text boxes using the 'value' of hte selected combo item. Hope it helps.. dont have much time to code it for you.. really sorry.
Make the combo box autopostback when a selection is made a postback will be done and you can put in the section for the post back the code to retreive the information from database. Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged Dim myConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString Dim sqlString As String = Nothing Dim myConnection As New MySqlConnection(myConnectionString) Dim myAdapter As MySqlDataAdapter = New MySqlDataAdapter(sqlString, myConnection) Dim DS As DataSet = New DataSet myAdapter.SelectCommand.CommandText = "select address,phonenumber from companies where companyid=" & DropDownList1.SelectedValue myAdapter.Fill(DS) Dim er As String If DS.Tables(0).Rows.Count > 0 Then Me.TextBox1.Text = DS.Tables(0).Rows(0).Item("address").ToString Me.TextBox2.Text = DS.Tables(0).Rows(0).Item("phonenumber").ToString End If myAdapter.Dispose() myConnection.Dispose() DS.Dispose() End Sub This will do the job. If you want you can add an updatepanel so that you dont get a complete refresh of the webpage when you do a post back.
hope it will help you Step 1:Make Autopostback property of combobox to true..by default it is false Step 2:Give connection string Step 3:write sqlquery select address,phonenumber from comp where cmpid='"+DropDownList1.SelectedValue+"' Step 4:Bind the data in textbox