救命啊,DATAGRID中更新数据出现“指定控件转换无效”

snowshaken 2008-07-16 03:47:11
哪位大侠给指导一下,谢谢了!
代码:


Private Sub ResourceInList_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles ResourceInList.UpdateCommand
Dim shebeibianhao, shebeimingcheng, shebeiguige, lingyongbumen, lingyongren, lingyongriqi, bdbh As TextBox
bdbh = e.Item.Cells(0).Controls(0)
shebeibianhao = e.Item.Cells(1).Controls(0)
shebeimingcheng = e.Item.Cells(2).Controls(0)
shebeiguige = e.Item.Cells(3).Controls(0)
lingyongbumen = e.Item.Cells(4).Controls(0)
lingyongren = e.Item.Cells(5).Controls(0)
lingyongriqi = e.Item.Cells(6).Controls(0)

Dim myConn As New SqlClient.SqlConnection
Dim myCmd As New SqlClient.SqlCommand
Dim myDataAdapter As New SqlClient.SqlDataAdapter
Dim connstr As String
connstr = ConfigurationSettings.AppSettings("dsn")
myConn.ConnectionString = connstr
myConn.Open()

myCmd.CommandText = "update tblshebeilingyong set shebeibianhao=@shebeibianhao,shebeimingcheng=@shebeimingcheng,shebeiguige=@shebeiguige,lingyongbumen=@lingyongbumen,lingyongyuangong=@lingyongren,lingyongriqi=@lingyongriq where bdbh=@bdbh"



myCmd.Parameters.Clear()
myCmd.Parameters.Add(New SqlClient.SqlParameter("@shebeibianhao", SqlDbType.VarChar, 50))
myCmd.Parameters.Add(New SqlClient.SqlParameter("@shebeimingcheng", SqlDbType.VarChar, 50))
myCmd.Parameters.Add(New SqlClient.SqlParameter("@shebeiguige", SqlDbType.VarChar, 200))
myCmd.Parameters.Add(New SqlClient.SqlParameter("@shebeizhuangtai", SqlDbType.VarChar, 50))
myCmd.Parameters.Add(New SqlClient.SqlParameter("@lingyongriqi", SqlDbType.SmallDateTime, 4))
myCmd.Parameters.Add(New SqlClient.SqlParameter("@lingyongbumen", SqlDbType.VarChar, 50))
myCmd.Parameters.Add(New SqlClient.SqlParameter("@lingyongren", SqlDbType.VarChar, 50))
myCmd.Parameters.Add(New SqlClient.SqlParameter("@bdbh", SqlDbType.VarChar, 50))


myDataAdapter.SelectCommand.Parameters("@shebeibianhao").Value = Trim(shebeibianhao.Text)
myDataAdapter.SelectCommand.Parameters("@shebeimingcheng").Value = Trim(shebeimingcheng.Text)
myDataAdapter.SelectCommand.Parameters("@shebeiguige").Value = Trim(shebeiguige.Text)
myDataAdapter.SelectCommand.Parameters("@lingyongriqi").Value = Trim(lingyongriqi.Text)
myDataAdapter.SelectCommand.Parameters("@lingyongbumen").Value = Trim(lingyongbumen.Text)
myDataAdapter.SelectCommand.Parameters("@lingyongren").Value = Trim(lingyongyuangong.Text)
myDataAdapter.SelectCommand.Parameters("@bdbh").Value = ResourceInList.DataKeys.Item(e.Item.ItemIndex)

myConn.Open()
myCmd.ExecuteNonQuery()
myConn.Close()
ResourceInList.EditItemIndex = -1
BindResourceInData()



End Sub
--------------------------------------------------------------------------------

指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidCastException: 指定的转换无效。

源错误:


行 192: Private Sub ResourceInList_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles ResourceInList.UpdateCommand
行 193: Dim shebeibianhao, shebeimingcheng, shebeiguige, lingyongbumen, lingyongren, lingyongriqi, bdbh As TextBox
行 194: bdbh = e.Item.Cells(0).Controls(0)
行 195: shebeibianhao = e.Item.Cells(1).Controls(0)
行 196: shebeimingcheng = e.Item.Cells(2).Controls(0)

...全文
60 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
nagexiatiank 2008-07-16
  • 打赏
  • 举报
回复
调试主要是看逻辑有没有问题.
snowshaken 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xiaoqijun 的回复:]
在出错行设个断点..... 调试模式下运行....
[/Quote]

可是调试没出错啊
snowshaken 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhuanshen712 的回复:]
就是没找到控件,我上个回帖还给你举了个例子...
唉!
[/Quote]

怎么确定有没有对应上?
谢谢!
zhuanshen712 2008-07-16
  • 打赏
  • 举报
回复
就是没找到控件,我上个回帖还给你举了个例子...
唉!
安宁 2008-07-16
  • 打赏
  • 举报
回复
在出错行设个断点..... 调试模式下运行....
snowshaken 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xiaoqijun 的回复:]
难道连调试都不会吗?
[/Quote]

不好意思,新手
能说的详细一点吗
安宁 2008-07-16
  • 打赏
  • 举报
回复
难道连调试都不会吗?
zhuanshen712 2008-07-16
  • 打赏
  • 举报
回复
e.Item.Cells(0).Controls(0)
对应的是DataGrid控件中 第一列中的第一个控件
e.Item.Cells(1).Controls(0)
对应的是DataGrid控件中 第二列中的第一个控件

以此类推...
看你的控件有没有对应上?

比如: 下面是一个Datagrid

第一列 | 第二列 | 第三列 |
Lable | TextBox Image | LinkButton |
Lable | TextBox Image | LinkButton |
Lable | TextBox Image | LinkButton |
Lable | TextBox Image | LinkButton |

e.Item.Cells(0).Controls(0) 对应的是第一列第一个控件 Lable
e.Item.Cells(1).Controls(0) 对应的是第二列中的第一个控件 TextBox

e.Item.Cells(1).Controls(1) 对应的是第二列中的第二个控件 Image
bindsang 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 snowshaken 的回复:]
引用 1 楼 xiaoqijun 的回复:
查看一下出错行的Control(0)的控件类型...是否是TextBox..?


怎么查看啊大哥

ps:我刚入门
[/Quote]
在那一行下断点,调式的时候看看e.Item.Cells(0).Controls(0)的类型是不是TextBox
一品梅 2008-07-16
  • 打赏
  • 举报
回复
用FindControl
snowshaken 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xiaoqijun 的回复:]
查看一下出错行的Control(0)的控件类型...是否是TextBox..?
[/Quote]

怎么查看啊大哥

ps:我刚入门
yuelailiu 2008-07-16
  • 打赏
  • 举报
回复
是不是某 Cells(?) 找不到指定的control(0)?
安宁 2008-07-16
  • 打赏
  • 举报
回复
查看一下出错行的Control(0)的控件类型...是否是TextBox..?

61,819

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧