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[RowCount er][ColCou nter]= 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?
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[RowCount
ColCounter++;
}
I am just trying to copy the contents from my text file to a column in the DatSet table. What am I missing?
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[RowCoun ter][ColCo unter]= 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(delimit er);
ColCounter = 0;
foreach (string a in split)
{
ds.Tables[0].Rows[RowCount er][ColCou nter]= a; ///// error - Cannot find table 0."
ColCounter++;
}
RowCounter++;
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(delimit
ColCounter = 0;
foreach (string a in split)
{
ds.Tables[0].Rows[RowCount
ColCounter++;
}
RowCounter++;
ASKER
I see the problem. I was just missing the "fill". Thanks for your help!
da.Fill(ds, "Mytable");
da.Fill(ds, "Mytable");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
strPath = Server.MapPath("/")
set objConnection=Server.Creat
objConnection.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & strPath & "\somedirectory\" & ";Extensions=asc,csv,tab,t
strSQL = "Select * From " & uploadname '<--------------------Here
set objRS=Server.CreateObject(
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