Auto Generate in DropDownList
auto generate in drop down list example E0001 etc..,
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 18, 2012, 9:34 PM EST
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Dynamic_increment_using_Dropdown._Default" %>
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 Dynamic_increment_using_Dropdown
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add("Select");
SqlConnection con = new SqlConnection(strConnString);
str = "select count(*) from employee";
com = new SqlCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
DropDownList1.Items.Add("E000" + count);
con.Close();
}
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "insert into employee values('" + DropDownList1.SelectedItem.Text.Trim() + "','" + txt_empname.Text.Trim() + "'," + txt_sal.Text.Trim() + ")";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Label4.Text = "Records successfully Inserted";
}
}
}
Thanks
If this post helps you mark it as answer
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 24, 2012, 5:32 PM EST
Thanks
SenthilkumarRank #387 · 6.5K reputationPosted Feb 28, 2012, 11:13 PM EST
If you are not getting the value from the sql server then you need to do it in C#.
Sql can have the auto generate number from identity column or ROW_NUMBER() in query.
In C#, if you use the datatable then you can set the datacolumn field as identity column.
or you can iterate the items that you want to add into dropdown list and the looping number became auto generate number.