I have written the below code to count the number of Month-Year combination present in the excel file. when i use the original file, it works fine. But the moment i add one more Month-Year combination , it doesnt work. Like in the below screenshot, the total
number of month-Year Column is 2 and Message Box is also showing 2. But the moment i add one more Month-Year combination say Apr-15, the Message Box doesnt pop up.
![]()
while (dt.Rows[InventoryRow][MonthColNum].ToString().Trim() !="")
{
MonthColNum++;
}
MessageBox.Show("Month
Col Number "+MonthColNum.ToString());
Message Box shows the Month Col Number when i use the original file. But when i add one more Month-Year, it doesnt show the Message.
Could any one please tell me why it is happening?
Full Code:
/* Microsoft SQL Server Integration Services Script Component
* Write scripts using Microsoft Visual C# 2008.
* ScriptMain is the entry point class of the script.*/
using
System;
using
System.Data;
using
Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using
Microsoft.SqlServer.Dts.Runtime.Wrapper;
using
SC_8f65b1fd232e416084be524f330491a4.csproj;
using
System.Text;
using
System.Data.OleDb;
using
System.Windows.Forms;
using
System.Collections.Generic;
[Microsoft.SqlServer.Dts.Pipeline.
SSISScriptComponentEntryPointAttribute]
public
classScriptMain:UserComponent
{
DataTabledt;
conststringDelimeter =" ";
intInventoryRow = 3;
intColumnOffset = 0;
publicoverridevoidPreExecute()
{
base.PreExecute();
}
publicoverridevoidPostExecute()
{
base.PostExecute();
}
publicoverridevoidCreateNewOutputRows()
{
try
{
dt = LoadWorkbook(Variables.VarExcelFileName,
"Sheet1");
intMonthColNum = 0;
while(dt.Rows[InventoryRow][MonthColNum].ToString().Trim() !="")
{
MonthColNum++;
}
MessageBox.Show("Month
Col Number "+MonthColNum.ToString());
stringDate;
catch(Exceptionex)
{
}
}
publicstaticDataTableLoadWorkbook(stringFileName,stringSheetName)
{
stringconnString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ FileName +";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\"";
//string connString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(FileName) + ";Extended
Properties=Excel 8.0";
// Create the connection object
OleDbConnectionoledbConn =newOleDbConnection(connString);
try
{
// Create a DataSet which will hold the data extracted from the worksheet.
DataSetds =newDataSet();
// Open connection
oledbConn.Open();
DataTableobjSheetNames = oledbConn.GetSchema("Tables");
OleDbCommandcmd;
if(SheetName ==null&& objSheetNames.Rows.Count > 0)
{
// Create OleDbCommand object and select data from worksheet Sheet1
cmd =
newOleDbCommand("SELECT
* FROM ["+ objSheetNames.Rows[0][2].ToString() +"]", oledbConn);
}
else
{
cmd =
newOleDbCommand("SELECT
* FROM ["+ SheetName +"$]", oledbConn);
}
// Create new OleDbDataAdapter
OleDbDataAdapteroleda =newOleDbDataAdapter();
oleda.SelectCommand = cmd;
// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(ds,
"LabOrder");
// Bind the data to the GridView
returnds.Tables[0];
}
finally
{
// Close connection
oledbConn.Close();
}
}
privatestaticstring[] _InvalidChars = {",","'","\""};
publicstaticstringSanitizeInput(stringvalue)
{
returnSanitizeInput(value, _InvalidChars);
}
publicstaticstringSanitizeInput(stringvalue,string[] InvalidChars)
{
value = value.Trim();
foreach(stringInvalidCharinInvalidChars)
{
value = value.Replace(InvalidChar,
"");
}
returnvalue;
//IEnumerable<char> chars = InvalidChars.SelectMany(x => x);
//return new string(value.Where(c => !chars.Contains(c)).ToArray());
}
}
}