Question Display data in datagridview

tqmd1

Well-known member
Joined
Dec 5, 2009
Messages
60
Programming Experience
Beginner
Dear Experts

Table gpass has data as
gate-----date-----c----bno----acccode----tittle----weight-------qty
1----18/01/2010---a----2--------2----------abc-----10.00-------15

Then there are two textbox having values as

txtgat.text=1
txtdat.text=18/01/2010

I these codes and it generates error message as

VB.NET:
ArgumentOutOfRangeException was unhandled.
Idex was out of range. Must be non-negative and less than the size of collection. Parameter name: Index


What mistake I am doing, please modify


VB.NET:
Private Sub txtGat_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGat.TextChanged
        If Len(Me.txtGat.Text) > 0 AndAlso Me.txtDat.Text IsNot Nothing Then

            str = "Select * from gpass where vou_no= " & Val(Me.txtGat.Text) & " And Date =   '" & Me.txtDat.Text & "'"
            dt = GetTable(str)

            If (dt.Rows.Count > 0) Then
                Me.txtCir.Text = dt.Rows(0)("c")
                Me.txtVou.Text = dt.Rows(0)("bno")
                cmd.ExecuteNonQuery()

            End If


            For i = 0 To dt.Rows.Count -1
                Me.DataGridView1.Rows.Add()
                Me.DataGridView1.Rows(i).Cells(0).Value = Val(dt.Rows(i).Item(1))
                Me.DataGridView1.Rows(i).Cells(1).Value = Trim(dt.Rows(i).Item(2))
                Me.DataGridView1.Rows(i).Cells(2).Value = Val(dt.Rows(i).Item(3))
                Me.DataGridView1.Rows(i).Cells(3).Value = Val(dt.Rows(i).Item(4))
            Next

        End If
    End Sub
 
The TextBoxChange event fires every time the data in the textbox changes, so that means if the data entered is 10 then it will have fired 2 times. Once for the data 1, and once for the data 10. It even fires when the backspace key is pressed, probably not the functionality that you are looking for. It would probably be better if you used the TextBoxLeave event.

VB.NET:
    Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtGat.Leave

        If Not String.IsNullOrEmpty(txtGat.Text) AndAlso Not String.IsNullOrEmpty(txtDat.Text) Then

            Str = "Select * from gpass where vou_no= " & Integer.Parse(txtGat.Text) & " AND Date = '" & txtDat.Text & "'"

            dt = GetTable(Str)

            If dt.Rows.Count > 0 Then
                txtCir.Text = dt.Rows(0)("c").ToString()
                txtVou.Text = dt.Rows(0)("bno").ToString()
                cmd.ExecuteNonQuery()

            End If

            Dim AddedRowIndex As Integer

            For i = 0 To dt.Rows.Count - 1
                AddedRowIndex = DataGridView1.Rows.Add()
                DataGridView1.Rows(AddedRowIndex).Cells(0).Value = dt.Rows(i)(1).ToString() 'date Row
                DataGridView1.Rows(AddedRowIndex).Cells(1).Value = dt.Rows(i)(2).ToString() 'c Row
                DataGridView1.Rows(AddedRowIndex).Cells(2).Value = dt.Rows(i)(3).ToString() 'bno Row
                DataGridView1.Rows(AddedRowIndex).Cells(3).Value = dt.Rows(i)(4).ToString() 'accode Row
            Next
            'DataGridView1.Refresh()

        End If 'end check for txtGat and txtDat
  End Sub

Suggestion:

In the middle of this code a command object performs a ExecuteNonQuery. There doesn't appear to be a connection set nor can you tell if the connection object attached to the command object is still viable at the time of execution. What if the user waits 2 hours to perform the action that would execute the query? There also doesn't appear to be any clean up of the connection or command.

VB.NET:
If dt.Rows.Count > 0 Then
                txtCir.Text = dt.Rows(0)("c").ToString()
                txtVou.Text = dt.Rows(0)("bno").ToString()
                'instead of this  cmd.ExecuteNonQuery() use the following

                Using sqlCon As New SqlClient.SqlConnection("Your Connection String")
                    If Not sqlCon.State = ConnectionState.Open Then sqlCon.Open()

                    Using sqlCmd As New SqlClient.SqlCommand()
                        sqlCmd.Connection = sqlCon
                        sqlCmd.CommandText = "Your SQL query here"
                        sqlCmd.CommandType = CommandType.Text

                        sqlCmd.ExecuteNonQuery()

                    End Using 'end using sqlCmd
                End Using 'end using SqlCon

            End If
 
Back
Top