我也碰到一个问题,
就是说用DATAGRID 的时候,点击更新,就发生错误:索引超出范围。必须为非负值并小于集合大小。参数名: index
行 54: strSql = "Update link Set sitename='" & txtSiteName.Text & "',URL='" & txtURL.Text & "',intro='" & txtIntro.Text & "',grade=" & CInt(txtGrade.Text) & ",submit_date=#" & Now() & "# Where link_id=" & myDataGrid.DataKeys(CInt(E.Item.ItemIndex))放在数据库中也没什么问题求救
就是说用DATAGRID 的时候,点击更新,就发生错误:索引超出范围。必须为非负值并小于集合大小。参数名: index
行 54: strSql = "Update link Set sitename='" & txtSiteName.Text & "',URL='" & txtURL.Text & "',intro='" & txtIntro.Text & "',grade=" & CInt(txtGrade.Text) & ",submit_date=#" & Now() & "# Where link_id=" & myDataGrid.DataKeys(CInt(E.Item.ItemIndex))放在数据库中也没什么问题求救
解决方案 »
- 有一网页客户端打开时 默认打开客户端的E盘如何实现
- vs开发环境,于.exe问题
- 求思路,统计不定项的数目
- 怎么样才能在一个客户端里同时连接两台服务器上的REMOTING服务?
- 在BS中使用多线程
- 急,js 限制 text 的输入!!!
- font类里没有关于颜色的设置,或都说颜色不应该在font类里
- a=3,则 a=b+1 ,a=b+2,=b+3, 问题,想不明白了,各位来帮忙看看。
- DirectoryEntry 对ActiveDirectory操作,用Rename方法和Moveto方法。有点问题!在线等
- 怎样使用非标准的WINDOWS的ActiveX控件?非常急的问题!!!
- 私人寻找合作伙伴!
- 我吧XML文件以文本格式存在数据库里, 请问如何把它取出来并生成一个XML文件??
如果你的txtUrl中带有',你的语句就玩完了。
"Now()" 换为DateTime.Now 试试
Sub MyDataGrid_Update(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs) Handles myDataGrid.UpdateCommand
'建立Connection对象
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("wwwlink.mdb"))
'建立Command对象
Dim txtSiteName, txtURL, txtIntro, txtGrade As TextBox '声明文本框控件变量
txtSiteName = E.Item.Cells(2).Controls(0)
txtURL = E.Item.Cells(3).Controls(0)
txtIntro = E.Item.Cells(4).Controls(0)
txtGrade = E.Item.Cells(5).Controls(0)
Dim strSql As String
strSql = "Update link Set sitename='" & txtSiteName.Text & "',URL='" & txtURL.Text & "',intro='" & txtIntro.Text & "',grade=" & CInt(txtGrade.Text) & ",submit_date=#" & DateTime.Now() & "# Where link_id=" & myDataGrid.DataKeys(CInt(E.Item.ItemIndex))
Dim cmd As New OleDbCommand(strSql, conn)
'执行更新操作
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
'重新绑定
myDataGrid.EditItemIndex = -1
Call BindData()
End Sub
不好意思,没看见你写的错误 :"索引超出范围。必须为非负值并小于集合大小"
这个错误应该不是COMMAND的问题,可能是“myDataGrid.DataKeys(CInt(E.Item.ItemIndex))”有问题to: 楼主
不好意思,不知你的问题是否解决?没有的话,把代码那出来,大家研究一下
{
bool bResult = false;
if (book == null) return bResult;
bResult = true; string connectstring = ApplicationConnectString.ConnectString;
OleDbConnection conn = new OleDbConnection(connectstring);
conn.Open(); OleDbCommand command = new OleDbCommand();
command.Connection = conn; string commandText = "UPDATE Book SET BOOKCODE =";
commandText = book.BookCode == null ? commandText + "null," : commandText + "'" + book.BookCode + "',BOOKNAME = ";
commandText = book.BookName == null ? commandText + "null," : commandText + "'" + book.BookName + "',AUTHOR = ";
commandText = book.Author == null ? commandText + "null," : commandText + "'" + book.Author + "',ISSUER = ";
commandText = book.Issuer == null ? commandText + "null," : commandText + "'" + book.Issuer + "',CONTENT = ";
commandText = book.Content == null ? commandText + "null," : commandText + "'" + book.Content + "',POSITION = ";
commandText = book.Position == null ? commandText + "null" : commandText + "'" + book.Position + "',RKTIME = ";
commandText = commandText+ "'" + book.RKTime + "'";
commandText += " where bookid =" + book.BookID; command.CommandText = commandText;
try
{
command.ExecuteNonQuery();
}
catch(Exception e)
{
bResult = false;
throw(e);
} conn.Close();
return bResult;
}
建议请贴出代码。
改为commandText += " where bookid ='" + book.BookID+"'"
我Update另外一张表就没有报错,那张表的结构和Book表几乎一样!另外我数据库是用的access,所以查错的手段比较少(也许有,我不知道)To fellowcheng(头都编大了):
Bookid是一个int型的,应该不会是你所说的问题大家有没有人碰到过类似的情况,最后解决的?
我现在总结一下我的问题:
1.直接用程序Update,抛出错误“Update语句语法错误”
2.跟踪进去,把SQL语句提取出来,不做任何改动,直接在数据库中执行,是ok的
3.我update另外一个表,语句也是这么写的,没有问题!
4.我真的很郁闷,从来没有碰到过这种问题!
直接将commandText 的值换成一句最简单的sql语句
比如update table set filed = 'xx'来看看哦。
一般来说语法错误都是sql的问题。
如果你是自动生成的command的话,才有可能是系统生成代码和你数据库不对应造成。
不过你是用一个command来做的,应该不会有这问题。
如你用commandtext最简单的语句代替还是有这问题的话,那就不清楚了。。