Hello dear i am in problems that i need to add type name from database table to dropdownlist
in asp.net c# by coding. how can i solve this problems.
Type
Type name Type name
Sub type name
when i add type after refreshing the page automatically added in dropdown list.
how can i solve this problems.
Loading

Satyapriya NayakPosted Mar 3, 2013, 3:26 AM
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_Insertshow
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dropbind();
}
}
protected void btn_add_Click(object sender, EventArgs e)
{
DropDownList1.Items.Clear();
SqlConnection con = new SqlConnection(strConnString);
com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "insert into employee(name)values(@name)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@name", txt_name.Text);
if (con.State == ConnectionState.Closed)
con.Open();
com.ExecuteNonQuery();
con.Close();
Response.Write("Records successfully inserted");
clear();
dropbind();
}
private void clear()
{
txt_name.Text = "";
}
private void dropbind()
{
SqlConnection con = new SqlConnection(strConnString);
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["name"].ToString());
}
reader.Close();
con.Close();
}
}
}