Thursday 4 April 2013

How To Get Newly Inserted Record In Sql Server DataBase Using ADO.Net In Asp.Net


How To Get Newly Inserted Record In Sql Server DataBase Using ADO.Net In Asp.Net
Some of my newly created article in asp.net articles are as follows.
Redirection To Login page After Session Time Out In Asp.Net ,Asp.Net Pass Multiple Parameters in Query String or URL, Jquery Code To Get Browser Name And Version In Asp.Net,Binding Dropdownlist Control To An xml File Using C# in Asp.Net ,Asp.Net GridView Image Bind

So for this article first you needed to create an asp.net application. In this article add a new page and then add some textbox and button control. After adding the control your html will look as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="BindGridView.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How To Import Excelsheet Data Into SQL Server Using Asp.Net In c#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     
 
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name :<asp:TextBox ID="TextBox1" runat="server"
 
            Width="200px"></asp:TextBox>
        <br />
        <br />
&nbsp;&nbsp;&nbsp; Address:<asp:TextBox ID="TextBox2" runat="server" Width="200px"></asp:TextBox>
        <br />
        <br />
        ContactNo :<asp:TextBox ID="TextBox3" runat="server" Width="200px"></asp:TextBox>
        <br />
        <br />
     
 
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save"
            Width="93px" />
        <br />
        <br />
        <asp:Label ID="lblmessage" runat="server"
 
            style="font-weight: 700; color: #FFFFFF; background-color: #FF0000"
 
            Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Now add the below code on button click event of the button.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Data.OleDb;

namespace BindGridView
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection objcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            try
            {
               
 DataTable objdt = new DataTable();
                string query = "Insert into student([Name],[Address],[ContactNo]) values(`" + TextBox1.Text + "`,`" + TextBox2.Text + "`,`" + TextBox3.Text + "`);SELECT @@IDENTITY as Pkvalue;";
                SqlDataAdapter objda = new SqlDataAdapter(query, objcon);
                objcon.Open();
                objda.Fill(objdt);
                objcon.Close();
                lblmessage.Text = "Your newly saved record Id is :" + objdt.Rows[0]["Pkvalue"].ToString();
            }
            catch
            {
                lblmessage.Text = "Error while saving the record.";
            }
        }
    }
}

In this after insert query i have added
 SELECT @@IDENTITY as Pkvalue; this will return the newly inserted column value.
Here i have created datatable and stored the record in datable.
 
Now open your sql server.

id key

Here we have 6 record in out table. Now run the page and add the record.

insert form

now click on save here after saving the data. Here is the datatable record  which i am displaying of datatable.

datatable

Now press F5 to complete the precess. Now here is the new records in the sql table.

primery key

and here is the final output .

primery key

No comments:

Post a Comment