Hi landomarossi,
Please refer below sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
<asp:Button ID="btnGet" runat="server" Text="Send" OnClick="OnbtnGet" />
<hr />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:DropDownList ID="ddlCountries" runat="server"></asp:DropDownList>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindDropDownList();
}
}
private void BindDropDownList()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT Country FROM Customers", con))
{
con.Open();
ddlCountries.DataSource = cmd.ExecuteReader();
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
con.Close();
}
}
ddlCountries.Items.Insert(0, new ListItem("---Select Country---", "0"));
}
protected void OnbtnGet(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("Select Name,Country from Customers Where CustomerId=@CustomerId", con))
{
cmd.Parameters.AddWithValue("@CustomerId", txtId.Text);
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
txtName.Text = sdr["Name"].ToString();
if (ddlCountries.Items.FindByValue(sdr["Country"].ToString()) != null)
{
ddlCountries.ClearSelection();
ddlCountries.Items.FindByValue(sdr["Country"].ToString()).Selected = true;
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindDropDownList()
End If
End Sub
Private Sub BindDropDownList()
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT Country FROM Customers", con)
con.Open()
ddlCountries.DataSource = cmd.ExecuteReader()
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
con.Close()
End Using
End Using
ddlCountries.Items.Insert(0, New ListItem("---Select Country---", "0"))
End Sub
Protected Sub OnbtnGet(ByVal sender As Object, ByVal e As EventArgs)
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("Select Name,Country from Customers Where CustomerId=@CustomerId", con)
cmd.Parameters.AddWithValue("@CustomerId", txtId.Text)
Try
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
txtName.Text = sdr("Name").ToString()
If ddlCountries.Items.FindByValue(sdr("Country").ToString()) IsNot Nothing Then
ddlCountries.ClearSelection()
ddlCountries.Items.FindByValue(sdr("Country").ToString()).Selected = True
End If
End If
Catch ex As Exception
Throw ex
Finally
con.Close()
End Try
End Using
End Using
End Sub
Screenshot