Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
System.IndexOutOfRangeException: 'Cannot find table 0.'


public UserControl3()
        {
            InitializeComponent();
            SQLiteConnection sqlcon = new SQLiteConnection("Data Source = carRentalDB.db;Version = 3; New = False; Compress = True;");
            SQLiteCommand sqlcmd = new SQLiteCommand("SELECT * FROM Client", sqlcon);
            sqlcon.Open();
            SQLiteDataAdapter sda = new SQLiteDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
            {
                cbClient.Items.Add(DS.Tables[0].Rows[i][0] + "|" + DS.Tables[0].Rows[i][1]);
            }
            
        }


What I have tried:

I tried using different events if it will work but have not
Posted
Updated 16-Mar-22 4:33am
Comments
Maciej Los 16-Mar-22 8:32am    
ds != DS
In other words, C# is case sensitive.

At the first glance - replace this:
C#
cbClient.Items.Add(DS.Tables[0].Rows[i][0] + "|" + DS.Tables[0].Rows[i][1]);

with this:
C#
cbClient.Items.Add(ds.Tables[0].Rows[i][0] + "|" + DS.Tables[0].Rows[i][1]);

due to the fact that C# is case-sensitive and you've declared:
C#
DataSet ds = new DataSet();
 
Share this answer
 
v4
Comments
Ernest Jan Sandoval 16-Mar-22 10:29am    
thank you .i did not notice it
Maciej Los 16-Mar-22 13:39pm    
Glad to hear that.
Ernest Jan Sandoval 16-Mar-22 10:30am    
and how can i put it in datagridview in different rows?
Maciej Los 16-Mar-22 13:39pm    
???
C# is case-sensitive, Use ds instead of DS on all occurrences.
DataSet ds = new DataSet();
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    cbClient.Items.Add(ds.Tables[0].Rows[i][0] + "|" + ds.Tables[0].Rows[i][1]);
}
 
Share this answer
 
Comments
Maciej Los 16-Mar-22 13:40pm    
If you wonder why your answer has been down-voted, i'll explain you.
M Imran Ansari 16-Mar-22 22:17pm    
Could you please explain?
Maciej Los 17-Mar-22 10:22am    
Well... Take a look at answer of mine and yours. Do they look very (i mean very) similar? Mine and yours answers are pointing out to the same things: 1) bad name od DataSet and 2) c# is case-sensitive. Conclusion: your answer is duplicate of mine (even if it's indirectly).
So, your answer has been reported as a repost, because you posted it almost 1 hour later then me (you have seen it before you wrote your answer). Some members of CP does not accept such of behaviour and they rate your answer of 1, because your practice is called "reputation hunting".
I'd strongly suggest delete your answer to avoid further down-voting.
M Imran Ansari 17-Mar-22 10:45am    
Well... Check your with this part????
You have mentioned ("|" + DS.Tables[0].Rows[i][1]) and in this part you have again mentioned DS which is not correct. there should be 'ds' whereas using DS. isn't it? There are three occurrences where DS was used.
Maciej Los 17-Mar-22 11:25am    
OK. I did not change the code completely, but this changes nothing. Your answer is a duplicate of mine.

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