Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form with multiple fields:
Name, account number, date, mileage, visits

When a user selects their name from the dropdown list "Name" I want to get their account number to populate automatically in the textbox "Account Number" from the Employee sqldatabase before they submit the balance of the information.

How would I do this?

I tried this javascript but I can't get it right. Can someone help me fix this script please?
JavaScript
function EnterNum() {
            var DropdownListName = document.getElementById('<%= DropDownListName.ClientID%>').Value;
            var TextBoxVendorNum = document.getElementById('<%=TextBoxVendorNum.ClientID%>').Value;
            if ((DropdownListName !== "") && (TextboxVendorNum !== "")) {
                var newValue = ("Select APVendorNumber FROM Employee WHERE ('DropDownList.SelectedValue')");
                document.getElementById('<%=TextBoxVendorNum.ClientID%>').Value = getSelection(newValue);
            }
            
            }


What I have tried:

VB
Protected Sub DropDownListName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownListName.SelectedIndexChanged
        If Me.DropDownListName.SelectedItem.Value <> "" Then
            Dim constr As String = ConfigurationManager.ConnectionStrings("Data Source=<server>; Initial Catalog=<database>;User ID=<user>;Password=<password>;").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT APVendorNumber FROM Employee WHERE EmployeeName = @EmployeeName", con)
                    Using da As New SqlDataAdapter(cmd)
                        cmd.Parameters.AddWithValue("@EmployeeName", Me.DropDownListName.SelectedItem.Value)
                        con.Open()
                        Dim VendorNumber As Object = cmd.ExecuteScalar()
                        con.Close()
                        Me.TextBoxVendorNum.Text = VendorNumber.ToString()
                    End Using
                End Using
            End Using
        Else
            Me.TextBoxVendorNum.Text = "Please Select Name from List"
        End If
    End Sub
End Class
Posted
Updated 12-Feb-16 9:59am
v4

Use the blur event to capture when the user tabs out of the name box

.blur() | jQuery API Documentation[^]

Or you could use the keyUp event. When the blur\keyup event fires get the value of what's in the name textbox using val

.val() | jQuery API Documentation[^]

Now call a webservice\url on your site that accepts the name as a param and returns the account number if one is found.

jQuery.ajax() | jQuery API Documentation[^]
 
Share this answer
 
Found this and it worked perfectly. Doubled click on my dropdownlist and added this to my code behind.

C#
' build the query with the product id as paramter
myCmd.CommandText = "SELECT product_name, product_title, product_desc, product_author FROM Product WHERE product_id = @product_id"
' add the parameter so the SqlCommand can build the final query
myCmd.Parameters.Add(New SqlParameter("@product_id", (DropDownList2.Text)))

' run the query and obtain a reader to get the results
Dim reader As SqlDataReader = myCmd.ExecuteReader()

' check if there are results
If (reader.Read()) Then
     ' populate the values of the controls
     txtProductName2.Text = reader(0)
     txtProductTitle2.Text = reader(1)
     txtProductDescription2.Text = reader(2)
     txtProductAuthor2.Text = reader(3)
End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900