Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fill textboxes from drop down list

Status
Not open for further replies.

CGreenMTU

Programmer
May 27, 2004
61
US
I need help. I'm trying to figure out how to select an item from a drop-down list and fill 3 textboxes relating to the value in the drop-down list. All fields are coming from the same database table. The drop-down list has a list of company names. Once a company name is selected, I want to populate three textboxes: Insurance Company, Policy ID, & Policy Number. Can anyone give me some sample code or head me in the right direction? thanks....
 
find the selected item in the dropdownlist... dl.selecteditem.value

use this to determine what to place in each of the other textboxes...
if you want to post what you have we can help you from there, so that we can better understand your approach. Do you intend to make a db call to obtain the other information after you have determined what is selected etc...
 
here is what i've come up with so far from looking at a few tutorials online. I used a seperate SUB to fill my drop down list. This is how i'm trying to fill textboxes from a selection from the drop down list and it does not work. When I run the code I get this error:

System.IndexOutOfRangeException: Index 0 is not non-negative and below total rows count.

HERE IS MY CODE:

Code:
Private Sub ddlInsured_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlInsured.SelectedIndexChanged
        Dim strInsuranceCompany, strID, strPolicyNumber As String

        Dim populatequery As String = "SELECT InsuranceCompany, ID, [Policy Number] FROM TblPolicyTable WHERE ID =" & ddlInsured.SelectedItem.Value

        Dim objCommand As New OleDb.OleDbCommand(populatequery, cnNewClaim)
        Dim objReader As OleDb.OleDbDataReader

        cnNewClaim.Open()
        objReader = objCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        While objReader.Read()
            strInsuranceCompany = objReader("InsuranceCompany")
            strID = objReader("ID")
            strPolicyNumber = objReader("Policy Number")
        End While

        txtInsCo.DataBind()
        txtPolicyID.DataBind()
        txtPolicyNumber.DataBind()

    End Sub
 
What are you doing with the string values?

Also what line of code is giving you the error? My guess is this is running when nothing is selected on Page Load. I would put code at the beginning to check and see if the selected value has been set.



Hope everyone is having a great day!

Thanks - Jennifer
 
i bet you didnt set the dropdownlist text and value items...

<asp:DropDownList datatextfield="companyname" datavaluefield="policyid" id="ddlCompany" runat="server" />

from your sql to fill the DDL,
SELECT CompanyName, PolicyID FROM myTable
...ddlCompany.DataBind...

you can have the same field in Text and Value,

or as easy as stating...

"SELECT InsuranceCompany, ID, [Policy Number] FROM TblPolicyTable WHERE ID =" & ddlInsured.SelectedItem.Text

but make sure your querying off of a text field.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top