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
stelle thomas
NA
19
4.1k
How do I delete and add new records in gridview in asp.net?
Mar 8 2016 4:10 AM
Please answer my question as soon as possible....I really need it for my project..this is my code for updating the records. Now what changes I should do in my code to delete and add new records??..I want to do changes in my code only...
Here is my exams.aspx page:-
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage2.master" AutoEventWireup="true" CodeFile="Exams.aspx.cs" Inherits="Exams" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
</div>
<div>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("EXAM_Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Title">
<ItemTemplate>
<asp:Label ID="lbl_title" runat="server" Text='<%#Eval("EXAM_TITLE") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_title" runat="server" Text='<%#Eval("EXAM_TITLE") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Date">
<ItemTemplate>
<asp:Label ID="lbl_date" runat="server" Text='<%#Eval("EXAM_DATE") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_date" runat="server" Text='<%#Eval("EXAM_DATE") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Time">
<ItemTemplate>
<asp:Label ID="lbl_time" runat="server" Text='<%#Eval("TOTAL_TIME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_time" runat="server" Text='<%#Eval("TOTAL_TIME") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Paper">
<ItemTemplate>
<asp:Label ID="lbl_paper" runat="server" Text='<%#Eval("GENERATED_EXAM_PAPER") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_paper" runat="server" Text='<%#Eval("GENERATED_EXAM_PAPER") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>
<RowStyle BackColor="#e7ceb6"/>
</asp:GridView>
</div>
<div>
<asp:label runat="server" ID="label12"></asp:label>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
</asp:Content>
HERE IS MY Exams.aspx.cs page :-
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.Configuration;
using System.Data;
public partial class Exams : System.Web.UI.Page
{
//Connection String from web.config File
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con;
SqlDataAdapter adapt;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
ShowData();
}
}catch(Exception ex)
{
label12.Text = ex.Message;
}
}
//ShowData method for Displaying Data in Gridview
protected void ShowData()
{
dt = new DataTable();
con = new SqlConnection(cs);
con.Open();
adapt = new SqlDataAdapter("Select EXAM_Id,EXAM_TITLE,EXAM_DATE,TOTAL_TIME,GENERATED_EXAM_PAPER from EXAM_PAPER", con);
adapt.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
//NewEditIndex property used to determine the index of the row being edited.
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
//Finding the controls from Gridview for the row which is going to update
Label id = GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
TextBox title = GridView1.Rows[e.RowIndex].FindControl("txt_title") as TextBox;
TextBox date = GridView1.Rows[e.RowIndex].FindControl("txt_date") as TextBox;
TextBox time = GridView1.Rows[e.RowIndex].FindControl("txt_time") as TextBox;
TextBox paper = GridView1.Rows[e.RowIndex].FindControl("txt_paper") as TextBox;
con = new SqlConnection(cs);
con.Open();
//updating the record
SqlCommand cmd = new SqlCommand("Update EXAM_PAPER set EXAM_TITLE='" + title.Text + "',EXAM_DATE='" + date.Text + "',TOTAL_TIME='" + time.Text + "',GENERATED_EXAM_PAPER='" + paper.Text + "' where EXAM_Id=" + Convert.ToInt32(id.Text), con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
ShowData();
}
protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
ShowData();
}
}
Reply
Answers (
2
)
How to print directly in Asp.net MVC
how to print the history details in c#