Aspx Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NewRegistration.aspx.cs" Inherits="Registration_Page.NewRegistration" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Name
</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator runat="server" ID="reqName" ControlToValidate="txtName" ErrorMessage="Please enter your name ! " ForeColor="Red" />
</td>
</tr>
<tr>
<td>Password
</td>
<td>
<asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Email
</td>
<td>
<asp:TextBox ID="txtmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Cell
</td>
<td>
<asp:TextBox ID="txtcell" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>address
</td>
<td>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>street
</td>
<td>
<asp:TextBox ID="txtstreet" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<table>
<asp:GridView ID="membergrid" runat="server" EnableModelValidation="false" AutoGenerateColumns="false" OnRowEditing="membergrid_RowEditing" OnRowUpdating="membergrid_RowUpdating" OnRowCancelingEdit="membergrid_RowCancelingEdit">
<Columns>
<asp:CommandField ShowSelectButton="true" CausesValidation="false" HeaderText="Select" />
<asp:CommandField ShowDeleteButton="true" CausesValidation="false" HeaderText="Delete" />
<asp:CommandField ShowEditButton="true" CausesValidation="false" HeaderText="Edit" />
<asp:TemplateField HeaderText="Member ID">
<ItemTemplate>
<asp:Label ID="lblmid" runat="server" Text='<%#Eval("MID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Member Name">
<ItemTemplate>
<%#Eval("name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditname" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mail">
<ItemTemplate>
<%#Eval("mail")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditmail" runat="server" Text='<%#Eval("mail")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="cell">
<ItemTemplate>
<%#Eval("cell") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditcell" runat="server" Text='<%#Eval("cell")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="address">
<ItemTemplate>
<%#Eval("address") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtditaddress" runat="server" Text='<%#Eval("address") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="street">
<ItemTemplate>
<%#Eval("street") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtditstreet" runat="server" Text='<%#Eval("street") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="#99FF99" />
</asp:GridView>
</table>
<asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" Width="75px" />
<asp:Button ID="btngrid" runat="server" Text="View" OnClick="btngrid_Click" Width="68px" CausesValidation="false" />
<asp:Button ID="btnclear" runat="server" Text="Clear" OnClick="btnclear_Click" CausesValidation="false" />
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Apx.cs code:
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;
namespace Registration_Page
{
public partial class NewRegistration : System.Web.UI.Page
{
//step 2
string conn = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridview();
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd =new SqlCommand("Up_MemberDetails",cn);
//if (txtname.Text == "" && txtpassword.Text == "" && txtmail.Text == "" && txtcell.Text == "")
//{
// lblmsg.Text = "Enter All Fields";
// return false;
//}
cmd.Parameters.AddWithValue("@name",txtname.Text);
cmd.Parameters.AddWithValue("@password", txtpassword.Text);
cmd.Parameters.AddWithValue("@mail", txtmail.Text);
cmd.Parameters.AddWithValue("@cell",txtcell.Text);
cmd.Parameters.AddWithValue("@address", txtaddress.Text);
cmd.Parameters.AddWithValue("@street", txtstreet.Text);
cmd.Parameters.AddWithValue("@Flag","I");
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
int i = cmd.ExecuteNonQuery();
cn.Close();
if (i == 1)
{
lblmsg.ForeColor = System.Drawing.Color.Green;
lblmsg.Text = " Inserted Successfull";
clear();
}
else
{
lblmsg.ForeColor = System.Drawing.Color.Red;
lblmsg.Text = " Not Inserted Successfull";
}
}
void clear()
{
txtname.Text = "";
txtpassword.Text = "";
txtmail.Text = "";
txtcell.Text = "";
txtaddress.Text = "";
txtstreet.Text = "";
}
void gridview()
{
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("Up_MemberDetails", cn);
cmd.Parameters.AddWithValue("@Flag", "V");
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "grid");
membergrid.DataSource = ds;
membergrid.DataBind();
}
protected void btngrid_Click(object sender, EventArgs e)
{
gridview();
}
protected void membergrid_RowEditing(object sender, GridViewEditEventArgs e)
{
membergrid.EditIndex = e.NewEditIndex;
gridview();
}
protected void membergrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = membergrid.Rows[e.RowIndex];
Label mid = (Label)row.FindControl("lblmid");
int mmid = int.Parse(mid.Text);
TextBox mname = (TextBox)row.FindControl("txteditname");
string mmname = mname.Text;
TextBox mmail = (TextBox)row.FindControl("txteditmail");
string mmmail = mmail.Text;
TextBox mcell = (TextBox)row.FindControl("txteditcell");
int mmcell =int.Parse(mcell.Text);
TextBox madders = (TextBox)row.FindControl("txtditaddress");
string mmaddress = madders.Text;
TextBox mstreet = (TextBox)row.FindControl("txtditstreet");
string mmstreet = mstreet.Text;
membergrid.EditIndex = -1;
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("Up_MemberDetails",cn);
cmd.Parameters.AddWithValue("@mid", mmid);
cmd.Parameters.AddWithValue("@name",mmname);
cmd.Parameters.AddWithValue("@mail",mmmail);
cmd.Parameters.AddWithValue("@cell", mmcell);
cmd.Parameters.AddWithValue("@address", mmaddress);
cmd.Parameters.AddWithValue("@street", mmstreet);
cmd.Parameters.AddWithValue("@flag","U");
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
int i = cmd.ExecuteNonQuery();
cn.Close();
if (i == 1)
{
lblmsg.Text = "Member Details updated";
lblmsg.ForeColor = System.Drawing.Color.Green;
}
else
{
lblmsg.Text = "Member Details Not updated";
lblmsg.ForeColor = System.Drawing.Color.Red;
}
}
protected void membergrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
membergrid.EditIndex = -1;
}
protected void txtcell0_TextChanged(object sender, EventArgs e)
{
}
public object mmaddress { get; set; }
protected void btnclear_Click(object sender, EventArgs e)
{
clear();
membergrid.Visible = false;
lblmsg.Text = "";
}
}
}
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;
namespace Registration_Page
{
public partial class NewRegistration : System.Web.UI.Page
{
//step 2
string conn = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridview();
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd =new SqlCommand("Up_MemberDetails",cn);
//if (txtname.Text == "" && txtpassword.Text == "" && txtmail.Text == "" && txtcell.Text == "")
//{
// lblmsg.Text = "Enter All Fields";
// return false;
//}
cmd.Parameters.AddWithValue("@name",txtname.Text);
cmd.Parameters.AddWithValue("@password", txtpassword.Text);
cmd.Parameters.AddWithValue("@mail", txtmail.Text);
cmd.Parameters.AddWithValue("@cell",txtcell.Text);
cmd.Parameters.AddWithValue("@address", txtaddress.Text);
cmd.Parameters.AddWithValue("@street", txtstreet.Text);
cmd.Parameters.AddWithValue("@Flag","I");
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
int i = cmd.ExecuteNonQuery();
cn.Close();
if (i == 1)
{
lblmsg.ForeColor = System.Drawing.Color.Green;
lblmsg.Text = " Inserted Successfull";
clear();
}
else
{
lblmsg.ForeColor = System.Drawing.Color.Red;
lblmsg.Text = " Not Inserted Successfull";
}
}
void clear()
{
txtname.Text = "";
txtpassword.Text = "";
txtmail.Text = "";
txtcell.Text = "";
txtaddress.Text = "";
txtstreet.Text = "";
}
void gridview()
{
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("Up_MemberDetails", cn);
cmd.Parameters.AddWithValue("@Flag", "V");
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "grid");
membergrid.DataSource = ds;
membergrid.DataBind();
}
protected void btngrid_Click(object sender, EventArgs e)
{
gridview();
}
protected void membergrid_RowEditing(object sender, GridViewEditEventArgs e)
{
membergrid.EditIndex = e.NewEditIndex;
gridview();
}
protected void membergrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = membergrid.Rows[e.RowIndex];
Label mid = (Label)row.FindControl("lblmid");
int mmid = int.Parse(mid.Text);
TextBox mname = (TextBox)row.FindControl("txteditname");
string mmname = mname.Text;
TextBox mmail = (TextBox)row.FindControl("txteditmail");
string mmmail = mmail.Text;
TextBox mcell = (TextBox)row.FindControl("txteditcell");
int mmcell =int.Parse(mcell.Text);
TextBox madders = (TextBox)row.FindControl("txtditaddress");
string mmaddress = madders.Text;
TextBox mstreet = (TextBox)row.FindControl("txtditstreet");
string mmstreet = mstreet.Text;
membergrid.EditIndex = -1;
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("Up_MemberDetails",cn);
cmd.Parameters.AddWithValue("@mid", mmid);
cmd.Parameters.AddWithValue("@name",mmname);
cmd.Parameters.AddWithValue("@mail",mmmail);
cmd.Parameters.AddWithValue("@cell", mmcell);
cmd.Parameters.AddWithValue("@address", mmaddress);
cmd.Parameters.AddWithValue("@street", mmstreet);
cmd.Parameters.AddWithValue("@flag","U");
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
int i = cmd.ExecuteNonQuery();
cn.Close();
if (i == 1)
{
lblmsg.Text = "Member Details updated";
lblmsg.ForeColor = System.Drawing.Color.Green;
}
else
{
lblmsg.Text = "Member Details Not updated";
lblmsg.ForeColor = System.Drawing.Color.Red;
}
}
protected void membergrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
membergrid.EditIndex = -1;
}
protected void txtcell0_TextChanged(object sender, EventArgs e)
{
}
public object mmaddress { get; set; }
protected void btnclear_Click(object sender, EventArgs e)
{
clear();
membergrid.Visible = false;
lblmsg.Text = "";
}
}
}
Database Tables and Procedures:
drop table member
Create table member(Mid int identity(1,1) primary key ,Name varchar(max),Password varchar(max),Mail Varchar(max),cell bigint,address varchar(max),street varchar(max))
drop proc Up_MemberDetails
Create proc Up_MemberDetails
@mid int=Null,
@name varchar(MAx)=Null,
@password varchar(max)=Null,
@mail varchar(max)=Null,
@cell bigint=Null,
@address varchar(max)=Null,
@street varchar(max)=Null,
@Flag varchar(max)=Null
As
Begin
IF(@Flag='I')
Begin
Insert into Member values(@name,@password,@mail,@cell,@address,@street)
End
IF(@Flag='U')
Begin
update member set name=@name,mail=@mail,cell=@cell,address=@address,street=@street where MID=@mid
End
IF(@Flag='V')
Begin
select MID,name,mail,cell ,address,street from member
End
End
Web.Config :
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<!--step 1-->
<connectionStrings>
<add name="con" connectionString="Data Source= DESKTOP-HEJKFCE; Integrated Security=true;Initial Catalog= Anil_Practice; uid=sa; Password=abc; " providerName="System.Data.SqlClient" />
</connectionStrings>
<!--end-->
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="none"></add>
</appSettings>
</configuration>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<!--step 1-->
<connectionStrings>
<add name="con" connectionString="Data Source= DESKTOP-HEJKFCE; Integrated Security=true;Initial Catalog= Anil_Practice; uid=sa; Password=abc; " providerName="System.Data.SqlClient" />
</connectionStrings>
<!--end-->
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="none"></add>
</appSettings>
</configuration>
No comments:
Post a Comment