I have imports.aspx
with an ASP:FileUpload
function.
In my code-behind I have this:
SEPTA_DSTableAdapters.ServiceTBLTableAdapter sta = new SEPTA_DSTableAdapters.ServiceTBLTableAdapter();
SEPTA_DSTableAdapters.RoutesTBLTableAdapter rta = new SEPTA_DSTableAdapters.RoutesTBLTableAdapter();
protected void Page_Load(object sender, EventArgs e)
{
ServiceDDL.DataBind();
}
protected void Submit_Click(object sender, ImageClickEventArgs e)
{
if (ImportRoutes.HasFile)
{
//Parse CSV
StreamReader reader = new StreamReader(ImportRoutes.FileContent);
var pathOfCsvFile = reader;
var adapter = new GenericParsing.GenericParserAdapter(pathOfCsvFile);
DataTable data = adapter.GetDataTable();
foreach (DataRow row in data.Rows)
{
int insertData = Convert.ToInt32(rta.InsertRoutes(Convert.ToInt32(row["RouteID"]), ServiceDDL.SelectedValue, row["ShortName"].ToString(), row["LongName"].ToString(), row["Type"].ToString(), row["Url"].ToString()));
}
}
}
Here is a sample of the code I am importing:
route_id,route_short_name,route_long_name,route_desc,agency_id,route_type,route_color,route_text_color,route_url
AIR,AIR,Airport Line, ,SEPTA,2,44697D,FFFFFF,
CHE,CHE,Chestnut Hill East, ,SEPTA,2,44697D,FFFFFF,
CHW,CHW,Chestnut Hill West, ,SEPTA,2,44697D,FFFFFF,
CYN,CYN,Cynwyd, ,SEPTA,2,44697D,FFFFFF,
And my Database columns are:
RouteID
Category
ShortName
LongName
Type
Url
Here is my Insert query:
INSERT INTO [dbo].[RoutesTBL] ([RouteID], [Category], [ShortName], [LongName], [Type], [Url]) VALUES (@RouteID, @Category, @ShortName, @LongName, @Type, @Url)
Here is the StackTrace
[ArgumentException: Column 'RouteID' does not belong to table .]
System.Data.DataRow.GetDataColumn(String columnName) +1775301
System.Data.DataRow.get_Item(String columnName) +13
Import.Submit_Click(Object sender, ImageClickEventArgs e) in c:\Documents and Settings\abpa\Desktop\ASP\SEPTAWeb\Import.aspx.cs:32
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +108
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +118
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Now, I have two questions.
I am getting a
row does not exist
error on theint insertData
portion, how can I fix that?The first row of the file is clearly the column names to the CSV, but when inserting I need this row skipped.
Thank in advance all.