0% found this document useful (0 votes)
93 views4 pages

ASP Dot Codes

This document contains code for performing CRUD (create, read, update, delete) operations on a hotel database table using a GridView control in ASP.NET. It includes code to: 1) Connect to a SQL Server database and load data from the hotel table into the GridView for viewing. 2) Add code to allow deleting rows from the GridView by getting the hidden field value and passing it to a DELETE query. 3) Add code to enable editing rows by setting events, converting labels to textboxes, and updating the database with new values on update.

Uploaded by

Manish Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views4 pages

ASP Dot Codes

This document contains code for performing CRUD (create, read, update, delete) operations on a hotel database table using a GridView control in ASP.NET. It includes code to: 1) Connect to a SQL Server database and load data from the hotel table into the GridView for viewing. 2) Add code to allow deleting rows from the GridView by getting the hidden field value and passing it to a DELETE query. 3) Add code to enable editing rows by setting events, converting labels to textboxes, and updating the database with new values on update.

Uploaded by

Manish Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

To show Data through grid view

// here connection string is made gobal


// “Datadirectory” is so that this databse can be used from any drives in the
system

public partial class edithotel : System.Web.UI.Page


{
private static string strconn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|
Datadirectory|\tourism.mdf;Integrated Security=True;User Instance=True";

protected void Page_Load(object sender, EventArgs e)


{
if (Page.IsPostBack == false)
{
BindData();
}

}
protected void BindData()
{
SqlConnection conn = new SqlConnection(strconn);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select * from hotel";
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
conn.Close();
}

----------------------Row deleting code through grid view----------------------


In this code “hdnhotelid” is the id of hidden field that we have used in our project

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs


e)
{
string hdnhotel =
((HiddenField)GridView1.Rows[e.RowIndex].Cells[0].FindControl("hdnhotelid"))
.Value;
SqlConnection conn = new SqlConnection(strconn);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "delete from hotel where Hotel_id='" + hdnhotel +
"'";
conn.Open();
int rowcount = 0;
rowcount = comm.ExecuteNonQuery();
if (rowcount > 0)
{
BindData();
}
conn.Close();
}
Row updating code

For row updating set three event to true

 RowEditing
 RowCancelingEdit
 RowUpdating

To do editing convet all label to text boxes for the row that we want to allow edit

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)


{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs


e)
{
GridView1.EditIndex = -1;
BindData();

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)


{
string hdhotel =
((HiddenField)GridView1.Rows[e.RowIndex].Cells[0].FindControl("hdnhotelid1")).Value;
string hotelname =
((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txthotelname1")).Text;
string city =
((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txtcity1")).Text;
string rating =
((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txtrating1")).Text;
string price =
((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txtprice1")).Text;

SqlConnection conn = new SqlConnection(strconn);


SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "update hotel set Hotel_name='" + hotelname.Trim() + "',City='" +
city.Trim() + "',Rating='" + rating.Trim() + "',Price_range='" + price.Trim() + "'where
Hotel_id='" + hdhotel.Trim() + "'";
conn.Open();
int rowcount = 0;
rowcount = comm.ExecuteNonQuery();
if (rowcount > 0)
{
BindData();
}

conn.Close();
GridView1.EditIndex = -1;
BindData();

You might also like