我在gridview编辑中有错误 [英] Am having error in gridview edit

查看:59
本文介绍了我在gridview编辑中有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您对此编辑网格视图有问题,问题是它没有编辑但删除 工作正常。

这是错误,

无法将'System.Web.UI.WebControls.DataControlLinkBut​​ton'类型的对象强制转换为''System.Web.UI.WebControls.TextBox''。



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");
   // 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();
        }
        

        }
}

推荐答案

首先,您始终需要指出抛出或传播异常的行。请看我的评论;它应该解释你的错误。



转到调试器下的这一行并检查单元格和控件。弄清楚为什么实际控件的运行时类型不是您期望的类型。解决问题。



-SA
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


这篇关于我在gridview编辑中有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆