特急,CSDN上。NET高手呢?
点击选择,可以选择gridview的相应要行(如第二条),点Button删除,也可以删除gridview的相应要行(第二条)。但是gridview会自动跳到下一条(第三条),并选择上了相应的行(第三条),如果点击删除,也可以删除。但是如果不点删除,点击选择(第三条)就会出错。
提示:
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息: ystem.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 40: CompanyTel.Text = this.GridView1.SelectedRow.Cells[7].Text.ToString();
行 41: LinkMan.Text = this.GridView1.SelectedRow.Cells[8].Text.ToString();
行 42: UserName.Text = GridView1.SelectedDataKey[ "UserName "].ToString();
行 43: AddTime.Text = GridView1.SelectedDataKey[ "AddTime "].ToString();
行 44: Memory.Text = GridView1.SelectedDataKey[ "Memory "].ToString();
源文件: d:\xxsbwh\base_system\Company_add_Mange.aspx.cs 行: 42
注明一下: 我的gridview中UserName,AddTime,Memory。为隐藏列。并加入到了datakeyname中了。请高手赐教!,谢谢
下面附上源代码。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class base_system_Company_add_Mange : System.Web.UI.Page
{
//绑定操作
public void bind()
{
SqlConnection strcon = new SqlConnection (System.Configuration.ConfigurationManager.AppSettings[ "strcon "]);
strcon.Open();
SqlDataAdapter sda = new SqlDataAdapter( "select * from tb_Commpany order by id desc ", strcon);
DataSet ds = new DataSet();
sda.Fill(ds, "tb_Commpany ");
// GridView1.DataSource = ds.Tables[ "tb_Commpany "];
GridView1.DataKeyNames = new string[] { "id " };
GridView1.DataBind();
}
//选择操作
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (GridView1.SelectedIndex!=-1)
{
keyid.Text=this.GridView1.SelectedRow.Cells[2].Text.ToString();
CompanyName.Text = this.GridView1.SelectedRow.Cells[3].Text.ToString();
CompanyType.Text = this.GridView1.SelectedRow.Cells[4].Text.ToString();
CompanyShort.Text = this.GridView1.SelectedRow.Cells[5].Text.ToString();
CompanyAddress.Text = this.GridView1.SelectedRow.Cells[6].Text.ToString();
CompanyTel.Text = this.GridView1.SelectedRow.Cells[7].Text.ToString();
LinkMan.Text = this.GridView1.SelectedRow.Cells[8].Text.ToString();
UserName.Text = GridView1.SelectedDataKey[ "UserName "].ToString();
AddTime.Text = GridView1.SelectedDataKey[ "AddTime "].ToString();
Memory.Text = GridView1.SelectedDataKey[ "Memory "].ToString();
}
}
//对GridView1进行限字符操作
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if((e.Row.Cells[3].Text).Length> 6)
{
e.Row.Cells[3].Text = (e.Row.Cells[3].Text).Substring(0,3)+ "… ";
}
if ((e.Row.Cells[6].Text).Length > 6)
{
e.Row.Cells[6].Text = (e.Row.Cells[6].Text).Substring(0, 3) + "… ";
}
if ((e.Row.Cells[5].Text).Length > 5)
{
e.Row.Cells[5].Text = (e.Row.Cells[5].Text).Substring(0, 3) + "… ";
}
}
}
//重置操作
protected void Button2_Click(object sender, EventArgs e)
{
keyid.Text = " ";
CompanyName.Text = " ";
CompanyType.Text = " ";
CompanyShort.Text = " ";
CompanyAddress.Text = " ";
CompanyTel.Text = " ";
LinkMan.Text = " ";
UserName.Text = " ";
AddTime.Text = " ";
Memory.Text = " ";
}
//删除操作
protected void Button4_Click(object sender, EventArgs e)
{
if (GridView1.SelectedIndex!=-1)
{
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings[ "strcon "]);
strcon.Open();
string id = GridView1.SelectedDataKey[ "id "].ToString();
SqlCommand scd = new SqlCommand( "delete from tb_Commpany where id= " + id, strcon);
scd.ExecuteNonQuery();
this.bind();
keyid.Text = " ";
CompanyName.Text = " ";
CompanyType.Text = " ";
CompanyShort.Text = " ";
CompanyAddress.Text = " ";
CompanyTel.Text = " ";
LinkMan.Text = " ";
UserName.Text = " ";
AddTime.Text = " ";
Memory.Text = " ";
}
}
//新增操作
protected void Button1_Click(object sender, EventArgs e)
{
if (IsValid == true)
{
string CompanyName = this.CompanyName.Text.ToString();
string keyid = this.keyid.Text.ToString();
string CompanyType = this.CompanyType.Text.ToString();
string CompanyShort = this.CompanyShort.Text.ToString();
string CompanyAddress = this.CompanyAddress.Text.ToString();
string CompanyTel = this.CompanyTel.Text.ToString();
string LinkMan = this.LinkMan.Text.ToString();
string UserName = Convert.ToString(Session[ "username "]);
string AddTime = Convert.ToString(DateTime.Today.ToLongDateString());
string Memory = this.Memory.Text.ToString();
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings[ "strcon "]);
strcon.Open();
SqlCommand cmd = new SqlCommand( "insert into tb_Commpany (keyid,CompanyName,CompanyType,CompanyShort,CompanyAddress,CompanyTel,LinkMan,UserName,AddTime,Memory)values( ' " + keyid + " ', ' " + CompanyName + " ', ' " + CompanyType + " ', ' " + CompanyShort + " ', ' " + CompanyAddress + " ', ' " + CompanyTel + " ', ' " + LinkMan + " ', ' " + UserName + " ', ' " + AddTime + " ', ' " + Memory + " ') ", strcon);
cmd.ExecuteNonQuery();
strcon.Close();
Response.Write( " <script language=javascript> alert( '祝贺你,你己经成功添加了一条记录 ');location= 'Company_add_Mange.aspx ' </script> ");
this.bind();
}
}
}
[解决办法]
确定用的名称都正确?
[解决办法]
点Button删除,也可以删除gridview的相应要行(第二条)。但是gridview会自动跳到下一条(第三条),并选择上了相应的行(第三条)
你的selectedIndex被设成了1,第二行被你删掉了,当然就把第三行选上了
正确的做法:
再delete事件里设置selectedIndex = -1
[解决办法]
如果在delete事件里设置selectedIndex = -1
gridview就不自动跳到下一条(第三条),但点击第三条仍一样的出错提示。
最起码解决了一个问题了.
你可以测试一下,第一次选择了一行,然后在选择另一行是否出错?
先选择一行,然后删除他,在选另一行(不一定是删除这行的下一行),是否出错?
不选择,能删除么?如果能,不选择,直接删除一行,然后选择另一行,是否出错?
出错的代码是那一段?
回答上述问题,给你解决方案。
[解决办法]
UserName.Text = GridView1.SelectedDataKey[ "UserName "].ToString();
删除后,界面显示第三行是一种显示效果,后边的SELECTED可能不是第三行,你临视一下你第二次点删除后,GridView1.SelectedDataKey[ "UserName "]的值就知道了,在删前和删后分别影视
[解决办法]
再delete事件里设置selectedIndex = -1