SAIRAJ MORE

SAIRAJ MORE

  • NA
  • 12
  • 2.6k

i can not edit update delete in gridview in asp.net c#

Oct 7 2016 6:38 AM
i can not edit update delete in gridview in asp.net  
data is not update and delete for my table so please help me
 
 
cs page
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class thakyou : System.Web.UI.Page
{
string connString = ConfigurationManager.ConnectionStrings["SairajDBConString"].ToString().Trim();
public object UserName { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["SairajDBConString"].ToString().Trim();
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("Select [UserName],[Emailid],[Password],[Birthday],[MobileNo],[Location]from [sairaj];", con);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable sairaj = new DataTable();
sda.Fill(sairaj);
gridview1.DataSource = sairaj;
gridview1.DataBind();
}
catch (Exception)
{
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Registrationpage.aspx");
}
protected void btndel_Click1(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["SairajDBConString"].ToString().Trim();
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("delete sairaj where UserName=@username,Emailid=@emailid,Password=@password,Birthday=@birthday,Location=@location", con);
try
{
con.Open();
cmd.Parameters.AddWithValue("@username", UserName);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception)
{
}
}
protected void gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridview1.EditIndex = -1;
gridview1.DataSource = sairaj;
gridview1.DataBind();
}
protected void gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)gridview1.Rows[e.RowIndex];
Label lbldeleteID = (Label)row.FindControl("lblid");
}
protected void gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{
gridview1.EditIndex = e.NewEditIndex;
DataBind();
}
protected void gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)Session["sairaj"];
GridViewRow row = gridview1.Rows[e.RowIndex];
string Username = (gridview1.Rows[e.RowIndex].Cells[2]).Text;
string Emailid = (gridview1.Rows[e.RowIndex].Cells[3]).Text;
string Password = (gridview1.Rows[e.RowIndex].Cells[4]).Text;
string Birthday = (gridview1.Rows[e.RowIndex].Cells[5]).Text;
string Mobileno = (gridview1.Rows[e.RowIndex].Cells[6]).Text;
string Location = (gridview1.Rows[e.RowIndex].Cells[7]).Text;
gridview1.EditIndex = -1;
DataBind();
}
private void Deletesairaj(int sairaj)
{
string connString = ConfigurationManager.ConnectionStrings["SairajDBConString"].ToString().Trim();
SqlConnection con = new SqlConnection(connString);
{
string query = "DELETE FROM sairaj WHERE sairaj=" + sairaj + "";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.Parameters.AddWithValue("@username", UserName);
cmd.ExecuteNonQuery();
con.Close();
}
}
private void Updatesairaj(int sairaj, string Username, string Emailid, string Password, string Birthday, string MobileNo, string Location)
{
string connString = ConfigurationManager.ConnectionStrings["SairajDBConString"].ToString().Trim();
SqlConnection con = new SqlConnection(connString);
{
string query = "UPDATE sairaj SET UserName='" + Username + "', Emailid='" + Emailid + "', Passwprd='" + Password + "', Birthday='" + Birthday + "', MobileNo='" + MobileNo + "', Location='" + Location + " WHERE sairaj=" + sairaj + "";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.Parameters.AddWithValue("@username", UserName);
cmd.ExecuteNonQuery();
con.Close();
}
}
public object sairaj { get; set; }
}
aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="thankyou.aspx.cs" Inherits="thakyou" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 501px;
}
.style4
{
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="2">
&nbsp;<asp:Image ID="Image1" runat="server" Height="255px"
ImageUrl="~/thankyou-for-registration-confirmation-temp-eng.png"
Width="759px" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btndi" runat="server" Height="25px" OnClick="Button3_Click"
Text="Display all Detalis" Width="119px" style="text-align: center" />
</td>
<td>
<asp:Button ID="txtre" runat="server" Height="25px" OnClick="Button2_Click"
Text="Registration page" style="margin-left: 0px" />
</td>
</tr>
</table>
</div>
<table class="style1">
<tr>
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" AutoGenerateEditButton="True"
AutoGenerateDelteButton="true" Height="441px"
Width="764px" OnRowCancelingEdit="gridview1_RowCancelingEdit"
OnRowDeleting="gridview1_RowDeleting" OnRowEditing="gridview1_RowEditing"
OnRowUpdating="gridview1_RowUpdating">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
<asp:BoundField DataField="Emailid" HeaderText="Emailid" SortExpression="Emailid" />
<asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
<asp:BoundField DataField="Birthday" HeaderText="Birthday" SortExpression="Birthday" />
<asp:BoundField DataField="MobileNo" HeaderText="MobileNo" SortExpression="MobileNo" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:CommandField HeaderText="Opration" ShowDeleteButton="True"
ShowEditButton="True" ShowHeader="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<td>
&nbsp;
</td>
</tr>
</table>
<table class="style1">
<tr>
<td class="style4">
<asp:GridView ID="GridViewNew" runat="server" AutoGenerateColumns="False"
DataKeyNames="sairaj" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:TemplateField HeaderText="UserName">
<EditItemTemplate>
<asp:TextBox ID="TextBoxUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emailid">
<EditItemTemplate>
<asp:TextBox ID="TextBoxEmailid" runat="server" Text='<%# Eval("Emailid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Emailid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<EditItemTemplate>
<asp:TextBox ID="TextBoxPassword" runat="server" Text='<%# Eval("Password") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Password") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Birthday">
<EditItemTemplate>
<asp:TextBox ID="TextBoxBirthday" runat="server" Text='<%# Eval("Birthday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Birthday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MobileNo">
<EditItemTemplate>
<asp:TextBox ID="TextBoxMobileNo" runat="server" Text='<%# Eval("MobileNo") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("MobileNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<EditItemTemplate>
<asp:TextBox ID="TextBoxLocation" runat="server" Text='<%# Eval("Location") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Location") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Operation" ShowDeleteButton="True"
ShowEditButton="True" ShowHeader="True" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" Horizontal />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
Sql data
SELECT TOP 1000 [UserName]
,[Emailid]
,[Password]
,[Birthday]
,[MobileNo]
,[Location]
FROM [sairaj].[dbo].[sairaj]
 

Answers (7)