• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

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?
0
Hojoformo
Asked:
Hojoformo
  • 2
  • 2
1 Solution
 
Coolhand2120Commented:
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
0
 
HojoformoAuthor Commented:
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++;
0
 
HojoformoAuthor Commented:
I see the problem.  I was just missing the "fill".    Thanks for your help!

da.Fill(ds, "Mytable");
0
 
Coolhand2120Commented:
NP!  Glad you figured it out!

-Coolhand2120
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now