hi am having problem with this edit gridview, the problem is that it doesn't edit but deleting
is working very fine.
This is the error,
Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.Web.UI.HtmlControls;
public partial class Hotet_Edit : System.Web.UI.Page
{
private SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=IMANET1;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvbind();
}
}
protected void gvbind()
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from HotelBooking", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
conn.Open();
SqlCommand cmd = new SqlCommand("delete FROM HotelBooking where CustomerID='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int CustomerID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
TextBox txtCustomerID = (TextBox)row.Cells[0].Controls[0];
TextBox txtRoomNo = (TextBox)row.Cells[0].Controls[0];
TextBox txtRoomCategory = (TextBox)row.Cells[1].Controls[0];
TextBox txtTimeCheckedIn = (TextBox)row.Cells[2].Controls[0];
TextBox txtDay = (TextBox)row.Cells[3].Controls[0];
TextBox txtMonth = (TextBox)row.Cells[4].Controls[0];
TextBox txtYear = (TextBox)row.Cells[5].Controls[0];
TextBox txtName = (TextBox)row.Cells[6].Controls[0];
TextBox txtGender = (TextBox)row.Cells[7].Controls[0];
TextBox txtLocation = (TextBox)row.Cells[8].Controls[0];
TextBox txtPhone = (TextBox)row.Cells[9].Controls[0];
TextBox txtDailyBookingCost = (TextBox)row.Cells[10].Controls[0];
TextBox txtDaysBooked = (TextBox)row.Cells[11].Controls[0];
TextBox txtRecordedBy = (TextBox)row.Cells[12].Controls[0];
TextBox txtRoomOccupied = (TextBox)row.Cells[13].Controls[0];
TextBox txtTotal = (TextBox)row.Cells[14].Controls[0];
GridView1.EditIndex = -1;
conn.Open();
SqlCommand cmd = new SqlCommand("update HotelBooking set CustomerID='" + txtCustomerID +"', RoomNo='" + txtRoomNo.Text + "',RoomCategory='" + txtRoomCategory.Text + "', TimeCheckedIn='" + txtTimeCheckedIn.Text + "', Day='" + txtDay.Text + "', Month='" + txtMonth.Text +"', Year='" + txtYear.Text + "', Name='" + txtName.Text + "', Gender='" + txtGender.Text + "', Location='" + txtLocation.Text + "', Phone='" + txtPhone.Text + "',DailyBooking='" + txtDailyBookingCost.Text +"', DaysBooked='" + txtDaysBooked.Text +"',RecordedBy='" + txtRecordedBy.Text +"', RoomOccupied='" + txtRoomOccupied.Text + "', Total='" + txtTotal.Text + " where CustomerID='" + CustomerID + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gvbind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
gvbind();
}
}
}