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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted Feb 29, 2012, 4:13 AM
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.
Satyapriya NayakPosted Feb 24, 2012, 10:32 PM
Thanks
Satyapriya NayakPosted Feb 19, 2012, 2:34 AM
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