c# - Why is my returned DataSet instance has no tables? -


when connect db , want read data table doesn't work shows these exception details:

system.indexoutofrangeexception: cannot find table 0. error in line 141

i have 1 table in dataset. know best way read table me ? return 1 table. when use foreach, how can read 1 row data?

       internal dataset read(string query)         {         dataset nds = new dataset();         try         {              string connectionstring = "...";             try             {                  sqlconnection nsqlc = new sqlconnection(connectionstring);                 sqlcommand = new sqlcommand(query, nsqlc);                 nsqlc.open();                 sqldataadapter nsda = new sqldataadapter(get);                 nsda.fill(nds);                 nsqlc.close();             }             catch (exception)             {               }              return nds;         }         catch (exception)         {              return nds;         }     } 

main form :

            links = links + t.tables[0].rows[i][0].tostring() + "%";             string links = "";             links = "movie%";             dataset t = n.read("select id , poster movie order id desc");             (int = 0; < 10; i++)             {                 links = links + t.tables[0].rows[i][0].tostring() + "%";                 links = links + t.tables[0].rows[i][1] + "%";              } 

the closest thing answer here have n.read method returns dataset instance has no tables in it. need step method debugger , figure out why behaves that.

if see problem is, fix it. if not, share read method code , able further.

looking @ updated post, see swallowing every possible exceptions occur while trying fetch data. remove catch block read method. allow see real problem is.

exception swallowing you'll want take away code on global scale. i'd suggest googling find out why it's terrible habit.

about "better way read table", should consider using idatareader.

string links = "movie%";  using (var connection = new sqlconnection("your connection string"); {     using (var command = someexistingconnection.createcommand())     {         command.commandtext = "select id, poster movie order id desc";         command.connection = connection;          connection.open();          try         {             using (var reader = command.executereader())             {                 while (reader.read())                 {                     var idvalue = reader.getobject(0).tostring(); // better use actual field type, unknown @ time i'm writing                     var postervalue = reader.getstring(1);                      // perform concatenation here using 2 variables declared above                     links += string.format("{0}%{1}%", idvalue, postervalue);                 }             }         }                 {             connection.close();         }     } } 

this outperform working dataset long shot.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -