如何在表格的gridView中删除行 [英] How to delete a row in a gridView of a table

查看:86
本文介绍了如何在表格的gridView中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除表中的一行,但是它说错误的方式如下.
''/''应用程序中的服务器错误.
-------------------------------------------------- ------------------------------

你调用的对象是空的.
说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例.

源错误:


第74行:受保护的void EmployeeGridView_RowDelete(对象发送者,GridViewDeleteEventArgs e)
第75行:{
第76行:int empid = Convert.ToInt32((((TextBox)EmployeeGridView.Rows [e.RowIndex] .FindControl("txtempid")).Text);

在第76行中,这是在说错误.

请告诉我解决方案,纠正错误


我已经编写了删除代码,如下所示

I have tried to delete a row in a table.But it was saying error as in the following way.
Server Error in ''/'' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 74: protected void EmployeeGridView_RowDelete(object sender, GridViewDeleteEventArgs e)
Line 75: {
Line 76: int empid = Convert.ToInt32(((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtempid")).Text);

in line 76 ,it was saying that error.

Please tell me solution rectify the error


I have written code for deleting was as like below

protected void EmployeeGridView_RowDelete(object sender, GridViewDeleteEventArgs e)
       {
           int empid = Convert.ToInt32(((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtempid")).Text);
           string connectionstring = @" Data Source=01HW235982\SQLEXPRESS2008;Initial Catalog=adarsh;Persist Security Info=True;User ID=sa;Password=Pass@123";
           SqlConnection con = new SqlConnection(connectionstring);
           con.Open();
           SqlCommand cmd = new SqlCommand("DeleteEmployee", con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@emp_id ", empid);
           cmd.ExecuteNonQuery();
           FillGrid();
       }



请告诉我解决方案



Please tell me the solution

推荐答案

int empid = Convert.toInt32(EmployeeGridView.Rows [e.RowIndex] .Cells [CellNumber] .Text);
什么是错误...您的代码似乎还可以
int empid = Convert.toInt32(EmployeeGridView.Rows[e.RowIndex].Cells[CellNumber].Text);
what is the error... ur code seems ok


int empid = Convert.ToInt32(EmployeeGridView.DataKeys[e.RowIndex].Value.ToString());


这篇关于如何在表格的gridView中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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