TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Rohit ßachhav-Patil
NA
3
680
Will it work as crud operation?
Mar 22 2018 12:52 PM
same page
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="same.aspx.cs" Inherits="same" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="hfid" runat="server" />
<div>
<table align="center">
<tr>
<th>First Name</th>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<th>Last Name</th>
<td>
<asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<th>Designation</th>
<td>
<asp:TextBox ID="txtdesignation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<th>Salary</th>
<td>
<asp:TextBox ID="txtsal" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<th>Date Of Birth</th>
<td>
<asp:TextBox ID="txtdob" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<th>Gender</th>
<td>
<asp:RadioButton ID="rbmael" runat="server" Text="Male" GroupName="Gender"/>
</td>
<td>
<asp:RadioButton ID="rbfemale" runat="server" Text="Female" GroupName="Gender" />
</td>
</tr>
<tr>
<th>Country</th>
<td>
<asp:DropDownList ID="ddlcountry" runat="server" OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</td>
</tr>
<tr>
<th>State</th>
<td>
<asp:DropDownList ID="ddlstate" runat="server" OnSelectedIndexChanged="ddlstate_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</td>
</tr>
<tr>
<th>City</th>
<td>
<asp:DropDownList ID="ddlcity" runat="server" AutoPostBack="true"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" />
</td>
<td>
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblsuccess" runat="server" Text="" ForeColor="Green"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblfail" runat="server" Text="" ForeColor="red"></asp:Label>
</td>
</tr>
</table>
</div>
<div align="center">
<asp:GridView ID="gd1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblf" runat="server" Text='<%# Bind("fname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lbll" runat="server" Text='<%# Bind("lname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lbld" runat="server" Text='<%# Bind("designation") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:Label ID="lbls" runat="server" Text='<%# Bind("salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of BIrth">
<ItemTemplate>
<asp:Label ID="lbldo" runat="server" Text='<%# Bind("dob") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="lblg" runat="server" Text='<%# Bind("gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblc" runat="server" Text='<%# Bind("cname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="lblst" runat="server" Text='<%# Bind("sname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblcu" runat="server" Text='<%# Bind("cname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbledit" runat="server" CommandArgument='<%# Bind("eid") %>' OnClick="lbledit_Click">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbldelete" runat="server" CommandArgument='<%# Bind("eid") %>' OnClick="lbldelete_Click">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public partial class same : System.Web.UI.Page
{
SqlCommand cmd = null;
SqlConnection cn = null;
SqlDataAdapter da = null;
DataSet ds = null;
protected void Page_Load(object sender, EventArgs e)
{
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlCon"].ConnectionString);
if(!Page.IsPostBack)
{
BindCountry();
}
BindGrid();
}
public void BindCountry()
{
cmd = new SqlCommand("findcountry", cn);
cmd.CommandType = CommandType.StoredProcedure;
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
ddlcountry.DataSource = ds;
ddlcountry.DataTextField = "cname";
ddlcountry.DataValueField = "ccid";
ddlcountry.DataBind();
}
public void BindState()
{
cmd = new SqlCommand("findstate", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ccid", SqlDbType.Int).Value = ddlcountry.SelectedValue;
da = new SqlDataAdapter(cmd);
ds=new DataSet();
da.Fill(ds);
ddlstate.DataSource = ds;
ddlstate.DataTextField = "sname";
ddlstate.DataValueField = "sid";
ddlstate.DataBind();
}
public void BindCity()
{
cmd = new SqlCommand("findcity", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@sid", SqlDbType.Int).Value = ddlstate.SelectedValue;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
ddlcity.DataSource = ds;
ddlcity.DataTextField = "cname";
ddlcity.DataValueField = "cid";
ddlcity.DataBind();
}
protected void btnsave_Click(object sender, EventArgs e)
{
cn.Open();
cmd = new SqlCommand("insertorupdate", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid", (hfid.Value == "" ? 0 : Convert.ToInt32(hfid.Value)));
cmd.Parameters.AddWithValue("@fname", txtfname.Text.Trim());
cmd.Parameters.AddWithValue("@lname", txtlname.Text.Trim());
cmd.Parameters.AddWithValue("@designation", txtdesignation.Text.Trim());
cmd.Parameters.AddWithValue("@salary", txtsal.Text.Trim());
cmd.Parameters.AddWithValue("@dob", txtdob.Text.Trim());
string Gender = null;
if(rbmael.Checked)
{
Gender="Male";
}
else
{
Gender="Female";
}
cmd.Parameters.AddWithValue("@gender", Gender);
cmd.Parameters.AddWithValue("@cid",ddlcity.SelectedItem.Value);
cmd.ExecuteNonQuery();
if(hfid.Value=="")
{
lblsuccess.Text = "Save Successfully";
}
else
{
lblsuccess.Text = "Update Successfully";
}
Clear();
BindGrid();
}
protected void btnClear_Click(object sender, EventArgs e)
{
Clear();
}
public void Clear()
{
hfid.Value = "";
txtlname.Text = txtfname.Text = txtsal.Text = txtdob.Text = txtdesignation.Text = string.Empty;
}
protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
BindCity();
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
BindState();
}
protected void lbledit_Click(object sender, EventArgs e)
{
int eid = Convert.ToInt32((sender as LinkButton).CommandArgument);
cn.Open();
da = new SqlDataAdapter("viewalldataid", cn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@eid", eid);
DataTable dt = new DataTable();
da.Fill(dt);
cn.Close();
hfid.Value = eid.ToString();
txtfname.Text = dt.Rows[0]["fname"].ToString();
txtlname.Text = dt.Rows[0]["lname"].ToString();
txtdesignation.Text = dt.Rows[0]["designation"].ToString();
txtdob.Text = dt.Rows[0]["dob"].ToString();
txtsal.Text = dt.Rows[0]["salary"].ToString();
btnsave.Text = "Update";
}
public void BindGrid()
{
cmd = new SqlCommand("viewalldata", cn);
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
gd1.DataSource = ds;
gd1.DataBind();
}
protected void lbldelete_Click(object sender, EventArgs e)
{
cn.Open();
cmd = new SqlCommand("deleterecord", cn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid", Convert.ToInt32(hfid.Value));
cmd.ExecuteNonQuery();
cn.Close();
Clear();
}
}
Diifernt pages
same .aspx
.aspx.cs
protected void lbkedit_Click(object sender, EventArgs e)
{
int eid = Convert.ToInt32((sender as LinkButton).CommandArgument);
Session["session_emp"] = eid;
Response.Redirect("diff1.aspx");
}
second page design is same only coding is diifferent
protected void Page_Load(object sender, EventArgs e)
{
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlCon"].ConnectionString);
if(!Page.IsPostBack)
{
bindcountry();
}
cn.Open();
da = new SqlDataAdapter("viewalldataid", cn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@eid", Session["session_emp"]);
DataTable dt = new DataTable();
da.Fill(dt);
cn.Close();
hfid.Value = Session["session_emp"].ToString();
txtfname.Text = dt.Rows[0]["fname"].ToString();
txtlname.Text = dt.Rows[0]["lname"].ToString();
txtdesign.Text = dt.Rows[0]["designation"].ToString();
txtsal.Text = dt.Rows[0]["salary"].ToString();
txtdob.Text = dt.Rows[0]["dob"].ToString();
}
protected void btnupdate_Click(object sender, EventArgs e)
{
cn.Open();
cmd = new SqlCommand("insertorupdate", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid",(hfid.Value==""?0:Convert.ToInt32(hfid.Value)));
cmd.Parameters.AddWithValue("@fname",txtfname.Text.Trim());
cmd.Parameters.AddWithValue("@lname",txtlname.Text.Trim());
cmd.Parameters.AddWithValue("@designation",txtdesign.Text.Trim());
cmd.Parameters.AddWithValue("@salary",txtsal.Text.Trim());
cmd.Parameters.AddWithValue("@dob",txtdob.Text.Trim());
cmd.Parameters.AddWithValue("@cid", ddlcity.SelectedItem.Value);
lblsuccess.Text="Update Successfully....";
}
Stord Procedures
i
Reply
Answers (
1
)
Entity Framework one to many relation not updated
Gridview CurrRowChanged Event