Click here to Skip to main content
15,914,409 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
I get "error=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".


VB
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
Posted
Comments
Prasad_Kulkarni 16-May-12 1:15am    
You might be inserting more data than you have defined its size in database. Check the size of each variable you have declared in database & actual value you're inserting.

VB
Dim row As New DataGridViewRow
row.Cells(1).Value = cb_from.Text
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.
 
Share this answer
 
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[^]
 
Share this answer
 
I corrected this as follow:

VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900