title: How to bind selected value from database in dropdownlist in asp.net
body: <p>I am trying to bind a selected item from the database in dropdownlist. I am not getting the users selected data in the dropdownlist instead it loads everything. What i need is to have a default selected value from the database along with other items. Please help me to overcome this problem. Thanking you in advance.</p>
  
  <p>Stored procedure:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[get_student_details]
    @StudentId int = 0
  AS
  BEGIN
      SET NOCOUNT ON;
  
      SELECT      
          dbo.Student.InstituteId, 
          dbo.Student.Institute,
          dbo.Student.Name, 
          dbo.Student.Gender,
          dbo.Student.Age
      FROM         
          dbo.Student 
      WHERE       
          dbo.Student.StudentId = @StudentId
  END             
  </code></pre>
  
  <p>My .aspx markup:</p>
  
  <pre><code>&lt;asp:DropDownList ID="ddlInstitute" runat="server"&gt;&lt;/asp:DropDownList&gt;
  &lt;asp:TextBox ID="txtName" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;asp:TextBox ID="txtGender" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;asp:TextBox ID="txtAge" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;asp:Button ID="btnPersonalDetails" runat="server"  Text="Search" OnClick="GetStudentDetails"/&gt;
  </code></pre>
  
  <p>My code behind:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          FillInstitute();
      }                   
  }
  
  public void FillInstitute()
  {
      SqlConnection con = new SqlConnection(constr);
      SqlCommand cmd = new SqlCommand();
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.CommandText = "get_Institute";
      cmd.Connection = con;
  
      try
      {
          con.Open();
          ddlInstitute.DataSource = cmd.ExecuteReader();
          ddlInstitute.DataTextField = "Institute";
          ddlInstitute.DataValueField = "InstituteId";
          ddlInstitute.DataBind();
          ddlInstitute.Items.Insert(0, new ListItem("--Select--", "0"));
      }
      catch (Exception ex)
      {
          throw ex;
      }
      finally
      {
          con.Close();
          con.Dispose();
      }
  }
  
  public void GetStudentDetails()
  {
      studentid= 123;
      SqlConnection con = new SqlConnection(constr);
      SqlCommand cmd = new SqlCommand();
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.CommandText = "get_student_details";
      cmd.Parameters.Add("@StudentId", SqlDbType.Int).Value = studentid;
      cmd.Connection = con;
  
      try
      {
          con.Open();
          SqlDataReader dr = cmd.ExecuteReader();
  
          if (dr.Read())
          {
              ddlInstitute.DataValueField= dr["InstituteId"].ToString();
              ddlInstitute.DataTextField= dr["Institute"].ToString();
              txtName.Text = dr["Name"].ToString();
              txtGender.Text = dr["Gender"].ToString();
              txtAge.Text = dr["Age"].ToString();
          }
      }
      catch (Exception ex)
      {
          throw ex;
      }
      finally
      {
          con.Close();
          con.Dispose();
      }
  }
  </code></pre>