Link to home
Start Free TrialLog in
Avatar of Hojoformo
Hojoformo

asked on

ASP.NET/ADO - DataSet Tables

I am trying to put together a simple program to read in a comma delimited file and store to database table but I am getting an error with my dataset "Cannot find table 0."

SqlCommand cmdSql = new SqlCommand( "Select Top 1 From Mytable", conn );
DataSet ds = new DataSet();
conn.Open();
dg.DataSource = cmdSql.ExecuteReader();
dg.DataBind();
conn.Close();
.
RowCounter++;
.

foreach (string a in split)
{
ds.Tables[0].Rows[RowCounter][ColCounter]= a;   "Error occurs here"  
ColCounter++;
}
 
I am just trying to copy the contents from my text file to a column in the DatSet table.  What am I missing?
Avatar of Coolhand2120
Coolhand2120
Flag of United States of America image

I had this probelm too, you must change your select statement to read somthing more like :

strPath = Server.MapPath("/")
set objConnection=Server.CreateObject("ADODB.Connection")
objConnection.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & strPath & "\somedirectory\" & ";Extensions=asc,csv,tab,txt"
strSQL = "Select * From " & uploadname  '<--------------------Here you must have the FILENAME of the file you're trying to read from
set objRS=Server.CreateObject("ADODB.RecordSet")
objRS.Open strSQL, objConnection,3,3
arrRows=objRS.GetRows()
set objRS = nothing
set objConnection = nothing

You don't have to use this method, just understand that the SQL statement must contain the file name of the file you are parseing.  If you're using an upload componet you can usualy find this value somewhere in it's object collection.

Sorry my only example is in VB, but the object syntax is the same.

-Coolhand2120
Avatar of Hojoformo
Hojoformo

ASKER

Actually, I only posted a small portion of the code.  I am using the streamreader object to read my txt file.  I am having a problem with assigning the contents of the text file to a column of my dataset.  There is something wrong with this line of code "ds.Tables[0].Rows[RowCounter][ColCounter]= a;".   I am new to asp.net/ado so I am sure it is something simple.    The error i get is "Cannot find table 0."  ANy other suggestions?  Thanks!

filetoread = Server.MapPath("test2.txt");
StreamReader reader = new StreamReader(filetoread);
for (string readcontents = reader.ReadLine(); readcontents != null; readcontents = reader.ReadLine())
{
  char [] delimiter = textdelimiter.ToCharArray();
  splitout = readcontents;
  String [] split = null;
  split = readcontents.Split(delimiter);
  ColCounter = 0;

  foreach (string a in split)
  {                          
    ds.Tables[0].Rows[RowCounter][ColCounter]= a;    ///// error - Cannot find table 0."
    ColCounter++;
   }
                        
RowCounter++;
I see the problem.  I was just missing the "fill".    Thanks for your help!

da.Fill(ds, "Mytable");
ASKER CERTIFIED SOLUTION
Avatar of Coolhand2120
Coolhand2120
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial