Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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'.

C#
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");
   // string str = "Data Source=.;Initial Catalog=IMANET1;Integrated Security=True";
    // private SqlConnection con = 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("SELECT * FROM detail", conn);
            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();
            //GridView1.DataBind();
        }
        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();
        }
        

        }
}
Posted
Updated 19-Mar-13 8:03am
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-13 14:21pm    
In what line? I guess it's where you do the type case (TextBox). What makes you thinking that runtime type of a cell a text box?
—SA

1 solution

First of all, you always need to indicate the lines where an exception was thrown or propagated. Please see my comment; it should explain your bug.

Go to this line under the debugger and inspect the cell and the controls. Figure out why the runtime type of an actual control is not the type you expected. Fix the problem.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900