Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL VB.NET
Hello Every One,
 
When i run my project i get Error "There is no row at position 0",please help.
 
Here is my code:
 

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:
 
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:
 
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 18-Feb-13 0:07am
Edited 18-Feb-13 0:22am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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..
strSelect = "SELECT * FROM dbo.hrEmployeeInformation WHERE eiEmployeeID = '" & dhcboEmployeeSelect.Text & "'"
 
 
check this line returns any result or empty..
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

hi dear,
 
please check
If DSet.Tables[0] is not nothing and DSet.Tables(0).Rows.Count > 0 
before assign datasource to dhcboEmployeeSelect
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100