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" %>
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
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();
}
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit KumarPosted Mar 8, 2016, 5:55 AM
Chandu KumawatPosted Mar 8, 2016, 5:27 AM