Asp.Net: Bind or populate dropdownList with data from database using c# & vb.net.

Aman Sharma
0
Introduction:  

Here, I will explain how to populate or bind dropdown list control from database in asp.net using c# and vb.net.  This article will be useful for beginners.

Demo:

Steps to follow:

Create table in database:
Create Table “Student_Info”  in Database “Blog” and Enter some data to display.

Column Name
DataType
Student_Id
Int
Student_name
Varchar(100)
Age
Int
Class
Varchar(100)

Create connection string in webconfig file:

  <connectionStrings>
<add name="con" connectionString="Data Source=localhost; initial catalog=Blog; integrated Security=true"></add>   

  </connectionStrings>

 Design Asp.Net webForm:


<fieldset style="width:350px;"><legend>Bind DropDownList In ASP.NET</legend>

      <table >
          <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
          </tr>
          <tr>
              <td>Select Student:</td>
              <td> <asp:DropDownList ID="ddlStudent" runat="server" Width="220px"></asp:DropDownList>
              </td>
              <td>&nbsp;</td>
          </tr>
          <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
          </tr>
          <tr>
              <td>&nbsp;</td>
              <td><asp:Button ID="btnSubmit" runat="server" Text="Show Selected Student" OnClick="btnSubmit_Click" />
              </td>
              <td>&nbsp;</td>
          </tr>
          <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
          </tr>
          <tr>
              <td>Selected Student: </td>
              <td> <asp:Label ID="lblStudent" runat="server" Text=""></asp:Label>
              </td>
              <td>&nbsp;</td>
          </tr>
      </table>

  </fieldset>


Asp.Net Code:

Using C#:

 //add following napespaces

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

 //write following code

public partial class DropDownList : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
        Bind_Dropdown();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (ddlStudent.SelectedIndex > 0)
        {
            lblStudent.Text = ddlStudent.SelectedItem.Text;
        }       
    }

   //bind dropdown
    public void Bind_Dropdown()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlCommand cmd = new SqlCommand("select * from student_info", con);      
        SqlDataAdapter adpData = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adpData.Fill(dt);
        ddlStudent.DataSource = dt;
        ddlStudent.DataTextField = "Student_name";
        ddlStudent.DataValueField = "Student_id";
        ddlStudent.DataBind();
        ddlStudent.Items.Insert(0,"Select Student");
        con.Close();
    }
}


Using VB.Net:

'Import Following namespaces

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

'Write following Code

Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Bind_Dropdown()
End If
End Sub

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If ddlStudent.SelectedIndex > 0 Then
lblStudent.Text = ddlStudent.SelectedItem.Text
End If
End Sub

Public Sub Bind_Dropdown()
If con.State = ConnectionState.Closed Then
con.Open()
End If

Dim cmd As New SqlCommand("select * from student_info", con)
Dim adpData As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
adpData.Fill(dt)
ddlStudent.DataSource = dt
ddlStudent.DataTextField = "Student_name"
ddlStudent.DataValueField = "Student_id"
ddlStudent.DataBind()
ddlStudent.Items.Insert(0, "Select Student")
con.Close()
End Sub

End Class


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !