ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Insert DropDownList Value with AutoIncrement ID using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">CREATE TABLE TableTest(ID INT IDENTITY, Name VARCHAR(20))</pre> <p>&nbsp;</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DropDownList ID=&#34;ddCustomerName&#34; runat=&#34;server&#34; OnSelectedIndexChanged=&#34;GetData&#34; AutoPostBack=&#34;true&#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:GridView runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; ID=&#34;GridView1&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;ID&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; if (!this.IsPostBack) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string query = "SELECT Name FROM Customers"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = GetData(query); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.DataSource = dt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.DataTextField = "Name"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.DataValueField = "Name"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.DataBind(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.Items.Insert(0, new ListItem("Select CustomerId", "0")); &nbsp;&nbsp;&nbsp; } } protected void GetData(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; &nbsp;&nbsp;&nbsp; using (SqlConnection con = new SqlConnection(constr)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlCommand cmd = new SqlCommand("INSERT INTO TableTest(Name) VALUES(@Name)", con)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue("@Name", ddCustomerName.SelectedItem.Value); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Close(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string query = "SELECT * FROM TableTest"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridView1.DataSource = GetData(query); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.GridView1.DataBind(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } } public DataTable GetData(string query) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; &nbsp;&nbsp;&nbsp; using (SqlConnection con = new SqlConnection(constr)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlCommand cmd = new SqlCommand(query, con)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlDataAdapter da = new SqlDataAdapter(cmd)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(dt); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return dt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load &nbsp;&nbsp;&nbsp; If Not Me.IsPostBack Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim query As String = "SELECT Name FROM Customers" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim dt As DataTable = GetData(query) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.DataSource = dt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.DataTextField = "Name" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.DataValueField = "Name" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.DataBind() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddCustomerName.Items.Insert(0, New ListItem("Select CustomerId", "0")) &nbsp;&nbsp;&nbsp; End If End Sub Protected Sub GetData(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp; Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString &nbsp;&nbsp;&nbsp; Using con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using cmd As SqlCommand = New SqlCommand("INSERT INTO TableTest(Name) VALUES(@Name)", con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue("@Name", ddCustomerName.SelectedItem.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Open() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Close() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim query As String = "SELECT * FROM TableTest" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridView1.DataSource = GetData(query) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Me.GridView1.DataBind() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp; End Using End Sub Public Function GetData(ByVal query As String) As DataTable &nbsp;&nbsp;&nbsp; Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString &nbsp;&nbsp;&nbsp; Using con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using cmd As SqlCommand = New SqlCommand(query, con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using da As SqlDataAdapter = New SqlDataAdapter(cmd) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(dt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return dt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Using &nbsp;&nbsp;&nbsp; End Using End Function</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/UElp9qs.gif" alt="" width="249" height="283" /></p>https://www.aspforums.net:443/Threads/372303/Insert-DropDownList-Value-with-AutoIncrement-ID-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/372303/Insert-DropDownList-Value-with-AutoIncrement-ID-using-C-and-VBNet-in-ASPNet/Fri, 08 Feb 2019 03:05:51 GMT