Insert DropDownList Value with AutoIncrement ID using C# and VB.Net in ASP.Net

Last Reply 8 months ago By pandeyism

Posted 8 months ago

I have Dropdownlist , inwhich customer name are coming.Now i want when i select one customer name  then customer ID auto insert into gridview.

<asp:DropDownList ID="ddCustomerId" runat="server" OnSelectedIndexChanged="GetData"
    AutoPostBack="true">
</asp:DropDownList>

here is my gridview 

<asp:GridView runat="server" AutoGenerateColumns="false" ID="GridView1" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
        <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
        <asp:BoundField DataField="ShipVia" HeaderText="ShipVia" />
        <asp:BoundField DataField="Freight" HeaderText="Freight" />
        <asp:BoundField DataField="Amount" HeaderText="Amount" />
    </Columns>
</asp:GridView>

please suggestion or guide...

You are viewing reply posted by: pandeyism 8 months ago.
Posted 8 months ago

Hi akhter,

Please refer below sample.

SQL

CREATE TABLE TableTest(ID INT IDENTITY, Name VARCHAR(20))

 

HTML

<asp:DropDownList ID="ddCustomerName" runat="server" OnSelectedIndexChanged="GetData"
    AutoPostBack="true">
</asp:DropDownList>
<asp:GridView runat="server" AutoGenerateColumns="false" ID="GridView1">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string query = "SELECT Name FROM Customers";
        DataTable dt = GetData(query);
        ddCustomerName.DataSource = dt;
        ddCustomerName.DataTextField = "Name";
        ddCustomerName.DataValueField = "Name";
        ddCustomerName.DataBind();
        ddCustomerName.Items.Insert(0, new ListItem("Select CustomerId", "0"));
    }
}

protected void GetData(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO TableTest(Name) VALUES(@Name)", con))
        {
            cmd.Parameters.AddWithValue("@Name", ddCustomerName.SelectedItem.Value);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            string query = "SELECT * FROM TableTest";
            GridView1.DataSource = GetData(query);
            this.GridView1.DataBind();
        }
    }
}
public DataTable GetData(string query)
{        
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim query As String = "SELECT Name FROM Customers"
        Dim dt As DataTable = GetData(query)
        ddCustomerName.DataSource = dt
        ddCustomerName.DataTextField = "Name"
        ddCustomerName.DataValueField = "Name"
        ddCustomerName.DataBind()
        ddCustomerName.Items.Insert(0, New ListItem("Select CustomerId", "0"))
    End If
End Sub

Protected Sub GetData(ByVal sender As Object, ByVal e As EventArgs)
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("INSERT INTO TableTest(Name) VALUES(@Name)", con)
            cmd.Parameters.AddWithValue("@Name", ddCustomerName.SelectedItem.Value)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            Dim query As String = "SELECT * FROM TableTest"
            GridView1.DataSource = GetData(query)
            Me.GridView1.DataBind()
        End Using
    End Using
End Sub

Public Function GetData(ByVal query As String) As DataTable
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(query, con)
            Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim dt As DataTable = New DataTable()
                da.Fill(dt)
                Return dt
            End Using
        End Using
    End Using
End Function

Screenshot