My First Web App Using Asp.Net
//ASPX CODE FOR LOGIN PAGE//
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="sonu.login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Login</title>
<script>
function fnvalidate() {
if (document.getElementById("txtUserName").value == "") {
alert("Please Enter your Roll Number");
return false;
}
if (document.getElementById("txtPassword").value == "") {
alert("Please Enter your Class");
return false;
}
}
</script>
<style type="text/css">
body {
background-image: url(p.jpg);
background-size: cover;
}
.aa {
width: 250px;
height: 250px;
background-color: rgba(0,0,0,0.2);
margin: 0 auto;
margin-top: 40px;
padding-top: 10px;
padding-left: 50px;
border-radius: 15px;
-webkit-border-radius: 15px;
-o-border-radius: 15px;
-moz-border-radius: 15px;
color: white;
font-weight: bolder;
}
.aa img {
width: 60px;
height: 60px;
}
.auto-style1 {
width: 78px;
}
.lbl {
background-color:red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="aa">
<h2>Login</h2>
<table>
<tr>
<td><asp:Label runat="server" ID="lblMsgA" Text="" CssClass="lbl"></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblMsg" Text="Roll:"></asp:Label>
</td>
<td class="auto-style1">
<input type="text" id="txtUserName" name="userName" />
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblMsg1" Text="Class:"></asp:Label>
</td>
<td class="auto-style1">
<input type="password" id="txtPassword" name="password" />
</td>
</tr>
<tr>
<td>
</td>
<td class="auto-style1">
<asp:Button runat="server" ID="btnLogin" Text="LogIn" OnClick="btnLogin_Click" OnClientClick="return fnvalidate()"/>
</td> </tr>
</table>
<%--<asp:Button runat="server" ID="btnSignup" Text="SignUp" OnClick="btnSignup_Click" />--%>
</div>
</form>
</body>
</html>
//C# CODE FOR LOGIN PAGE//
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sonu
{
public partial class login : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
string userName = "";
string password = "";
string userNameR = "";
string passwordR = "";
protected void btnLogin_Click(object sender, EventArgs e)
{
userName = Request.Form["userName"];
password = Request.Form["password"];
MySqlConnection con = new MySqlConnection(conStr);
MySqlDataReader dreader;
string query = " select roll,class from studentDetails where roll='" + userName + "' and class='" + password + "';";
con.Open();
MySqlCommand cmd = new MySqlCommand(query, con);
dreader = cmd.ExecuteReader();
while (dreader.Read())
{
userNameR = dreader["roll"].ToString();
passwordR = dreader["class"].ToString();
}
if ( userNameR == userName && passwordR == password)
{
Response.Redirect("gridView.aspx");
}
else
{
lblMsgA.Text = "..Try Again!!!";
}
con.Close();
}
//protected void btnSignup_Click(object sender, EventArgs e)
//{
// Response.Redirect("studentDetails.aspx");
//}
}
}
//ASPX File of Grid View Student Details view//
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView.aspx.cs" Inherits="sonu.UpdateEdit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>student Details GridView</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
function validate() {
if (document.getElementById("txtRoll").value == "") {
alert("Please Enter your Roll No.");
return false;
}
if (document.getElementById("txtName").value == "") {
alert("Please Enter your Name");
return false;
}
if (document.getElementById("txtClass").value == "") {
alert("Please Enter your Class ");
return false;
}
if (document.getElementById("txtAddress").value == "") {
alert("Please Enter your Address");
return false;
}
if (document.getElementById("txtContact").value == "") {
alert("Please Enter your Contact No.");
return false;
}
}
function DeleteConfirm() {
var Ans = confirm("Do you want to Delete Selected Student Record?");
if (Ans) {
return true;
}
else {
return false;
}
}
</script>
<script>
//function ShowIframe(sr) {
// var row = sr.parentElement.parentElement;
// var cells = row.getElementsByTagName("td");
// var SrNo = cells[1].innerText;
// var src = "update.aspx?srno=" + SrNo + "";
// newwindow = window.open(src, 'Update', 'height=400,width=600');
// if (window.focus) { newwindow.focus() }
// return false;
//}
function ShowDetailView(sr) {
//var row = sr.parentElement.parentElement;
//var cells = row.getElementsByTagName("td");
//var SrNo = cells[1].innerText;
var src = "ViewStudentDetails.aspx?srno=" + sr + "";
newwindow = window.open(src, 'DetailView', 'height=400,width=450');
if (window.focus) { newwindow.focus() }
return false;
}
</script>
<style>
body {
background-color: whitesmoke;
}
/*h2 {
background-color: red;
}*/
.grdview {
width: 100%;
}
.grdview th {
width: 20px !important;
}
.grdview td {
width: 10px;
}
/*.frm {
margin-left: 500px;
margin-right: 550px;
}*/
.frm1 {
margin-left: 200px;
margin-right: 200px;
background-color: lightblue;
}
h2 {
/*margin-left: 500px;
margin-right: 550px;*/
height:80px;
background-color:navy;
color:white;
font-weight:bold;
text-align:center;
font-size:45px;
}
.successPanel {
color: red;
}
.lbl {
margin-left: 790px;
}
.auto-style1 {
width: 225px;
}
.auto-style2 {
}
.auto-style5 {
width: 625px;
}
.footer {
margin: 45px 0px 30px 0px;
background-color: lightpink;
border-top-color: #26BD71;
text-align: center;
}
</style>
</head>
<body>
<h2>Student Details Form</h2>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="lblMsg"></asp:Label>
<marquee direction="left" behavior="alternate" bgcolor="lightpink" >............This is the my First Web Application............</marquee>
</div>
<div class="frm1">
<table>
<tr >
<td class="auto-style1"></td>
<td class="auto-style2" >
<asp:Button ID="btnemoAjax" runat="server" Text="Demo Ajax" Width="102px" OnClick="btnemoAjax_Click" />
<asp:TextBox ID="ser" runat="server" placeholder="Enter Roll OR Name OR Class" style="margin-left: 0px" Width="200px"></asp:TextBox>
<asp:Button ID="btnSer" runat="server" Text="Search" OnClick="btnSer_Click" Width="92px" />
<asp:Button ID="btnAdd" runat="server" Text="Add" Width="102px" OnClick="btnAdd_Click" />
<asp:Button ID="btnDeleteRecord" runat="server" Text="Delete" Width="102px" OnClick="btnDeleteRecord_Click" />
<asp:LinkButton ID="LinkButton1" runat="server" class="btnlink" OnClick="LinkButton1_Click"><img src="logout.jpg" height="20" width="60" /></asp:LinkButton>
</td>
</tr>
</table>
<table>
<tr>
<td class="auto-style5">
</td>
<td class="auto-style4">
<asp:DropDownList ID="dropState" runat="server" OnSelectedIndexChanged="dropState_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
<asp:DropDownList ID="dropCountry" runat="server" OnSelectedIndexChanged="dropCountry_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
<asp:Label ID="lblCount" runat="server" Text="" ></asp:Label>
</td>
</tr>
</table>
<fieldset>
<legend>Grid View</legend>
<table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="grdview" AllowPaging="true" PageSize="10" OnPageIndexChanging="OnPageIndexChanging" >
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-Width="0.15%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkdel" runat="server" />
<input type="hidden" value="" id="cid" />
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="srno" HeaderText="Serialno" ItemStyle-CssClass="trItem" ItemStyle-Width="0.15%" HeaderStyle-Width="0.15%" ItemStyle-HorizontalAlign="Center" />--%>
<asp:TemplateField HeaderText="Serial No." ItemStyle-Width="1.15%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblsrno" runat="server" Text='<%# Container.DataItemIndex +1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="roll" HeaderText="Roll No." ItemStyle-Width="2.15%" HeaderStyle-Width="2.15%" ItemStyle-HorizontalAlign="Center" />
<%-- <asp:BoundField DataField="name" HeaderText="Name" ItemStyle-Width="5.5%" HeaderStyle-Width="5.15%" ItemStyle-HorizontalAlign="Center" />--%>
<asp:TemplateField HeaderText="Name" HeaderStyle-Font-Size="Medium" ItemStyle-Width="5%" ItemStyle-Font-Size="Medium" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkview" runat="server" Text="" OnClientClick="ShowDbbetailView('this')"><a href="#" onclick="ShowDetailView('<%#Eval("srno")%>')"><%#Eval("name")%></a>
</asp:LinkButton>
</ItemTemplate>
<%--<HeaderStyle></HeaderStyle>
<ItemStyle></ItemStyle>--%>
</asp:TemplateField>
<asp:BoundField DataField="class" HeaderText="Class" ItemStyle-Width="0.15%" HeaderStyle-Width="0.15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="address" HeaderText="Address" ItemStyle-Width="3.15%" HeaderStyle-Width="3.15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="state" HeaderText="State" ItemStyle-Width="1.15%" HeaderStyle-Width="1.15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="1.15%" HeaderStyle-Width="1.15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="contact" HeaderText="Contact" ItemStyle-Width="1.15%" HeaderStyle-Width="1.15%" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Link" ItemStyle-Width="0.15%" HeaderStyle-Width="0.15%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%--<asp:LinkButton ID="btnUpdate" OnClientClick="return ShowIframe(this);" runat="server" CammandName="upd">Update </asp:LinkButton>--%>
<asp:LinkButton ID="btnUpdate" runat="server" CommandName="upd" CommandArgument='<%#Eval("srno")%>' >
<a href="update.aspx?srno=<%#Eval("srno")%>"> update</a></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</table>
</fieldset>
</div>
</form>
<footer>
<div class="footer">
<span>All Rights Are Reserved At Student Application</span>
<address>Cation Group Gurgaon - 110011 <b>INDIA</b></address>
</div>
</footer>
</body>
</html>
//C# Code for Grid View Student Details view//
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sonu
{
public partial class UpdateEdit : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
showdata();
bindCountry();
bindState();
btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
}
}
protected void DeleteRecord(String roll)
{
string str = "";
MySqlConnection con = new MySqlConnection(conStr);
str = "delete from studentDetails where roll='" + roll + "'";
MySqlCommand cmd = new MySqlCommand(str, con);
con.Open();
int i=cmd.ExecuteNonQuery();
if (i>0)
{
lblMsg.Text = "Record has been successfully deleted";
lblMsg.CssClass = "successPanel";
}
con.Close();
}
protected void btnDeleteRecord_Click(object sender, EventArgs e)
{
foreach (GridViewRow grow in GridView1.Rows)
{
//Searching CheckBox("chkDel") in an individual row of Grid
CheckBox chkdel = (CheckBox)grow.FindControl("chkDel");
//If CheckBox is checked than delete the record with particular srno.
if (chkdel.Checked)
{
string roll = (grow.Cells[2].Text);
DeleteRecord(roll);
}
}
Response.Redirect("GridView.aspx");
}
protected void showdata()
{
string query = " select dbSt.srno,dbSt.roll,dbSt.name,dbSt.class,dbSt.address,dbS.state as state,dbC.Country as country,dbSt.contact ";
query += " from studentdetails dbSt ";
query += " inner join addcountry dbC on dbC.srno=dbSt.Country ";
query += " inner join addstate dbS on dbS.srno=dbSt.state";
if (ser.Text.ToString().Trim().Length > 0)
{
query += " where dbSt.roll LIKE '%" + ser.Text + "%' or dbSt.name LIKE '%" + ser.Text + "%' or dbSt.class LIKE '%" + ser.Text + "%'";
}
if (dropCountry.SelectedIndex > 0)
{
query += " where dbC.Country LIKE '%" + dropCountry.SelectedItem + "%'";
}
if (dropState.SelectedIndex > 0)
{
query += " where dbS.State LIKE '%" + dropState.SelectedItem + "%'";
}
query += " order BY ModificationDate DESC";
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
lblCount.Text = "Total Record:" + ds.Tables[0].Rows.Count.ToString();
}
else
{
lblCount.Text = "No Record";
}
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("studentDetails.aspx");
}
protected void btnSer_Click(object sender, EventArgs e)
{
showdata();
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
showdata();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("login.aspx");
}
public void bindState()
{
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select State,srno FROM addState", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dropState.DataSource = ds;
dropState.DataTextField = "State";
dropState.DataValueField = "srno";
dropState.DataBind();
dropState.Items.Insert(0, new ListItem("--State--", "0"));
con.Close();
}
public void bindCountry()
{
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select srno,Country FROM addcountry", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dropCountry.DataSource = ds;
dropCountry.DataTextField = "Country";
dropCountry.DataValueField = "srno";
dropCountry.DataBind();
dropCountry.Items.Insert(0, new ListItem("--Country--", "0"));
con.Close();
}
protected void dropCountry_SelectedIndexChanged(object sender, EventArgs e)
{
showdata();
}
protected void dropState_SelectedIndexChanged(object sender, EventArgs e)
{
showdata();
}
protected void btnemoAjax_Click(object sender, EventArgs e)
{
Response.Redirect("DataInsertFromAjax.aspx");
}
}
}
//ASPX code for Student Details form
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="studentDetails.aspx.cs" Inherits="sonu.studentDetails" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Student Details</title>
<script type="text/javascript">
function validate() {
if (document.getElementById("txtRoll").value == "")
{
alert("Please Enter your Roll No.");
return false;
}
if (document.getElementById("txtName").value == "")
{
alert("Please Enter your Name");
return false;
}
if (document.getElementById("txtClass").value == "")
{
alert("Please Enter your Course ");
return false;
}
if (document.getElementById("txtAddress").value == "")
{
alert("Please Enter your Address");
return false;
}
if (document.getElementById("dropState").value == "0") {
alert("Please Select State.");
return false;
}
if (document.getElementById("dropCountry").value == "0") {
alert("Please Select Country.");
return false;
}
if (document.getElementById("txtContact").value == "")
{
alert("Please Enter your Contact No.");
return false;
}
}
//Function to allow only numbers to textbox
function validate1(key) {
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('txtContact');
//comparing pressed keycodes
if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
return false;
}
else {
//Condition to check textbox contains ten numbers or not
if (phn.value.length < 12) {
return true;
}
else {
return false;
}
}
}
function ShowDetailView() {
var src = "addState.aspx";
newwindow = window.open(src, 'DetailView', 'height=250,width=350');
if (window.focus) { newwindow.focus() }
return false;
}
function ShowDetailView1() {
var src = "addCountry.aspx";
newwindow = window.open(src, 'DetailView', 'height=250,width=350');
if (window.focus) { newwindow.focus() }
return false;
}
function Addcourse() {
var src = "AddCourse.aspx";
newwindow = window.open(src, 'DetailView', 'height=250,width=350');
if (window.focus) { newwindow.focus() }
return false;
}
</script>
<style>
body {
margin-left:500px;
margin-right:550px;
background-color:whitesmoke;
}
h1 {
background-color:red;
}
.frm {
background-color:lightblue;
}
.auto-style1 {
width: 169px;
}
</style>
</head>
<body >
<h1> Student Details Form</h1>
<form id="form1" runat="server">
<asp:Label runat="server" ID="lblMsg"></asp:Label>
<div class="frm">
<fieldset><legend> Form</legend>
<table>
<tr>
<td>
Roll:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtRoll" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Name:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Course:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtClass" Name="class" runat="server" Width="73px"></asp:TextBox><asp:LinkButton ID="LinkButton3" runat="server" Text="" OnClientClick="Addcourse('this')"><a href="#" onclick="Addcourse()">Select Course</a>
</asp:LinkButton>
</td>
</tr>
<tr>
<td>
Address:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtAddress" Textmode="multiline" runat ="server" Width="166px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
State:
</td>
<td class="auto-style1">
<asp:DropDownList ID="dropState" runat="server" Width="125px" >
</asp:DropDownList>
<asp:LinkButton ID="LinkButton1" runat="server" Text="" OnClientClick="ShowDbbetailView('this')"><a href="#" onclick="ShowDetailView()">Add</a>
</asp:LinkButton>
</td>
</tr>
<tr>
<td>
Country:
</td>
<td class="auto-style1">
<asp:DropDownList ID="dropCountry" runat="server" Width="125px" style="margin-bottom: 8px">
</asp:DropDownList> <asp:LinkButton ID="LinkButton2" runat="server" Text="" OnClientClick="ShowDbbetailView1('this')"><a href="#" onclick="ShowDetailView1()">Add</a>
</asp:LinkButton>
</td>
</tr>
<tr>
<td>
Contact:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtContact" runat="server" onkeypress="return validate1(event)"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" text="Submit" OnClick="btnSave_Click" OnClientClick="return validate()"/>
</td>
<td class="auto-style1">
<asp:Button ID="btnBack" runat="server" text="Back" OnClick="btnBack_Click"/>
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
// C# code for Student details form//
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sonu
{
public partial class studentDetails : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
bindCountry();
bindState();
}
}
public void bindCountry() {
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select srno,Country FROM addcountry", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dropCountry.DataSource = ds;
dropCountry.DataTextField = "Country";
dropCountry.DataValueField = "srno";
dropCountry.DataBind();
dropCountry.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
public void bindState()
{
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select State,srno FROM addState", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dropState.DataSource = ds;
dropState.DataTextField = "State";
dropState.DataValueField = "srno";
dropState.DataBind();
dropState.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
String stxtRoll ="";
String stxtName = "";
String stxtClass = "";
String stxtAddress = "";
String sdropState = "";
String sdropCountry = "";
String stxtContact = "";
protected void btnSave_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(conStr);
string sqlQuery = "select * from studentDetails where roll='" + txtRoll.Text.Trim() + "'";
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(sqlQuery, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
lblMsg.Text = "Please Enter another Roll Number. ";
lblMsg.CssClass = "errorMsg";
}
else
{
stxtRoll = txtRoll.Text;
stxtName = txtName.Text;
stxtClass = txtClass.Text;
stxtAddress = txtAddress.Text;
sdropState = dropState.SelectedValue.ToString();
sdropCountry = dropCountry.SelectedValue.ToString();
stxtContact = txtContact.Text;
String query = "Insert into studentDetails(roll,name,class,address,state,country,contact,ModificationDate) values('" + stxtRoll + "','" + stxtName + "','" + stxtClass + "','" + stxtAddress + "','" + sdropState + "','" + sdropCountry + "','" + stxtContact + "',now())";
MySqlCommand cmd = new MySqlCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblMsg.Text = "success";
}
else
{
lblMsg.Text = "success";
}
con.Close();
txtRoll.Text = "";
txtName.Text = "";
txtClass.Text = "";
txtAddress.Text = "";
txtContact.Text = "";
dropCountry.SelectedIndex = 0;
dropState.SelectedIndex = 0;
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("GridView.aspx");
}
}
}
//ASPX Code for ADD Course//
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddCourse.aspx.cs" Inherits="sonu.AddCourse" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Select Course</title>
<script>
function closeDialog(e) {
window.close();
}
function fnSubmit()
{
var RB1 = document.getElementById("<%=rblCourse.ClientID%>");
var radio = RB1.getElementsByTagName("input");
var isChecked = false;
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
isChecked = true;
break;
}
}
if (!isChecked) {
alert("Please select Course");
}
return isChecked;
//window.close();
}
</script>
<style>
.adc {
background-color: lightblue;
}
h1 {
background-color: red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="adc">
<h1>Courses:</h1>
<asp:Label ID="lbl" runat="server"></asp:Label>
<fieldset><legend>Course:</legend>
<table>
<tr>
<td>
<asp:RadioButtonList ID="rblCourse" runat="server">
<asp:ListItem Text="MCA" Value="MCA"></asp:ListItem>
<asp:ListItem Text="BCA" Value="BCA"></asp:ListItem>
<asp:ListItem Text="MBA" Value="MBA"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<asp:HiddenField ID="hdnCourse" runat="server" />
<asp:HiddenField ID="hdnValue" runat="server" />
<tr>
<td> <asp:Button ID="btnSumit" runat="server" Text="Submit" OnClick="btnSumit_Click" OnClientClick="return fnSubmit()"/> <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnClose_Click" OnClientClick="closeDialog(this)"/></td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
//C# code for ADD Course//
All Work complete with Jquery in ASPX Code
// ASPX code of ADD STATE//
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="addState.aspx.cs" Inherits="sonu.addState" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Add Country</title>
<script lang="text/javascript">
function validate() {
if (document.getElementById("txtState").value == "") {
alert("Please Enter New State.");
return false;
}
}
function closeDialog(e) {
window.close();
window.parent.opener.location.reload();
}
</script>
<style>
body {
/*margin-left:500px;
margin-right:550px;*/
background-color:whitesmoke;
}
h1 {
background-color:red;
}
.frm {
background-color:lightblue;
}
.lblMsg1 {
color:red;
}
.auto-style1 {
width: 200px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Label runat="server" ID="lblMsg" CssClass="lblMsg1"></asp:Label>
<h1>Add State Name</h1>
<div class="frm">
<fieldset><legend>Add State</legend>
<table>
<tr>
<td class="auto-style1">
State Name:
</td>
<td>
<asp:TextBox ID="txtState" runat="server" Width="184px"></asp:TextBox> </td>
</tr>
<tr>
<td class="auto-style1">
</td>
<td style="text-align:right">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return validate()" OnClick="btnSave_Click" />
<asp:Button ID="btnBack" runat="server" Text="Close" OnClick="btnClose_Click" OnClientClick="closeDialog(this)" />
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
// C# code for ADD STATE//
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sonu
{
public partial class addState : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
String stxtState = "";
protected void btnSave_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(conStr);
string sqlQuery = "select * from addState where State='" + txtState.Text.Trim() + "'";
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(sqlQuery, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
lblMsg.Text = "Please Enter another State Name. ";
lblMsg.CssClass = "errorMsg";
}
else
{
stxtState = txtState.Text;
String query = "Insert into addState(State) values('" + stxtState + "')";
MySqlCommand cmd = new MySqlCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblMsg.Text = "success";
}
else
{
lblMsg.Text = "success";
}
con.Close();
txtState.Text = "";
}
}
protected void btnClose_Click(object sender, EventArgs e)
{
}
}
}
//ASPX CODE FOR ADD COUNTRY//
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="addCountry.aspx.cs" Inherits="sonu.addCountry" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Add Country</title>
<script lang="text/javascript">
function validate() {
if (document.getElementById("txtCountry").value == "") {
alert("Please Enter New Country.");
return false;
}
}
function closeDialog(e) {
window.close();
window.parent.opener.location.reload();
}
</script>
<style>
body {
/*margin-left:500px;
margin-right:550px;*/
background-color:whitesmoke;
}
.frm {
background-color:lightblue;
}
h1 {
background-color:red;
}
.lblMsg1 {
color:red;
}
.auto-style1 {
width: 200px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Label runat="server" ID="lblMsg" CssClass="lblMsg1"></asp:Label>
<h1>Add Country Name</h1>
<div class="frm">
<fieldset><legend>Add Country</legend>
<table>
<tr>
<td class="auto-style1">
Country Name:
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server" Width="184px"></asp:TextBox> </td>
</tr>
<tr>
<td class="auto-style1">
</td>
<td style="text-align:right">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return validate()" OnClick="btnSave_Click"/>
<asp:Button ID="btnBack" runat="server" Text="Close" OnClientClick="closeDialog(this)" />
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
//C# CODE FOR ADD COUNTRY//
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sonu
{
public partial class addCountry : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
String stxtCountry = "";
protected void btnSave_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(conStr);
string sqlQuery = "select * from addCountry where Country='" + txtCountry.Text.Trim() + "'";
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(sqlQuery, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
lblMsg.Text = "Please Enter another Country Name. ";
lblMsg.CssClass = "errorMsg";
}
else
{
stxtCountry = txtCountry.Text;
String query = "Insert into addCountry(Country) values('" + stxtCountry + "')";
MySqlCommand cmd = new MySqlCommand(query, con);
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
lblMsg.Text = "success";
}
else
{
lblMsg.Text = "success";
}
txtCountry.Text = "";
}
con.Close();
}
}
}
// ASPX STUDENT DETAILS CODE FOR UPDATE DATA//
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="update.aspx.cs" Inherits="sonu.update" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Update Student Details</title>
<script type="text/javascript">
function validate() {
if (document.getElementById("txtRoll").value == "") {
alert("Please Enter your Roll No.");
return false;
}
if (document.getElementById("txtName").value == "") {
alert("Please Enter your Name");
return false;
}
if (document.getElementById("txtClass").value == "") {
alert("Please Enter your Class ");
return false;
}
if (document.getElementById("txtAddress").value == "") {
alert("Please Enter your Address");
return false;
}
if (document.getElementById("dropState").value == "0") {
alert("Please Select State.");
return false;
}
if (document.getElementById("dropCountry").value == "0") {
alert("Please Select Country.");
return false;
}
if (document.getElementById("txtContact").value == "") {
alert("Please Enter your Contact No.");
return false;
}
}
//Function to allow only numbers to textbox
function validate1(key) {
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('txtContact');
//comparing pressed keycodes
if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
return false;
}
else {
//Condition to check textbox contains ten numbers or not
if (phn.value.length < 12) {
return true;
}
else {
return false;
}
}
}
</script>
<style>
body {
background-color: whitesmoke;
}
h2 {
background-color: red;
}
.frm {
background-color: lightblue;
}
.auto-style1 {
width: 160px;
}
.grdview {
width: 100%;
}
.grdview th {
width: 20px !important;
}
.grdview td {
width: 10px;
}
.frm {
margin-left: 500px;
margin-right: 550px;
}
/*.frm1 {
margin-left: 200px;
margin-right: 200px;
}*/
h2 {
margin-left: 500px;
margin-right: 550px;
}
</style>
</head>
<body>
<h2>Student Details Form</h2>
<form id="form1" runat="server">
<asp:Label runat="server" ID="lblMsg"></asp:Label>
<div class="frm">
<fieldset>
<legend>Form</legend>
<asp:Button ID="btnBack" runat="server" Text="Back" OnClientClick="return validate()" Width="61px" OnClick="btnBack_Click" />
<table>
<tr>
<td>Serial No.:
</td>
<td class="auto-style2">
<asp:TextBox ID="txtSrno" runat="server" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<tr>
<td>Roll:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtRoll" runat="server" ReadOnly="false"></asp:TextBox>
</td>
</tr>
<tr>
<td>Name:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Class:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtClass" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Address:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtAddress" TextMode="multiline" runat="server" Width="160px"></asp:TextBox>
</td>
</tr>
<tr>
<td>State:
</td>
<td>
<asp:DropDownList ID="dropState" runat="server" Width="169px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Country:
</td>
<td>
<asp:DropDownList ID="dropCountry" runat="server" Width="168px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Contact:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtContact" runat="server" onkeypress="return validate1(event)"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td class="tdd">
<asp:Button ID="btnUpate" runat="server" Text="Update" OnClientClick="return validate()" OnClick="btnUpate_Click" />
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
// C # STUDENT DETAILS CODE FOR UPDATE DATA//
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sonu
{
public partial class update : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();
bindCountry();
bindState();
}
}
public void bindCountry()
{
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select srno,Country FROM addcountry", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dropCountry.DataSource = ds;
dropCountry.DataTextField = "Country";
dropCountry.DataValueField = "srno";
dropCountry.DataBind();
dropCountry.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
public void bindState()
{
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select State,srno FROM addState", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dropState.DataSource = ds;
dropState.DataTextField = "State";
dropState.DataValueField = "srno";
dropState.DataBind();
dropState.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
protected void btnUpate_Click(object sender, EventArgs e)
{
string query = "UPDATE studentdetails SET roll='" + txtRoll.Text + "', name='" + txtName.Text + "',class='" + txtClass.Text + "',address='" + txtAddress.Text + "', state='" + dropState.SelectedValue.ToString() + "',country='" + dropCountry.SelectedValue.ToString() + "',contact='" + txtContact.Text + "',ModificationDate= now() WHERE srno=" + txtSrno.Text;
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
lblMsg.Text = "Update Success";
}
protected void getdata()
{
string srno = "";
srno = Request.QueryString["srno"];
if (srno!=null)
{
string query = "select roll,name,class,address,state,country,contact from studentdetails where srno=" + srno;
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataReader mydr = cmd.ExecuteReader();
while (mydr.Read())
{
txtSrno.Text = srno;
txtRoll.Text = mydr["roll"].ToString();
txtName.Text = mydr["name"].ToString();
txtClass.Text = mydr["class"].ToString();
txtAddress.Text = mydr["address"].ToString();
dropState.Text = mydr["state"].ToString();
dropCountry.Text = mydr["country"].ToString();
txtContact.Text = mydr["contact"].ToString();
}
con.Close();
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("GridView.aspx");
}
}
}
//ASPX CODE FOR STUDENT DETAILS POPUP//
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="studentDetails.aspx.cs" Inherits="sonu.studentDetails" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Student Details</title>
<script type="text/javascript">
function validate() {
if (document.getElementById("txtRoll").value == "")
{
alert("Please Enter your Roll No.");
return false;
}
if (document.getElementById("txtName").value == "")
{
alert("Please Enter your Name");
return false;
}
if (document.getElementById("txtClass").value == "")
{
alert("Please Enter your Course ");
return false;
}
if (document.getElementById("txtAddress").value == "")
{
alert("Please Enter your Address");
return false;
}
if (document.getElementById("dropState").value == "0") {
alert("Please Select State.");
return false;
}
if (document.getElementById("dropCountry").value == "0") {
alert("Please Select Country.");
return false;
}
if (document.getElementById("txtContact").value == "")
{
alert("Please Enter your Contact No.");
return false;
}
}
//Function to allow only numbers to textbox
function validate1(key) {
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('txtContact');
//comparing pressed keycodes
if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
return false;
}
else {
//Condition to check textbox contains ten numbers or not
if (phn.value.length < 12) {
return true;
}
else {
return false;
}
}
}
function ShowDetailView() {
var src = "addState.aspx";
newwindow = window.open(src, 'DetailView', 'height=250,width=350');
if (window.focus) { newwindow.focus() }
return false;
}
function ShowDetailView1() {
var src = "addCountry.aspx";
newwindow = window.open(src, 'DetailView', 'height=250,width=350');
if (window.focus) { newwindow.focus() }
return false;
}
function Addcourse() {
var src = "AddCourse.aspx";
newwindow = window.open(src, 'DetailView', 'height=250,width=350');
if (window.focus) { newwindow.focus() }
return false;
}
</script>
<style>
body {
margin-left:500px;
margin-right:550px;
background-color:whitesmoke;
}
h1 {
background-color:red;
}
.frm {
background-color:lightblue;
}
.auto-style1 {
width: 169px;
}
</style>
</head>
<body >
<h1> Student Details Form</h1>
<form id="form1" runat="server">
<asp:Label runat="server" ID="lblMsg"></asp:Label>
<div class="frm">
<fieldset><legend> Form</legend>
<table>
<tr>
<td>
Roll:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtRoll" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Name:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Course:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtClass" Name="class" runat="server" Width="73px"></asp:TextBox><asp:LinkButton ID="LinkButton3" runat="server" Text="" OnClientClick="Addcourse('this')"><a href="#" onclick="Addcourse()">Select Course</a>
</asp:LinkButton>
</td>
</tr>
<tr>
<td>
Address:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtAddress" Textmode="multiline" runat ="server" Width="166px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
State:
</td>
<td class="auto-style1">
<asp:DropDownList ID="dropState" runat="server" Width="125px" >
</asp:DropDownList>
<asp:LinkButton ID="LinkButton1" runat="server" Text="" OnClientClick="ShowDbbetailView('this')"><a href="#" onclick="ShowDetailView()">Add</a>
</asp:LinkButton>
</td>
</tr>
<tr>
<td>
Country:
</td>
<td class="auto-style1">
<asp:DropDownList ID="dropCountry" runat="server" Width="125px" style="margin-bottom: 8px">
</asp:DropDownList> <asp:LinkButton ID="LinkButton2" runat="server" Text="" OnClientClick="ShowDbbetailView1('this')"><a href="#" onclick="ShowDetailView1()">Add</a>
</asp:LinkButton>
</td>
</tr>
<tr>
<td>
Contact:
</td>
<td class="auto-style1">
<asp:TextBox ID="txtContact" runat="server" onkeypress="return validate1(event)"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" text="Submit" OnClick="btnSave_Click" OnClientClick="return validate()"/>
</td>
<td class="auto-style1">
<asp:Button ID="btnBack" runat="server" text="Back" OnClick="btnBack_Click"/>
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
//C# CODE FOR STUDENT DETAILS POPUP//
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sonu
{
public partial class studentDetails : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
bindCountry();
bindState();
}
}
public void bindCountry() {
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select srno,Country FROM addcountry", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dropCountry.DataSource = ds;
dropCountry.DataTextField = "Country";
dropCountry.DataValueField = "srno";
dropCountry.DataBind();
dropCountry.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
public void bindState()
{
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select State,srno FROM addState", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dropState.DataSource = ds;
dropState.DataTextField = "State";
dropState.DataValueField = "srno";
dropState.DataBind();
dropState.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
String stxtRoll ="";
String stxtName = "";
String stxtClass = "";
String stxtAddress = "";
String sdropState = "";
String sdropCountry = "";
String stxtContact = "";
protected void btnSave_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(conStr);
string sqlQuery = "select * from studentDetails where roll='" + txtRoll.Text.Trim() + "'";
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(sqlQuery, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
lblMsg.Text = "Please Enter another Roll Number. ";
lblMsg.CssClass = "errorMsg";
}
else
{
stxtRoll = txtRoll.Text;
stxtName = txtName.Text;
stxtClass = txtClass.Text;
stxtAddress = txtAddress.Text;
sdropState = dropState.SelectedValue.ToString();
sdropCountry = dropCountry.SelectedValue.ToString();
stxtContact = txtContact.Text;
String query = "Insert into studentDetails(roll,name,class,address,state,country,contact,ModificationDate) values('" + stxtRoll + "','" + stxtName + "','" + stxtClass + "','" + stxtAddress + "','" + sdropState + "','" + sdropCountry + "','" + stxtContact + "',now())";
MySqlCommand cmd = new MySqlCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblMsg.Text = "success";
}
else
{
lblMsg.Text = "success";
}
con.Close();
txtRoll.Text = "";
txtName.Text = "";
txtClass.Text = "";
txtAddress.Text = "";
txtContact.Text = "";
dropCountry.SelectedIndex = 0;
dropState.SelectedIndex = 0;
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("GridView.aspx");
}
}
}
//ASPX CODE FOR PERSONAL DETAILS WITH AJAX//
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataInsertFromAjax.aspx.cs" Inherits="sonu.DataInsertFromAjax" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<title>Demo of Ajax</title>
<script>
$(document).ready(function () {
$('#btnsubmit').click(function () {
if (document.getElementById("txtName").value == "") {
alert("plz Enter your Name");
return false;
}
if (document.getElementById("txtAge").value == "") {
alert("plz Enter your Age");
return false;
}
if (document.getElementById("ddlSex").value == "0") {
alert("plz fill Sex");
return false;
}
else {
a();
}
});
});
function a() {
var name = $('#txtName').val();
var age = $('#txtAge').val();
var sex = $('#ddlSex').val();
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'DataInsertFromAjax.aspx/Insert_Data',
data: "{'name':'" + name + "','age':'" + age + "','sex':'" + sex + "'}",
async: false,
success: function (response) {
$('#txtName').val(''); $('#txtAge').val(''); $('#ddlSex').val('');
alert("Record saved successfully..!!");
},
error: function () {
alert("Error");
}
});
}
</script>
<style>
.tbl {
background-color: lightgreen;
margin: 100px 100px 100px 400px;
width: 243px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="tbl">
<fieldset>
<legend>Personal Details</legend>
<table >
<tr>
<td width="40%"></td>
<td align="right">
<asp:Button runat="server" ID="btnBack" Text="Back" OnClick="btnBack_Click1"/>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblName" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblAge" Text="Age"></asp:Label>
</td>
<td>
<asp:TextBox runat="server" ID="txtAge"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblSex" Text="Sex"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlSex" runat="server">
<asp:ListItem Value="0">--Select---</asp:ListItem>
<asp:ListItem Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td width="40%"></td>
<td>
<asp:Button runat="server" ID="btnsubmit" Text="Submit" OnClientClick="return validate();"/>
</td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Grid View</legend>
<table >
<tr>
<td align="right">
<asp:Label runat="server" ID="lblCount"></asp:Label></td>
</tr>
<tr>
<td>
<asp:GridView ID="gvDetails" runat="server" OnSelectedIndexChanged="gvDetails_SelectedIndexChanged">
</asp:GridView>
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
//C# CODE FOR PERSONAL DETAILS WITH AJAX//
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace sonu
{
public partial class DataInsertFromAjax : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
showdata();
}
//public class userDetails
//{
// public string name { get; set; }
// public string age { get; set; }
// public string sex { get; set; }
//}
protected void btnSumbmit_Click(object sender, EventArgs e)
{
}
[WebMethod]
public static string Insert_Data(string name, string age, string sex)
{
MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
try
{
con.Open();
MySqlCommand cmd = new MySqlCommand("Insert into Personal_Details (Name,Age,Sex) values(@name,@age,@sex)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@age", age);
cmd.Parameters.AddWithValue("@sex", sex);
cmd.ExecuteNonQuery();
con.Close();
return "Success";
}
catch (Exception e)
{
return "failure";
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("GridView.aspx");
}
protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void showdata()
{
string query = " select name,age,sex from Personal_Details ";
MySqlConnection con = new MySqlConnection(conStr);
con.Open();
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
lblCount.Text = "Total Record:" + ds.Tables[0].Rows.Count.ToString();
}
else
{
lblCount.Text = "No Record";
}
gvDetails.DataSource = ds;
gvDetails.DataBind();
con.Close();
}
protected void btnBack_Click1(object sender, EventArgs e)
{
Response.Redirect("GridView.aspx");
}
}
}