CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

datagrid编辑是出现“指定的转换无效”的错误

楼主jasonlee0927(一颗心忘了收...)2005-06-01 18:37:43 在 .NET技术 / ASP.NET 提问

在点datagrid的编辑的时候出现“指定的转换无效”的错误:Dim   projectid   As   String   =   CType(e.Item.Cells(3).Controls(0),   TextBox).Text  
  请各位帮帮忙,急啊!!!!!!!!!!!!!!  
   
  --------------源码------  
  Sub   dgResult_UpdateCommand(ByVal   sender   As   Object,   ByVal   e   As   DataGridCommandEventArgs)   Handles   dgResult.UpdateCommand  
                  Dim   intID   As   Integer   =   dgResult.DataKeys(e.Item.ItemIndex)  
                  Dim   projectid   As   String   =   CType(e.Item.Cells(3).Controls(0),   TextBox).Text  
   
                  Dim   myCommand   As   SqlCommand  
   
                  strSql   =   "UPDATE   Project   Set   projectid=@projectid   WHERE   id="   &   intID   &   ""  
   
                  myCommand.Parameters.Add(New   SqlParameter("@projectid",   SqlDbType.VarChar,   10))  
                  myCommand.Parameters("@projectid").Value   =   projectid  
   
                  myCommand   =   New   SqlCommand(strSql,   myConnection)  
                  myConnection.Open()  
                  Try  
                          myCommand.ExecuteNonQuery()  
                          dgResult.EditItemIndex   =   -1  
                  Catch   ex   As   SqlException  
                          If   ex.Number   =   2627   Then  
                                  lblErrorMessage.Visible   =   True  
                                  lblErrorMessage.Text   =   "数据库中已经存在相同的项目标识!"  
                          Else  
                                  lblErrorMessage.Visible   =   True  
                                  lblErrorMessage.Text   =   "不能更新记录!"  
                          End   If  
                  End   Try  
                  myConnection.Close()  
                  BindData()  
   
  问题点数:20、回复次数:9Top

1 楼zhangzs8896(小二)回复于 2005-06-01 18:50:16 得分 5

试试  
  Dim   projectid   As   String   =   (CType(e.Item.Cells(3).Controls(0),   TextBox)).Text  
  Top

2 楼jasonlee0927(一颗心忘了收...)回复于 2005-06-02 08:59:42 得分 0

还是一样的错误啊!!!!  
  -------------------------------  
  试试  
  Dim   projectid   As   String   =   (CType(e.Item.Cells(3).Controls(0),   TextBox)).Text  
  Top

3 楼ghchen()回复于 2005-06-02 09:08:37 得分 5

你的dataGrid的Item.Cells(3).Controls(0不是TextBoxTop

4 楼sukaru(逍遥子)回复于 2005-06-02 09:28:10 得分 0

你的textbox中必须输入的都是数字才能这样转换的。Top

5 楼renyu732(Sysinfo)回复于 2005-06-02 09:34:15 得分 5

这样做试试:  
  将Dim   projectid   As   String   =   CType(e.Item.Cells(3).Controls(0),   TextBox).Text  
  改为  
  Dim   projectText   as   TextBox=Ctype(e.Item.Cells(3).Controls(0),TextBox)  
  然后  
  Dim   Projectid   as   String=projectText.TextTop

6 楼xzq686(★_瞬_★)回复于 2005-06-02 09:36:26 得分 0

e.Item.Cells(3).Controls(0)是什么控件??  
  如果确实是TextBox控件。你再用:  
  CType(e.Item.FindControl("控件ID"),TextBox)  
  Top

7 楼renyu732(Sysinfo)回复于 2005-06-02 09:36:53 得分 5

如果还不成,那应该就是  
  e.Item.Cells(3).Controls(0)  
  的问题了.  
   
  你可以将这个输入看看是什么...Top

8 楼jasonlee0927(一颗心忘了收...)回复于 2005-06-02 10:31:54 得分 0

都还是不行啊!!!!  
  html:  
  <asp:TemplateColumn   HeaderText="项目标识">  
  <EditItemTemplate>  
  <asp:TextBox   ID="txtProjectid"   Runat="server"   Text='<%#DataBinder.Eval(Container.Dataitem,"projectid")%>'>  
  </asp:TextBox>  
  </EditItemTemplate>  
  </asp:TemplateColumn>Top

9 楼jasonlee0927(一颗心忘了收...)回复于 2005-06-02 13:23:02 得分 0

已经搞定了。  
  Dim   projectid   As   String   =   CType(e.Item.Cells(3).Controls(0),   TextBox).Text  
  应该改成:  
  Dim   projectid   As   String   =   CType(e.Item.Cells(3).Controls(1),   TextBox).Text  
  就ok了。  
  还是谢谢各位的帮忙!!!!^_^Top

相关问题

  • 指定的转换无效
  • 无法更新 DataGrid中数据?指定的转换无效,
  • datagrid指定的转换无效啊???郁闷啊
  • datagrid 编辑时,出现指定的转换无效!问题?
  • 编写DataGrid自定义控件时出现“指定的转换无效”
  • "指定的转换无效"怎么办?
  • DataLish发生指定的转换无效
  • 指定的转换无效。求助
  • 请教:System.InvalidCastException: 指定的转换无效。
  • 请问,为什么用DataGrid在修改数据时提示“指定转换无效”?

关键词

  • 转换
  • 控件
  • projectid
  • dgresult
  • lblerrormessage
  • ctype
  • textbox
  • cells
  • mycommand
  • dim

得分解答快速导航

  • 帖主:jasonlee0927
  • zhangzs8896
  • ghchen
  • renyu732
  • renyu732

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo