帮助我纠正此错误 [英] help me for rectify this error
问题描述
I get "error=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".
Private Sub txt_unload_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_unload.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
con.Open()
cmd.Connection = con
cmd.CommandText = "insert into Trip(TDate,TFrom,Tto,Party,KM,Quantity,Rate,Type,FAmt,Rcv,Balance,TLoad,TUnload,TSDate,VehNo)values('" & dtp_trip.Value & "','" & cb_from.Text & "','" & cb_to.Text & "', '" & cb_party.Text & "', " & txt_km.Text & ", " & txt_qty.Text & "," & txt_rate.Text & ",'" & cb_mkqty.Text & "', " & txt_freight.Text & "," & txt_recv.Text & "," & txt_bal.Text & ", " & txt_load.Text & "," & txt_unload.Text & ",'" & dtp_date.Value & "','" & cb_vno.Text & "')"
cmd.ExecuteNonQuery()
dtp_trip.Focus()
'ElseIf e.KeyChar = ChrW(Keys.Tab) Then
''Dim cmdstr As String
'cmdstr = "select * from Trip"
'da = New SqlDataAdapter(cmdstr, con)
'ds = New DataSet()
'da.Fill(ds, "trip")
'dgtrip.DataSource = ds.Tables(0)
Dim row As New DataGridViewRow
Dim cell As New DataColumn
row = New DataGridViewRow
'row.Cells(0).Value = dtp_trip.Value
row.Cells(1).Value = cb_from.Text
row.Cells(2).Value = cb_to.Text
row.Cells(3).Value = cb_party.Text
row.Cells(4).Value = txt_km.Text
row.Cells(5).Value = txt_qty.Text
row.Cells(6).Value = txt_rate.Text
row.Cells(7).Value = cb_mkqty.Text
row.Cells(8).Value = txt_freight.Text
row.Cells(9).Value = txt_recv.Text
row.Cells(10).Value = txt_bal.Text
row.Cells(11).Value = txt_load.Text
row.Cells(12).Value = txt_unload.Text
dgtrip.Rows.Add(row)
End If
End Sub
推荐答案
Dim row As New DataGridViewRow
row.Cells(1).Value = cb_from.Text
仅此一项就会给您带来问题.
您需要先创建单元格,然后才能为其设置值!如果您已经设置了DataGridView的列,则首先添加该行,或者在创建一个方法之前,先创建一个空白行,该行具有足够的单元格来容纳数据.
顺便说一句:不要那样做您的SQL查询!不要连接字符串以构建SQL命令.它使您对意外或蓄意的SQL注入攻击敞开大门,这可能会破坏整个数据库.改为使用参数化查询.
This alone will give you the problem.
You need to create the cells before you can set a value for them! If you have set the columns for the DataGridView already, then add the row first, or look at creating a method with creates a blank row with enough cells for your data before you insert it.
BTW: do not do your SQL query like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
索引超出范围.必须为非负数并且小于集合的大小
当参数的值超出所调用的方法定义的允许的值范围时抛出的异常.
此处的详细信息: MSDN:ArgumentOutOfRangeException类 [ SQL注入缓解:使用参数化查询 [
Index was out of range. Must be non-negative and less than the size of the collection
The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.
Details here: MSDN: ArgumentOutOfRangeException Class[^]
Here, are you sure that your DataGridViewRow has 13 rows? Cell''s are 0 indexed and thus if you have 12 rows, then your cell index would be from 0 to 11. Please check it.
Further, the way you have implemented your database update code is a bad practice. You have left your Db open for SQL Injection.
Read about protecting from SQL Injection here: SQL Injection Mitigation: Using Parameterized Queries[^]
我将其更正为:
I corrected this as follow:
If e.KeyChar = ChrW(Keys.Enter) Then
con.Open()
cmd.Connection = con
cmd.CommandText = "insert into Trip(TDate,TFrom,Tto,Party,KM,Quantity,Rate,Type,FAmt,Rcv,Balance,TLoad,TUnload,TSDate,VehNo)values('" & dtp_trip.Value & "','" & cb_from.Text & "','" & cb_to.Text & "', '" & cb_party.Text & "', " & txt_km.Text & ", " & txt_qty.Text & "," & txt_rate.Text & ",'" & cb_mkqty.Text & "', " & txt_freight.Text & "," & txt_recv.Text & "," & txt_bal.Text & ", " & txt_load.Text & "," & txt_unload.Text & ",'" & dtp_date.Value & "','" & cb_vno.Text & "')"
cmd.ExecuteNonQuery()
dtp_trip.Focus()
Dim dgvRow As New DataGridViewRow
Dim dgvCell As DataGridViewCell
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = dtp_trip.Value
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = cb_from.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = cb_to.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = cb_party.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = txt_km.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = txt_qty.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = txt_rate.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = cb_mkqty.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = txt_freight.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = txt_recv.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = txt_bal.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = txt_load.Text
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = txt_unload.Text
dgvRow.Cells.Add(dgvCell)
dgtrip.Rows.Add(dgvRow)
con.Close()
End If
我尝试将值传递给网格...我通过此编码获得了它...感谢您提出的答案
I try to pass the values to grid... i got it through this coding..... Thanks to you for suggest your answer
这篇关于帮助我纠正此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!