This works as long as every ‘item’ is numeric OR every item is alpha numeric. When i have ‘items’ that are numeric AND alpha numeric it does not fill in the values correctly.
Here is the code:
public void updateInventory()
{
try
{
if (File.Exists(inventoryUpdateDirectory + "inventoryUpdate.csv"))
{
csvReader csv = new csvReader();
DataTable inventory = csv.read(inventoryUpdateDirectory + "inventoryUpdate.csv");
//int test = inventory.Rows.Count;
string sql = "";
foreach (DataRow inventoryItem in inventory.Rows)
{
try
{
sql = " Update Inventory set OnHand = " + inventoryItem[1] + " WHERE Sku = '" + inventoryItem[0].ToString().Trim() + "'";
//executeSQL(sql);
}
catch { }
}
File.Delete(inventoryUpdateDirectory + "inventoryUpdate.csv");
}
else
{
writeToFile("fileDoesntExist", inventoryUpdateDirectory + "error.txt");
}
}
catch { }
}
Here is the file it reads:
ALB001,0
ALB002,66
10001,0
10016,348
This will work:
ALB001,0
ALB002,66
This will work:
10001,0
10016,348
This will not work:
ALB001,0
ALB002,66
10001,0
10016,348
It fills out the inventoryItem array as an empty {}
+ inventoryItem[0] {} object {System.DBNull}
Which should have the value of ALB001
The first ‘column’ in the CSV should always be treated as a string as it can contain numbers of letters, the second ‘column’ will always be numbers.
Anyone able to help me figure this out?
I think I just need to edit the sql query to cast them as a string but I’m not sure.
CSV READER EDIT:
namespace CSV
{
public class csvReader
{
public DataTable read(string strFileName)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=NO;FMT=Delimited\"");
conn.Open();
string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(strQuery, conn);
DataSet ds = new DataSet("CSV File");
adapter.Fill(ds);
return ds.Tables[0];
}
public DataTable read(string strFileName, bool firstRowHeaders)
{
string hdr = "NO";
if (firstRowHeaders) { hdr = "YES"; }
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=" + hdr + ";FMT=Delimited\"");
conn.Open();
string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(strQuery, conn);
DataSet ds = new DataSet("CSV File");
adapter.Fill(ds);
return ds.Tables[0];
}
}
}
The problem is obviously in the CsvReader class. Since you didn’t attached its source code it is hard to know why it is not filling that datatable with the content and the best I can do is guess.
I will try to help you by suggesting you to use this csv reader from codeproject:
http://www.codeproject.com/Articles/86973/C-CSV-Reader-and-Writer
you will not need to work with data tables as it enables you to iterate over the file rows using simple while loop. Your code will look like: