Forum guidelines
AuthorQuestion
DropDownList with TextBox
Posted By Davin Martyn on 07 Oct 2012
Hello sir

I am working in asp.net and use dropdownlist with textbox. I want to insert textbox value in dropdownlist with database also . please define a some code for this problem.

AuthorReply
Re: DropDownList with TextBox
Posted By Satyapriya Nayak on 07 Oct 2012  
Hi Davin,


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DropDownList_with_TextBox._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"
            Text="Insert" />
   
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    </form>
</body>
</html>











using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace DropDownList_with_TextBox
{
    public partial class _Default : System.Web.UI.Page
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlCommand com;
        string str;
    
      

         void showdrop()
        {
          
            SqlConnection con = new SqlConnection(connStr);
            if (!IsPostBack)
            {
              DropDownList1.Items.Add("Choose Name");
                con.Open();
                str = "select * from  employee";
                com = new SqlCommand(str, con);

                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    DropDownList1.Items.Add(reader["ename"].ToString());
                }
                reader.Close();
                con.Close();
            }
        }

         protected void btn_insert_Click(object sender, EventArgs e)
         {
             SqlConnection con = new SqlConnection(connStr);
             con.Open();
             com = new SqlCommand("insert into employee values(@ename)", con);
             com.Parameters.AddWithValue("@ename", TextBox1.Text);
             com.ExecuteNonQuery();
             con.Close();  
         }

         protected void Page_Load(object sender, EventArgs e)
         {
             showdrop();
         }
    }
}



Thanks


If this post helps you mark it as answer
Thanks

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.