Insert Primary Key column value in ASP.Net GridView using C# and VB.Net

Last Reply 11 days ago By pandeyism

Posted 13 days ago

i have record in which i am taking Primary key in a text box and customer name  in a dropdowlist ,Now how to insert Primary ID into gridview?

 

<asp:DropDownList ID="ddCustomerName" runat="server" OnSelectedIndexChanged="GetData"
    AutoPostBack="true">
</asp:DropDownList>
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:GridView runat="server" AutoGenerateColumns="false" ID="GridView1">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
</asp:GridView>

 

Posted 11 days ago

Hi akhter,

Please refer below sample.

HTML

<asp:DropDownList ID="ddCustomerName" runat="server" OnSelectedIndexChanged="SelectedtData"
    AutoPostBack="true">
    <asp:ListItem Text="John Hammond" />
    <asp:ListItem Text="Mudassar Khan" />
    <asp:ListItem Text="Suzanne Mathews" />
    <asp:ListItem Text="Robert Schidner" />
</asp:DropDownList>
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
<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
Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string query = "SELECT MAX(CustomerId) CustomerId FROM Customers";
        DataTable dt = GetData(query);
        txtId.Text = dt.Rows[0]["CustomerId"].ToString();
        ddCustomerName.Items.Insert(0, new ListItem("Select Name", "0"));
    }
}

protected void SelectedtData(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO TableTest(ID,Name) VALUES(@ID,@Name)", con))
        {
            cmd.Parameters.AddWithValue("@ID", txtId.Text);
            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 MAX(CustomerId) CustomerId FROM Customers"
        Dim dt As DataTable = GetData(query)
        txtId.Text = dt.Rows(0)("CustomerId").ToString()
        ddCustomerName.Items.Insert(0, New ListItem("Select Name", "0"))
    End If
End Sub

Protected Sub SelectedtData(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(ID,Name) VALUES(@ID,@Name)", con)
            cmd.Parameters.AddWithValue("@ID", txtId.Text)
            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