<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Insert.aspx.cs" Inherits="Insert_Function_Web_Form.Insert" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
.auto-style1{
width:100%;
}
.auto-style2 {
text-align: center;
}
.auto-style3 {
width: 120px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="1" cellspacing="2" class="auto-style1">
<tr>
<td colspan="2" class="auto-style2"><strong>Web form with database</strong></td>
</tr>
<tr>
<td class="auto-style3"></td>
<td></td>
</tr>
<tr>
<td class="auto-style3"></td>
<td></td>
</tr>
<tr>
<td class="auto-style3">Id</td>
<td>
<asp:TextBox ID="IdTextBox" runat="server" Width="172px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3">Name</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Width="172px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3">Age</td>
<td>
<asp:TextBox ID="AgeTextBox" runat="server" Width="172px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3"></td>
<td></td>
</tr>
<tr>
<td class="auto-style3"></td>
<td>
<asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Button1_Click"/>
</td>
</tr>
<tr>
<td class="auto-style3">
<asp:Label ID="Label1" runat="server" Text="Insertion" Visible="false" ForeColor="#00CC00"></asp:Label>
</td>
<td></td>
</tr>
<tr>
<td class="auto-style3"></td>
<td>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseOneConnectionString %>" DeleteCommand="DELETE FROM [utbl] WHERE [Id] = @Id" InsertCommand="INSERT INTO [utbl] ([Id], [Name], [Age]) VALUES (@Id, @Name, @Age)" ProviderName="<%$ ConnectionStrings:DatabaseOneConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [utbl]" UpdateCommand="UPDATE [utbl] SET [Name] = @Name, [Age] = @Age WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Age" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Age" Type="Int32" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataKeyNames="Id" DataSourceID="SqlDataSource1" ForeColor="Black" Width="273px">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
ABOVE FILE IS Insert.aspx
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.Configuration;
using System.Security.Cryptography;
namespace Insert_Function_Web_Form
{
public partial class Insert : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["DatabaseOneConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void BindGridView()
{
try
{
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
string query = "SELECT * FROM utbl";
SqlCommand cmd = new SqlCommand(query,con);
SqlDataReader reader = cmd.ExecuteReader();
//GridView1.DataSource = reader;
GridView1.DataBind();
}
}
catch (SqlException ex)
{
Response.Write("SqlException : " + ex.Message);
}
catch (Exception ex)
{
Response.Write("Exception : " + ex.Message);
}
}
protected void EmptyControls()
{
IdTextBox.Text = "";
NameTextBox.Text = "";
AgeTextBox.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IdTextBox.Text == "" || NameTextBox.Text == "" || AgeTextBox.Text == "")
{
Response.Write("<script type='text/javascript'>alert('All fields are required !!')</script>");
Label1.Visible = false;
}
else
{
try
{
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
string query = "INSERT INTO utbl(Id,Name,Age) VALUES(@Id,@Name,@Age)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", IdTextBox.Text);
cmd.Parameters.AddWithValue("@Name", NameTextBox.Text);
cmd.Parameters.AddWithValue("@Age", AgeTextBox.Text);
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
Response.Write("<script type='text/javascript'>alert('Successfully Inserted !!')</script>");
Label1.Text = "New record inserted successfully";
Label1.Visible = true;
EmptyControls();
BindGridView();
}
else
{
Response.Write("<script type='text/javascript'>alert('Insertion Failed !!')</script>");
}
}
}
catch (SqlException ex)
{
Response.Write("SqlException : " + ex.Message);
}
catch (Exception ex)
{
Response.Write("Exception : " + ex.Message);
}
}
}
}
}
ABOVE FILE IS Insert.aspx.cs
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="DatabaseOneConnectionString" connectionString="Data Source=DESKTOP-77M6N4G\SQLEXPRESS;Initial Catalog=DatabaseOne;Integrated Security=True;TrustServerCertificate=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
</controls>
</pages>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
ABOVE FILE IS Web.config
CREATE DATABASE DatabaseOne;
USE DatabaseOne;
CREATE TABLE utbl(
Id INT NOT NULL PRIMARY KEY,
Name NVARCHAR(50) NOT NULL,
Age INT NOT NULL
);
SELECT * FROM utbl;
Comments
Post a Comment