Hi micah,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:DropDownList ID="ddlNames" runat="server" OnSelectedIndexChanged="ddlNames_SelectedIndexChanged"
AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Value="Maria">Maria</asp:ListItem>
<asp:ListItem Value="Ana Trujillo">Ana Trujillo</asp:ListItem>
<asp:ListItem Value="Antonio Moreno">Antonio Moreno</asp:ListItem>
<asp:ListItem Value="Thomas Hardy">Thomas Hardy</asp:ListItem>
</asp:DropDownList>
<br />
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void ddlNames_SelectedIndexChanged(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand command = new SqlCommand("SELECT CustomerID,City,Country FROM Customers WHERE ContactName = @Name", con);
command.Parameters.AddWithValue("@Name", ddlNames.SelectedItem.Text);
con.Open();
SqlDataReader sdr = command.ExecuteReader();
if (sdr.Read())
{
txtId.Text = sdr["CustomerID"].ToString();
txtCity.Text = sdr["City"].ToString();
txtCountry.Text = sdr["Country"].ToString();
}
sdr.Close();
}
}
VB.Net
Protected Sub ddlNames_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Dim command As SqlCommand = New SqlCommand("SELECT CustomerID,City,Country FROM Customers WHERE ContactName = @Name", con)
command.Parameters.AddWithValue("@Name", ddlNames.SelectedItem.Text)
con.Open()
Dim sdr As SqlDataReader = command.ExecuteReader()
If sdr.Read() Then
txtId.Text = sdr("CustomerID").ToString()
txtCity.Text = sdr("City").ToString()
txtCountry.Text = sdr("Country").ToString()
End If
sdr.Close()
End Using
End Sub
Screenshot
