Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have one table in sql bank, but when i run my program gives me an error message:cannot find table 0.

C#
for (int i = 0; i <= ds.Tables[0].Rows.Count; i++)
        {
            cmd.CommandText = @"select * from metb";
            da.SelectCommand = cmd;
            da.Fill(ds);
            string fnList = ds.Tables[0].Rows[i]["fn"].ToString();
            Label1.Text += fnList+"/n";
        }
Posted

It looks like your are trying to loop through the dataset before you fill it.
You need to add or refresh the rows in dataset first and then loop through the rows.
something like this

C#
cmd.CommandText = @"select * from metb";
            da.SelectCommand = cmd;
            da.Fill(ds);

        if (ds.Tables.Count > 0)
        {
         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
         {
            string fnList = ds.Tables[0].Rows[i]["fn"].ToString();
            Label1.Text += fnList+"/n";
         }
        }
 
Share this answer
 
v3
Comments
me64 24-Aug-12 4:24am    
it gives the message:There is no row at position 3. However,there is that
pramod.hegde 24-Aug-12 4:34am    
Check the modified solution in Solution2.
i <= ds.Tables[0].Rows.Count has been modified to
i < ds.Tables[0].Rows.Count.

Now you should not get any errors.
__TR__ 24-Aug-12 4:40am    
Thanks for the correction :)
Hi,

Your query may result none. Check you database query. Also check if you have nonzero table count. if it is zero then return some appropriate message.

Thanks
-Amit
 
Share this answer
 
Comments
me64 24-Aug-12 4:14am    
There is a table in the database.However, it gives the message.
AmitGajjar 24-Aug-12 4:16am    
add one condition ds.Tables.Count >0 in your code and check if it passed from this condition?
me64 24-Aug-12 4:21am    
if (ds.Tables.Count > 0)
{
for (int i = 0; i <= ds.Tables[0].Rows.Count; i++)
{
cmd.CommandText = @"select * from metb";
da.SelectCommand = cmd;
da.Fill(ds);
string webAddress = ds.Tables[0].Rows[i]["website"].ToString();
Label1.Text += webAddress + "/n";
}
}
else
{
Label2.Text = "Error";
}

Result: Error
AmitGajjar 24-Aug-12 4:22am    
on which line ?
AmitGajjar 24-Aug-12 4:23am    
before fillup your dataset you will not find any table. so after da.Fill(ds) you need to use ds.Table[0] to access first table.
I thing you have connection error.
Check your sql connection setting.
 
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