Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Every One,

When i run my project i get Error "There is no row at position 0",please help.

Here is my code:


VB
Private Sub PopulateDepartmentHead()

        'Fill data in comboBox
        Dim SqlDataAdapter As OleDbDataAdapter
        Dim DSet As New DataSet
        Dim strSelect As String
        SQLConn.Close()
        SQLConn.ConnectionString = oFunc.GetConnectionString(sINIFile)
        'myConn = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & strPath & "\" & strName & ".mdf" & ";Integrated Security=True;User Instance=True")
        SQLConn.Open()

        Try
            strSelect = "SELECT * FROM dbo.hrEmployeeInformation"
            SqlDataAdapter = New OleDbDataAdapter(strSelect, SQLConn)
            DSet = New DataSet()
            SqlDataAdapter.Fill(DSet)
            Me.dhcboEmployeeSelect.DataSource = DSet.Tables(0)
            Me.dhcboEmployeeSelect.ValueMember = "eiEmployeeID"
            Me.dhcboEmployeeSelect.DisplayMember = "eiFirstName"
            'MsgBox(Me.dhcboEmployeeSelect.DisplayMember = "Cust_ID")
            SQLConn.Close()
        Catch ex As Exception
            MessageBox.Show("Error No : " & Err.Number & vbCrLf _
                          & "Error : " & ex.Message & vbCrLf _
                          & "Source : " & Err.Source & vbCrLf _
                          , "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


Code for selectindexchanged event:

VB
Private Sub dhcboEmployeeSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dhcboEmployeeSelect.SelectedIndexChanged
        Dim SqlDataAdapter As OleDbDataAdapter
        Dim custDataset As New DataSet
        Dim strSelect As String
        SQLConn.Close()
        SQLConn.ConnectionString = oFunc.GetConnectionString(sINIFile)
        SQLConn.Open()

        Try
            strSelect = "SELECT * FROM dbo.hrEmployeeInformation WHERE eiEmployeeID = '" & dhcboEmployeeSelect.Text & "'"
            SqlDataAdapter = New OleDbDataAdapter(strSelect, SQLConn)
            custDataset = New DataSet()
            SqlDataAdapter.Fill(custDataset, "dbo.hrEmployeeInformation")
            SqlDataAdapter.Dispose()
            Dim custTable As DataTable = custDataset.Tables(0)

            dhEmployeeID.Text = custTable.Rows(0).Item(0)
            dhEmployeeName.Text = custTable.Rows(0).Item(1)

            SQLConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            SQLConn.Close()
        End Try
    End Sub


Code for Form Load:

VB
Private Sub frmDepartmentHead_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            SQLConn.Close()
            SQLConn.ConnectionString = oFunc.GetConnectionString(sINIFile)
            FormName()
            EnableControlsFormLoad(True)
            SQLConn.Open()
            ds.Clear()
            SQLConn.Close()
            'Fill Combo
            PopulateDepartmentHead()
        Catch ex As Exception
            MessageBox.Show("Error No : " & Err.Number & vbCrLf _
                          & "Error : " & ex.Message & vbCrLf _
                          & "Source : " & Err.Source & vbCrLf _
                          , "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


PLEASE HELP ME, WHERE DID I WENT WRONG....

Thanks
Posted
Updated 18-Feb-13 0:22am
v2

C#
Me.dhcboEmployeeSelect.DataSource = DSet.Tables(0)


I think this line you got that error message .
What's the problem in that is your DataSet doesn't have "0"th Tables.
But your try to call the table[0]. that's way this error occured.

Once try to execute your SQL query ,your query returns any value or not checkout that..
SQL
strSelect = "SELECT * FROM dbo.hrEmployeeInformation WHERE eiEmployeeID = '" & dhcboEmployeeSelect.Text & "'"


check this line returns any result or empty..
 
Share this answer
 
v2
The exception is thrown at line
dhEmployeeID.Text = custTable.Rows(0).Item(0)

You should always check if there is at least one row in table before accessing it.
You can do this with
custTable.Rows.Count > 0

The same can be said for line
Dim custTable As DataTable = custDataset.Tables(0)


Also there are few points which can be eliminated when using DataAdapter
1. You do not need to open or close connection as DataAdapter does this for you.
2. You should not call dispose on DataAdapter. Ideally, you should retain the same DataAdapter because it has already performed it's initialization. A DataAdapter provides properties such as the SelectCommand, UpdateCommand, InsertCommand and DeleteCommand which allow you to set different Command objects to perform these different function on the datasource. So, you see, the DataAdapter is designed to be reused for multiple commands (for the same database connection).
 
Share this answer
 
hi dear,

please check
VB
If DSet.Tables[0] is not nothing and DSet.Tables(0).Rows.Count > 0 

before assign datasource to dhcboEmployeeSelect
 
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