System.Data.OleDb.OleDbException(0x80004005): Unspecified Error - c#

Through a series of loops and Database calls (Select statements only) saved as datatables I am generating a .txt file with the values. It runs fine for a little while (19 rows...19 full loop cycles) then I get a runtime exception being thrown by connection.Open()
I have the entire loop inside a try catch block in order to catch the exception and produce the message then 2 blank lines then the stack trace.
I have tried to read this and figure out what to do but I am a bit of a Novice when it comes to DB connections. I have looked elsewhere but do not seem to find a question that quite fits my situation.
FYI: C# 4.0 Windows Form Application, Access DB
I am hoping to find some suggestions on where to begin looking. I am positive that my connection is closed when this error is thrown due to the validation i implemented as shown here:
internal IDbConnection GetConnection()
{
try
{
var connection = _assemblyProvider.Factory.CreateConnection();
connection.ConnectionString = _connectionString;
_connectionState = connection.State.ToString();
if (_connectionState == "Open")
GetConnection();
else
{
connection.Open();
}
return connection;
}
catch (Exception exept)
{
throw new Exception(exept.ToString() + "\n\n" + exept.StackTrace.ToString());
}
}
This method is being called from here:
public DataTable ExecuteDataTable(string commandText, string tableName, DbParameterCollection paramCollection, CommandType commandType)
{
DataTable dtReturn;
IDbConnection connection = null;
try
{
connection = _connectionManager.GetConnection();
dtReturn = _dbAdapterManager.GetDataTable(commandText, paramCollection, connection, tableName, commandType);
}
finally
{
if (connection != null)
{
connection.Close();
connection.Dispose();
}
}
return dtReturn;
}
public DataTable ExecuteDataTable(string commandText, string tableName, CommandType commandType)
{
return ExecuteDataTable(commandText, tableName, new DbParameterCollection(), commandType);
}
public DataTable ExecuteDataTable(string commandText)
{
return ExecuteDataTable(commandText, string.Empty, CommandType.Text);
}
and
//read from DB using a SQL statement and return a DataTable
internal static DataTable readDB(string SQL)
{
var dbHelper = new DbHelper();
using (IDbConnection connection = dbHelper.GetConnObject())
{
return dbHelper.ExecuteDataTable(SQL);
}
}
Here is the loop (its kinda long and could probably be done better but I just want to find why its breaking after its worked several times)
The exception is thrown from the line that Reads:
DataTable iRecNum2ClaimRecNumFromClaim = dbConnect.readDB(SQLString);
inside this:
SQLString = "SELECT * FROM Claim WHERE ClaimStatus <> 1";
DataTable allRecsFromClaimNotStatus1 = dbConnect.readDB(SQLString);
if (allRecsFromClaimNotStatus1.Rows.Count == 0)
return;
else
{
string path = txtExtractFileLocation.Text;
if (txtExtractFileLocation.Text.Substring(txtExtractFileLocation.Text.Length - 2) == "\\\\")
{
path = path.Substring(0, path.Length - 1);
}
if (path.Substring(path.Length - 1) == "\\")
path += "DI_Extract.txt";
else
path += #"\DI_Extract.txt";
using (StreamWriter sw = new StreamWriter(#path))
{
for (int i = 0; i < allRecsFromClaimNotStatus1.Rows.Count; i++)
{
rNum = allRecsFromClaimNotStatus1.Rows[i][2].ToString().Trim();//Claim.InsuredRecNum
SQLString = "SELECT * FROM Insured WHERE RecNum = " + rNum;
DataTable allInsuredByRecNum = dbConnect.readDB(SQLString);
lossDate = allRecsFromClaimNotStatus1.Rows[i][11].ToString().Trim();//Claim.LossDate
lossDate = (Convert.ToDateTime(lossDate)).Date.ToString("MM/dd/yyyy");
reportedDate = allRecsFromClaimNotStatus1.Rows[i][9].ToString().Trim();//Claim.ReportedDate
reportedDate = (Convert.ToDateTime(reportedDate)).Date.ToString("MM/dd/yyyy");
claim = allRecsFromClaimNotStatus1.Rows[i][0].ToString().Trim();//Claim.ClaimNumber
if (chkIncludePaymentsForCurrentMonth.Checked == true)
{
direct = allRecsFromClaimNotStatus1.Rows[i][4].ToString().Trim();//Claim.DirectReserve
WP = allRecsFromClaimNotStatus1.Rows[i][5].ToString().Trim();//Claim.WPReserve
ceded = allRecsFromClaimNotStatus1.Rows[i][6].ToString().Trim();//Claim.CededReserve
}
else
{
direct = allRecsFromClaimNotStatus1.Rows[i][29].ToString().Trim();//Claim.MonthEndDirect
WP = allRecsFromClaimNotStatus1.Rows[i][30].ToString().Trim();//Claim.MonthEndWP
ceded = allRecsFromClaimNotStatus1.Rows[i][31].ToString().Trim();//Claim.MonthEndCeded
}
ced = Convert.ToDecimal(ceded);
wav = Convert.ToDecimal(WP);
ceded = ced.ToString("#.##");
WP = wav.ToString("#.##");
if (ceded == "")
ceded = "0";
if (WP == "")
WP = "0";
if ((allRecsFromClaimNotStatus1.Rows[i][10].ToString().Trim() != null) &&
(allRecsFromClaimNotStatus1.Rows[i][10].ToString().Trim() != ""))//Claim.WaiverDate
{
onWaiver = "YES";
}
else
{
onWaiver = "NO";
}
reinsPreNotice = "NO";
reinsCeded = "NO";
switch (allRecsFromClaimNotStatus1.Rows[i][7].ToString().Trim())//Claim.CededPre
{
case "1":
{
reinsPreNotice = "YES";
break;
}
case "2":
{
reinsCeded = "YES";
break;
}
}//end switch
state = allRecsFromClaimNotStatus1.Rows[i][8].ToString().Trim();//Claim.LossState
lName = allInsuredByRecNum.Rows[0][1].ToString().Trim();//Insured.LastName
fName = allInsuredByRecNum.Rows[0][0].ToString().Trim();//Insured.FirstName
mi = allInsuredByRecNum.Rows[0][2].ToString().Trim();//Insured.MI
policy = allInsuredByRecNum.Rows[0][43].ToString().Trim();//Insured.PolicyNumber
DOB = allInsuredByRecNum.Rows[0][10].ToString().Trim();//Insured.DOB
DOB = (Convert.ToDateTime(DOB)).Date.ToString("MM/dd/yyyy");
age = allInsuredByRecNum.Rows[0][11].ToString().Trim();//Insured.TrueAge
issueAge = calculateAge(Convert.ToDateTime(allInsuredByRecNum.Rows[0][10].ToString().Trim()), //Insured.DOB
Convert.ToDateTime(allInsuredByRecNum.Rows[0][45].ToString().Trim()));//Insured.EffectiveDate
SQLString = "SELECT InsuredRecNum, RecNum FROM Claim WHERE InsuredRecNum = " + rNum;
DataTable iRecNum2ClaimRecNumFromClaim = dbConnect.readDB(SQLString);
rNum = iRecNum2ClaimRecNumFromClaim.Rows[0][1].ToString().Trim();
issueDate = allInsuredByRecNum.Rows[0][45].ToString().Trim();//Insured.EffectiveDate
issueDate = (Convert.ToDateTime(issueDate)).Date.ToString("MM/dd/yyyy");
sex = allInsuredByRecNum.Rows[0][13].ToString().Trim();//Insured.Gender
planCode = allInsuredByRecNum.Rows[0][44].ToString().Trim();//Insured.PlanMnemonic
issueAmt = allInsuredByRecNum.Rows[0][49].ToString().Trim();//Insured.BenefitAmount (Monthly Benefit Amount before Offset)
benefitPeriod = allInsuredByRecNum.Rows[0][50].ToString().Trim();//Insured.BenefitPeriod
if (allInsuredByRecNum.Rows[0][54].ToString().Trim().Length == 2)//Insured.EliminationPeriod
eliminationPeriod = "0" + allInsuredByRecNum.Rows[0][54].ToString().Trim();
else
eliminationPeriod = allInsuredByRecNum.Rows[0][54].ToString().Trim();
premiumAmount = allInsuredByRecNum.Rows[0][48].ToString().Trim();//Insured.AnnualPremium
occupationClass = allInsuredByRecNum.Rows[0][55].ToString().Trim();//Insured.OccupationClass
//select only status = EXEC (0)
SQLString = "SELECT * FROM Offset WHERE ClaimRecNum = " + rNum + " AND Status = 0";
DataTable allOffsetByClaimRecNumAndStatus0 = dbConnect.readDB(SQLString);
offsetAmt = 0;
dblSTDOffsetAmount = 0;
dblRecOffsetAmount = 0;
RECOffsetOcc = "0";
RECOffsetExecuted = "0";
int offsetCount = allOffsetByClaimRecNumAndStatus0.Rows.Count;
if (offsetCount != 0)
{
for (int j = 0; j < offsetCount; j++)
{
//accumulate standard offset (STD) and Recovery offset (REC)
if (allOffsetByClaimRecNumAndStatus0.Rows[0][1].ToString().Trim() == "0")//Offset.Type
{
//Standard Type
dblSTDOffsetAmount += Convert.ToDouble(allOffsetByClaimRecNumAndStatus0.Rows[j][4].ToString().Trim());//Offset.Amount
}
else
{
//Recovery type
dblRecOffsetAmount = Convert.ToDouble(allOffsetByClaimRecNumAndStatus0.Rows[j][4].ToString().Trim());//Offset.Amount
RECOffsetOcc = allOffsetByClaimRecNumAndStatus0.Rows[j][5].ToString().Trim();//Offset.Occurance
RECOffsetExecuted = allOffsetByClaimRecNumAndStatus0.Rows[j][6].ToString().Trim();//Offset.Executed
}//end if
}//end for loop
}//end if
STDOffsetAmount = dblSTDOffsetAmount.ToString();
RECOffsetAmount = dblRecOffsetAmount.ToString();
if (chkIncludePaymentsForCurrentMonth.Checked == true)
SQLString = "SELECT * FROM Payment WHERE InsuredRecNum = " + rNum + " AND IssueDate >= #01/01/" + DateTime.Today.Date.Year + "# AND IssueDate <= #" + DateTime.Today.Date.ToShortDateString() + "#";
else
SQLString = "SELECT * FROM Payment WHERE InsuredRecNum = " + rNum + " AND IssueDate >= #01/01/" + endDate.Substring(endDate.Length - 4) + "# AND IssueDate <= #" + Convert.ToDateTime(endDate).Date.ToShortDateString() + "#";
DataTable allPaymentByIRecNumAndIssDateInRange = dbConnect.readDB(SQLString);
YTDPmt = 0;
if (allPaymentByIRecNumAndIssDateInRange.Rows.Count == 0)
YTDPmt = 0;
else
{
int paymentCount = allPaymentByIRecNumAndIssDateInRange.Rows.Count;
double issAmt;
for (int k = 0; k < paymentCount; k++)
{
issAmt = Convert.ToDouble(allPaymentByIRecNumAndIssDateInRange.Rows[0][30].ToString().Trim());//Payment.IssueAmount
YTDPmt += issAmt;
}// end loop
}//end if
YTDPmts = YTDPmt.ToString();
if (chkIncludePaymentsForCurrentMonth.Checked == true)
SQLString = "SELECT * FROM Payment WHERE ClaimRecNum = " + rNum;
else
SQLString = "SELECT * FROM Payment WHERE ClaimRecNum = " + rNum + " AND IssueDate <= #" + Convert.ToDateTime(endDate).Date.ToShortDateString() + "#";
DataTable allPaymentByRNum = dbConnect.readDB(SQLString);
totalPmt = 0;
if (allPaymentByRNum.Rows.Count == 0)
totalPmt = 0;
else
{
double issAmt = Convert.ToDouble(allPaymentByRNum.Rows[0][30].ToString().Trim());
for (int m = 0; m < allPaymentByRNum.Rows.Count; m++)
{
totalPmt += issAmt;
}
}
allPmts = totalPmt.ToString();
//set spacing for output
string block1 = policy + claim + planCode;
block1 = setSpacing(block1, 28);
string block2 = setSpacing(benefitPeriod, 3) + eliminationPeriod + occupationClass;
block2 = setSpacing(block2, 11);
issueAmt = setSpacing(issueAmt, 8);
STDOffsetAmount = setSpacing(STDOffsetAmount, 8);
RECOffsetAmount = setSpacing(RECOffsetAmount, 8);
RECOffsetOcc = setSpacing(RECOffsetOcc, 3);
RECOffsetExecuted = setSpacing(RECOffsetExecuted, 3);
string block3 = lossDate + age;
block3 = setSpacing(block3, 13);
issueAge = setSpacing(issueAge, 3);
string block4 = issueDate + DOB + sex + onWaiver + premiumAmount;
block4 = setSpacing(block4, 32);
reinsPreNotice = setSpacing(reinsPreNotice, 3);
reinsCeded = setSpacing(reinsCeded, 4);
double ap = Convert.ToDouble(allPmts);
allPmts = ap.ToString("#.#");
allPmts = setSpacing(allPmts, 8);
YTDPmts = setSpacing(YTDPmts, 8);
lName = setSpacing(lName, 19);
fName = fName + " " + mi;
fName = setSpacing(fName, 20);
string block5 = state + direct;
block5 = setSpacing(block5, 10);
ceded = setSpacing(ceded, 8);
WP = setSpacing(WP, 8);
reportedDate = setSpacing(reportedDate, 10);
//save row data for text file
dataOutput = (block1 + block2 + issueAmt + STDOffsetAmount + RECOffsetAmount + RECOffsetOcc + RECOffsetExecuted +
block3 + issueAge + block4 + reinsPreNotice + reinsCeded + allPmts + YTDPmts + lName + fName +
block5 + ceded + WP + reportedDate);
//Write to the output record DI_Extract.txt
sw.WriteLine(dataOutput);
counter++;
pbrRecordsProcessed.Value = counter;
}//end for loop
}//end streamwriter
}//end if

After looking deeper into the code I realized that the connection was trying to open 3 times before it closed. Not sure why I was not getting exceptions all the time but correcting this issue not only sped up the application tremendously it cleared the exception.

Related

How to scan a file while uploading using Kendo UI in ASP.NET MVC 4

How can I scan (Symantec) a file for a virus while uploading, using Kendo UI Upload?
This question was answered on the Telerik forum:
The Kendo UI Upload does not include any file scanning capabilities
and frankly speaking, we have no intentions with this regard for the
time being. You can scan files via custom implementation or a tool
after saving them on the server, in the same fashion as you would do
that with a plain element.
You'll need to implement general server-side scanning of files, Kendo UI doesn't support it.
using com.symantec.scanengine.api;
public ActionResult Save(IEnumerable<HttpPostedFileBase> files)
{
int bresult = 0;
string sReturn = string.Empty;
bresult = SaveFiles(files);
if (bresult == -2)
{
return Content("Corrupted");
}
else if (bresult == -1)
{
return Content("Incorrect");
}
else
{
return Json("", JsonRequestBehavior.AllowGet);
}
}
public int SaveFiles(IEnumerable<HttpPostedFileBase> files)
{
int bresult = 0;
string sReturn = string.Empty;
if (ModelState.IsValid)
{
string sMessage = null;
try
{
foreach (var file in files)
{
// Some browsers send file names with full path. We only care about the file name.
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = System.IO.Path.GetFileName(file.FileName);
string fileExt = System.IO.Path.GetExtension(fileName);
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
bool IsValid = true;
if (IsValid)
{
file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
string scanIP = string.Empty;
int scanPort = Convert.ToInt16(ConfigurationManager.AppSettings["scanPort"]);
scanIP = ConfigurationManager.AppSettings["scanIPFromConfig"];
int scanResult = ScanUploads(scanIP, scanPort, fileName, fileBytes);
if (scanResult == -1)
{
bresult = -2;
}
else
{
//your logic to remove files
}
}
}
}
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message.ToString());
obj.ExceptionLogger(ex);
bresult = -1;
}
}
return bresult;
}
public int ScanUploads(string ScanIP, int scanPort, string valuetoscan, byte[] fileStream)
{
//try
//{
List<ScanEngineInfo> scanEnginesForScanning = new List<ScanEngineInfo>();
scanEnginesForScanning.Add(new ScanEngineInfo(ScanIP, scanPort));
//ScanEngineInfo scanEnginesForScanning = new ScanEngineInfo(ScanIP, scanPort);
ScanRequestManager requestManagerObj = new ScanRequestManager();
requestManagerObj.PrepareForScan(scanEnginesForScanning, 20000, 20);
//string scanPolicy = "policy";
string setScanPolicy = "DEFAULT";
dynamic scPolicy = (Policy)Enum.Parse(typeof(Policy), setScanPolicy);
StreamScanRequest testobjtoscan = requestManagerObj.CreateStreamScanRequest(scPolicy);
// byte[] array1 = null;
// int i = 0;
MemoryStream iStream = new MemoryStream();
ScanResult scanResult = default(ScanResult);
string scanresultClean = null;
int scanresultINFECTED = 0;
string scanresultcount = null;
string scanfilestatus = null;
iStream.Write(fileStream, 0, fileStream.Length);
testobjtoscan.Start(valuetoscan, "ScanFile");
testobjtoscan.Send(fileStream);
try
{
scanResult = testobjtoscan.Finish(iStream);
}
catch (Exception e)
{
throw;
}
scanfilestatus = Convert.ToString(scanResult.fileStatus.ToString());
scanresultcount = scanResult.threat.ToString();
scanresultClean = scanResult.fileStatus.ToString();
scanresultINFECTED = scanResult.totalInfection;
if (scanfilestatus != "CLEAN")
{
string errorMessage;
if(scanfilestatus == null)
{
errorMessage = "File Scan Status: null" + " Total Infection: " + scanResult.totalInfection + " connTriesInfo: ";
}
else{
errorMessage = "File Scan Status: " + scanfilestatus + " Total Infection: " + scanResult.totalInfection + " connTriesInfo: ";
}
foreach (var conninfo in scanResult.connTriesInfo)
{
errorMessage += " port: " + conninfo.port.ToString() + " problem encountered:" + conninfo.problemEncountered.ToString() + " scan host" + conninfo.scanHost.ToString();
}
iStream.Dispose();
Exception ex = new Exception("Symantec Virus Scan prevented file from being uploaded " + errorMessage);
obj.ExceptionLogger(ex);
return -1;
}
else
return 0;
}
public int ScanStream(string scanServer, int scanPort, string fileName, byte[] fileStream)
{
byte[] buffer = new byte[1024];
int iRx;
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string retDesc=string.Empty;
try
{
if (scanPort == 0)
scanPort = 1344;
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(scanServer);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, scanPort);
soc.Connect(remoteEndPoint);
if (soc.Connected)
{
string cmd = "RESPMOD icap://" + scanServer + ":" + scanPort + "/AVSCAN ICAP/1.0\n";
cmd = cmd + "Host: " + scanServer + ":" + scanPort + "\n";
cmd = cmd + "Allow: 204\n";
cmd = cmd + "Encapsulated: req-hdr=0, res-hdr=84, res-body=131\n";
cmd = cmd + "\n";
cmd = cmd + "GET http://”" + scanServer + "/" + fileName + " HTTP/1.1\n";
cmd = cmd + "Host: " + scanServer + "\n";
cmd = cmd + "\n";
cmd = cmd + "HTTP/1.1 200 OK\n";
cmd = cmd + "Transfer-Encoding: chunked\n";
cmd = cmd + "\n";
cmd = cmd + String.Format("{0:X2}", fileStream.Length) + "\n";
soc.Send(System.Text.Encoding.ASCII.GetBytes(cmd));
soc.Send(fileStream);
cmd = "\n";
cmd = cmd + "\n";
cmd = cmd + "0\n";
cmd = cmd + "\n";
soc.Send(System.Text.Encoding.ASCII.GetBytes(cmd));
while ((soc.Connected) && ((iRx = soc.Receive(buffer)) > 0))
{
char[] chars = new char[iRx];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
retDesc = retDesc + szData;
}
soc.Close();
if (retDesc.Contains("X-Violations-Found:"))
return -1;
else
return 0;
}
else
return -1;
}
catch (Exception e)
{
if (soc != null)
soc.Close();
retDesc = e.Message;
obj.ExceptionLogger(e);
return -1;
}
}

Uploading a file to folder

With the code below I am able to save files to folder.
My problem is only two upload fields are mandatory and the remaining three are not. The code works if all the upload fields have a files selected otherswise its throws a NullReferenceException.
if (AnnualReport != null || ProjectReports != null || Publications != null || Other != null || RegistDoc != null) {
int filesize = AnnualReport.PostedFile.ContentLength;
int filesizeP = ProjectReports.PostedFile.ContentLength;
int filesizePub = Publications.PostedFile.ContentLength;
int filesizeOther = Other.PostedFile.ContentLength;
int filesizeReg = RegistDoc.PostedFile.ContentLength;
if (filesize > 2097152 && filesizeP > 2097152 && filesizePub > 1048576 && filesizeOther > 1048576 && filesizeReg > 1048576) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Maximum File size For Annual/Project reports is 1.5MB and for the Publications/Other Attachemnets is 1MB');", true);
} else {
const string ReportDirectory = "REPORTS/";
//Other Document
string OtherPath = ReportDirectory + Other.FileName;
string fileNameWithoutExtensionOther = System.IO.Path.GetFileNameWithoutExtension(Other.FileName);
int iterationOther = 1;
while (System.IO.File.Exists(Server.MapPath(OtherPath))) {
OtherPath = string.Concat(ReportDirectory, fileNameWithoutExtensionOther, "-", iterationOther, ".pdf");
iterationOther++;
}
//Registration Document
string RigisDocPath = ReportDirectory + RegistDoc.FileName;
string fileNameWithoutExtensionRegis = System.IO.Path.GetFileNameWithoutExtension(RegistDoc.FileName);
int iterationRE = 1;
while (System.IO.File.Exists(Server.MapPath(RigisDocPath))) {
RigisDocPath = string.Concat(ReportDirectory, fileNameWithoutExtensionRegis, "-", iterationRE, ".pdf");
iterationRE++;
}
//Annual Reports
string ReportPath = ReportDirectory + AnnualReport.FileName;
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(AnnualReport.FileName);
int iteration = 1;
while (System.IO.File.Exists(Server.MapPath(ReportPath))) {
ReportPath = string.Concat(ReportDirectory, fileNameWithoutExtension, "-", iteration, ".pdf");
iteration++;
}
//Project Report
string ProjecttPath = ReportDirectory + ProjectReports.FileName;
string fileNameWithoutExtensionP = System.IO.Path.GetFileNameWithoutExtension(ProjectReports.FileName);
int iterationP = 1;
while (System.IO.File.Exists(Server.MapPath(ProjecttPath))) {
ProjecttPath = string.Concat(ReportDirectory, fileNameWithoutExtensionP, "-", iterationP, ".pdf");
iterationP++;
}
//publication
string publicationPath = ReportDirectory + Publications.FileName;
string fileNameWithoutExtensionPub = System.IO.Path.GetFileNameWithoutExtension(Publications.FileName);
int iterationPub = 1;
while (System.IO.File.Exists(Server.MapPath(publicationPath))) {
publicationPath = string.Concat(ReportDirectory, fileNameWithoutExtensionPub, "-", iterationPub, ".pdf");
iterationPub++;
}
ProjectReports.SaveAs(Server.MapPath(ProjecttPath));
AnnualReport.SaveAs(Server.MapPath(ReportPath));
Publications.SaveAs(Server.MapPath(publicationPath));
RegistDoc.SaveAs(Server.MapPath(RigisDocPath));
Other.SaveAs(Server.MapPath(OtherPath));
The code you posted is very poorly formated. However, the solution to your immediate problem is to move the null checks down to each individual document.
Instead of doing a huge if line (which has questionable logic, as it only checks if ANY of the documents were uploaded)
You can just check if the required documents are present. (looking at your exising code, present means document name object is not null)
If not, throw an error.
If they are, then proceed with the rest of the code, but wrap the individual processing of optional documents in their own null check if-s.
ie.
if (AnnualReport != null) {
//the block that does stuff with the anual report object
}
I did beak down the code into diferent methods like #irreal suggested, like below;
public void PublicationReporting() {
//connection for the datareader
string csoWConn = ConfigurationManager.ConnectionStrings["RegisterCon"].ToString();
SqlConnection csoW_connection = new SqlConnection(csoWConn);
string database = csoW_connection.DataSource.ToString();
csoW_connection.Open();
if (Publications == null)
{
Publications.Dispose();
////
String MyString = #"UPDATE tb_Quadrennial_Report SET PublicationsPath='' WHERE Org_ID = '" + Accrediated_Orgs.SelectedValue + "'";
SqlCommand MyCmd = new SqlCommand(MyString, csoW_connection);
int LastInsertedRecordID;
LastInsertedRecordID = Convert.ToInt32(MyCmd.ExecuteScalar());
}
else
{
int filesizeP = Publications.PostedFile.ContentLength;
if (filesizeP > 2097152)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Maximum File size For Publication is 2.0 MB');", true);
}
else
{
const string ReportDirectory = "REPORTS/";
//publication
string publicationPath = ReportDirectory + Publications.FileName;
string fileNameWithoutExtensionPub = System.IO.Path.GetFileNameWithoutExtension(Publications.FileName);
int iteration = 1; while (System.IO.File.Exists(Server.MapPath(publicationPath)))
{
publicationPath = string.Concat(ReportDirectory, fileNameWithoutExtensionPub, "-", iteration, ".pdf");
iteration++;
}
Publications.SaveAs(Server.MapPath(publicationPath));
String MyString = #"UPDATE tb_Quadrennial_Report SET PublicationsPath='" + publicationPath + "' WHERE Org_ID = '" + Accrediated_Orgs.SelectedValue + "'";
SqlCommand MyCmd = new SqlCommand(MyString, csoW_connection);
int LastInsertedRecordID;
LastInsertedRecordID = Convert.ToInt32(MyCmd.ExecuteScalar());
}
}
}
I then called it o the click event
try{
PublicationReporting();
}
catch (Exception ex)
{
pgError.Text = "Publication Exception Message: " + ex.Message;
}
finally
{
csoW_connection.Close();
}
From here it was pretty easy to figure out the problem.
I just needed to dispose the content in the upload field if no file was selected like this
public void PublicationReporting() {
//connection for the datareader
string csoWConn = ConfigurationManager.ConnectionStrings["RegisterCon"].ToString();
SqlConnection csoW_connection = new SqlConnection(csoWConn);
string database = csoW_connection.DataSource.ToString();
csoW_connection.Open();
if (Publications == null)
{
Publications.Dispose();
////
String MyString = #"UPDATE tb_Quadrennial_Report SET PublicationsPath='' WHERE Org_ID = '" + Accrediated_Orgs.SelectedValue + "'";
SqlCommand MyCmd = new SqlCommand(MyString, csoW_connection);
int LastInsertedRecordID;
LastInsertedRecordID = Convert.ToInt32(MyCmd.ExecuteScalar());
}
else{
//program continues}

Dynamic Crystal Report using Dataset, Microsoft Access Database

I'm trying to get a report from a custom query accessing to a Microsoft Access Database filling a Dataset and then a CrystalReportViwer but I just got an empty report with the column names.
This is my connection:
public String ConectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data";
public OleDbConnection miconexion;
public Int32 retorna = 0;
public Int32 conectar(String location){
try
{
ConectionString += #" Source="+location;
miconexion = new OleDbConnection(ConectionString);
//Open Database Conexion
miconexion.Open();
retorna = 1;
}
catch (OleDbException ex){
retorna = 0;
MessageBox.Show("Database Error: "+ex.ToString());
}
return retorna;
}
public void desconectar() {
if(miconexion.State == ConnectionState.Open){
miconexion.Close();
}
}
Method that process the sql //---------------------------------
//txt_CustomConsult is the textbox that contains the custom query.
public String ExecuteSQl()
{
string sql = null;
string inSql = null;
string firstPart = null;
string LastPart = null;
int SelectStart = 0;
int fromStart = 0;
string[] fields = null;
string[] sep = { "," };
int i = 0;
TextObject MyText;
inSql = txt_CustomConsult.Text;
inSql = inSql.ToUpper();
SelectStart = inSql.IndexOf("SELECT");
fromStart = inSql.IndexOf("FROM");
SelectStart = SelectStart + 6;
firstPart = inSql.Substring(SelectStart, (fromStart - SelectStart));
LastPart = inSql.Substring(fromStart, inSql.Length - fromStart);
fields = firstPart.Split(',');
firstPart = "";
for (i = 0; i <= fields.Length - 1; i++) {
if (i > 0)
{
firstPart = firstPart + ", " + fields[i].ToString() + " AS COLUMN" + (i + 1);
firstPart.Trim();
MyText = (TextObject)ObjRep.ReportDefinition.ReportObjects[i + 1];
MyText.Text = fields[i].ToString();
}
else {
firstPart = firstPart + fields[i].ToString() + " AS COLUMN"+(i+1);
firstPart.Trim();
MyText = (TextObject)ObjRep.ReportDefinition.ReportObjects[i + 1];
MyText.Text = fields[i].ToString();
}
}
sql = "SELECT "+firstPart+" "+LastPart;
return sql;
}
// ---------------------------------------------------
Method that exceute report. lstbx_Tablas is the ListBox that contains all tables,
I got all the tables but report doesn't work.
public void DynamicRep() {
try {
//Windows Form where the rpt is located
ReporteResult mirepres = new ReporteResult();
//Connection
conexion miconect = new conexion();
miconect.conectar(Environment.GetEnvironmentVariable("dbUbicacion"));
String sql = ExecuteSQl();
OleDbDataAdapter dscm = new OleDbDataAdapter(sql,miconect.miconexion);
MisConsultas Dataset1 = new MisConsultas();
dscm.Fill(Dataset1,lstbx_Tablas.SelectedItem.ToString());
ObjRep.SetDataSource(Dataset1.Tables[0]);
mirepres.crv_Reporte.ReportSource = ObjRep;
mirepres.crv_Reporte.Refresh();
mirepres.Show();
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}

ArgumentOutOfRangeException was unhandled?

Here is my case: i can retrieved the data from the database, but when i run the program and filled up the Quantity column (on the third line), the error says: index was out of range. Anyone know why is this happen? (When i tried to fill the Quantity column (on third line), the error appeared) <-- shown in the picture below.
Here is the images of my program:
Here is the images of where the error is pointed:
Here is the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;
namespace Sell_System
{
public partial class Form2 : Form
{
string connectionString = (#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;");
private Form1 firstForm;
private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>();
private List<List<TextBox>> textBoxAllTotalContainer = new List<List<TextBox>>();
public Form2()
{
InitializeComponent();
}
public Form2(Form1 firstForm)
: this()
{
this.firstForm = firstForm;
}
private void Form2_Load(object sender, EventArgs e)
{
UpdateTextPosition();
OleDbDataReader dReader;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT [Code] FROM [Data]", conn);
dReader = cmd.ExecuteReader();
AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();
while (dReader.Read())
{
string numString = dReader[0].ToString().PadLeft(4, '0');
codesCollection.Add(numString);
}
dReader.Close();
conn.Close();
if (firstForm.comboBox1.SelectedIndex == 0)
{
label1.Text = "Code:";
label1.Location = new Point(60, 125);
label2.Text = "Welcome to the Selling System.";
label2.Location = new Point(600, 0);
label3.Text = "Quantity:";
label3.Location = new Point(155, 125);
label4.Text = "Description:";
label4.Location = new Point(580, 125);
label5.Text = "Sub Total on Rp:";
label5.Location = new Point(1020, 125);
label6.Text = "Total on Rp:";
label6.Location = new Point(1210, 125);
label7.Text = "Total on Rp:";
label7.Location = new Point(1080, 580);
}
else if (firstForm.comboBox1.SelectedIndex == 1)
{
label1.Text = "Kode:";
label1.Location = new Point(60, 125);
label2.Text = "Selamat datang di Selling System.";
label2.Location = new Point(600, 0);
label3.Text = "Banyaknya:";
label3.Location = new Point(145, 125);
label4.Text = "Keterangan:";
label4.Location = new Point(580, 125);
label5.Text = "Sub Total di Rp:";
label5.Location = new Point(1020, 125);
label6.Text = "Total di Rp:";
label6.Location = new Point(1210, 125);
label7.Text = "Total di Rp:";
label7.Location = new Point(1080, 580);
}
//****TextBox for Code****
for (int y = 0; y <= 16; y++)
{
textBoxCodeContainer.Add(new List<TextBox>());
textBoxCodeContainer[0].Add(new TextBox());
textBoxCodeContainer[0][y].Size = new Size(100, 50);
textBoxCodeContainer[0][y].Location = new Point(25, 150 + (y * 25));
textBoxCodeContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest;
textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource;
textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection;
Controls.Add(textBoxCodeContainer[0][y]);
}
//****TextBox for Quantity****
for (int y = 0; y <= 16; y++)
{
textBoxQuantityContainer.Add(new List<TextBox>());
textBoxQuantityContainer[0].Add(new TextBox());
textBoxQuantityContainer[0][y].Size = new Size(100, 50);
textBoxQuantityContainer[0][y].Location = new Point(125, 150 + (y * 25));
textBoxQuantityContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
Controls.Add(textBoxQuantityContainer[0][y]);
}
//****TextBox for Description****
for (int y = 0; y <= 16; y++)
{
textBoxDescContainer.Add(new List<TextBox>());
textBoxDescContainer[0].Add(new TextBox());
textBoxDescContainer[0][y].Size = new Size(750, 50);
textBoxDescContainer[0][y].Location = new Point(225, 150 + (y * 25));
Controls.Add(textBoxDescContainer[0][y]);
}
//****TextBox for Sub Total****
for (int y = 0; y <= 16; y++)
{
textBoxSubTotalContainer.Add(new List<TextBox>());
textBoxSubTotalContainer[0].Add(new TextBox());
textBoxSubTotalContainer[0][y].Size = new Size(175, 50);
textBoxSubTotalContainer[0][y].Location = new Point(975, 150 + (y * 25));
Controls.Add(textBoxSubTotalContainer[0][y]);
}
//****TextBox for Total****
for (int y = 0; y <= 16; y++)
{
textBoxTotalContainer.Add(new List<TextBox>());
textBoxTotalContainer[0].Add(new TextBox());
textBoxTotalContainer[0][y].Size = new Size(175, 50);
textBoxTotalContainer[0][y].Location = new Point(1150, 150 + (y * 25));
textBoxTotalContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
Controls.Add(textBoxTotalContainer[0][y]);
}
//****TextBox for Total All****
textBoxAllTotalContainer.Size = new Size(175, 50);
textBoxAllTotalContainer.Location = new Point(1150, 575);
textBoxAllTotalContainer.TextChanged += new System.EventHandler(this.textBox_TextChanged);
Controls.Add(textBoxAllTotalContainer);
}
private void UpdateTextPosition()
{
Graphics g = this.CreateGraphics();
Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
String tmp = " ";
Double tmpWidth = 0;
while ((tmpWidth + widthOfASpace) < startingPoint)
{
tmp += " ";
tmpWidth += widthOfASpace;
}
this.Text = tmp + this.Text.Trim();
}
private void UpdateDatas()
{
int codeValue = 0;
int index = 0;
string query = "SELECT [Description], [Price] FROM [Data] WHERE [Code] IN (";
OleDbDataReader dReader;
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
if (int.TryParse(this.textBoxCodeContainer[0][0].Text, out codeValue))
{
query = query + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][1].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][2].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][3].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][4].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][5].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][6].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][7].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][8].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][9].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][10].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][11].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][12].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][13].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][14].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][15].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
if (int.TryParse(this.textBoxCodeContainer[0][16].Text, out codeValue))
{
query = query + "," + codeValue.ToString();
}
query = query + ")";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
dReader = cmd.ExecuteReader();
while (dReader.Read())
{
if (textBoxCodeContainer[0][index].TextLength != 0)
{
this.textBoxDescContainer[0][index].Text = dReader["Description"].ToString();
this.textBoxSubTotalContainer[0][index].Text = dReader["Price"].ToString();
}
index += 1;
}
dReader.Close();
conn.Close();
}
private void UpdatePrice()
{
if (textBoxQuantityContainer[0][0].Text == "")
{
textBoxTotalContainer[0][0].Text = "";
}
else if (textBoxQuantityContainer[0][0].Text == "1")
{
textBoxTotalContainer[0][0].Text = textBoxSubTotalContainer[0][0].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text;
}
if (textBoxQuantityContainer[0][1].Text == "")
{
textBoxTotalContainer[0][1].Text = "";
}
else if (textBoxQuantityContainer[0][1].Text == "1")
{
textBoxTotalContainer[0][1].Text = textBoxSubTotalContainer[0][1].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][1].Text;
}
if (textBoxQuantityContainer[0][2].Text == "")
{
textBoxTotalContainer[0][2].Text = "";
}
else if (textBoxQuantityContainer[0][2].Text == "1")
{
textBoxTotalContainer[0][2].Text = textBoxSubTotalContainer[0][2].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][2].Text;
}
if (textBoxQuantityContainer[0][3].Text == "")
{
textBoxTotalContainer[0][3].Text = "";
}
else if (textBoxQuantityContainer[0][3].Text == "1")
{
textBoxTotalContainer[0][3].Text = textBoxSubTotalContainer[0][3].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][3].Text;
}
if (textBoxQuantityContainer[0][4].Text == "")
{
textBoxTotalContainer[0][4].Text = "";
}
else if (textBoxQuantityContainer[0][4].Text == "1")
{
textBoxTotalContainer[0][4].Text = textBoxSubTotalContainer[0][4].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][4].Text;
}
if (textBoxQuantityContainer[0][5].Text == "")
{
textBoxTotalContainer[0][5].Text = "";
}
else if (textBoxQuantityContainer[0][5].Text == "1")
{
textBoxTotalContainer[0][5].Text = textBoxSubTotalContainer[0][5].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][5].Text;
}
if (textBoxQuantityContainer[0][6].Text == "")
{
textBoxTotalContainer[0][6].Text = "";
}
else if (textBoxQuantityContainer[0][6].Text == "1")
{
textBoxTotalContainer[0][6].Text = textBoxSubTotalContainer[0][6].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][6].Text;
}
if (textBoxQuantityContainer[0][7].Text == "")
{
textBoxTotalContainer[0][7].Text = "";
}
else if (textBoxQuantityContainer[0][7].Text == "1")
{
textBoxTotalContainer[0][7].Text = textBoxSubTotalContainer[0][7].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][7].Text;
}
if (textBoxQuantityContainer[0][8].Text == "")
{
textBoxTotalContainer[0][8].Text = "";
}
else if (textBoxQuantityContainer[0][8].Text == "1")
{
textBoxTotalContainer[0][8].Text = textBoxSubTotalContainer[0][8].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][8].Text;
}
if (textBoxQuantityContainer[0][9].Text == "")
{
textBoxTotalContainer[0][9].Text = "";
}
else if (textBoxQuantityContainer[0][9].Text == "1")
{
textBoxTotalContainer[0][9].Text = textBoxSubTotalContainer[0][9].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][9].Text;
}
if (textBoxQuantityContainer[0][10].Text == "")
{
textBoxTotalContainer[0][10].Text = "";
}
else if (textBoxQuantityContainer[0][10].Text == "1")
{
textBoxTotalContainer[0][10].Text = textBoxSubTotalContainer[0][10].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][10].Text;
}
if (textBoxQuantityContainer[0][11].Text == "")
{
textBoxTotalContainer[0][11].Text = "";
}
else if (textBoxQuantityContainer[0][11].Text == "1")
{
textBoxTotalContainer[0][11].Text = textBoxSubTotalContainer[0][11].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][11].Text;
}
if (textBoxQuantityContainer[0][12].Text == "")
{
textBoxTotalContainer[0][12].Text = "";
}
else if (textBoxQuantityContainer[0][12].Text == "1")
{
textBoxTotalContainer[0][12].Text = textBoxSubTotalContainer[0][12].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][12].Text;
}
if (textBoxQuantityContainer[0][13].Text == "")
{
textBoxTotalContainer[0][13].Text = "";
}
else if (textBoxQuantityContainer[0][13].Text == "1")
{
textBoxTotalContainer[0][13].Text = textBoxSubTotalContainer[0][13].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][13].Text;
}
if (textBoxQuantityContainer[0][14].Text == "")
{
textBoxTotalContainer[0][14].Text = "";
}
else if (textBoxQuantityContainer[0][14].Text == "1")
{
textBoxTotalContainer[0][14].Text = textBoxSubTotalContainer[0][14].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][14].Text;
}
if (textBoxQuantityContainer[0][15].Text == "")
{
textBoxTotalContainer[0][15].Text = "";
}
else if (textBoxQuantityContainer[0][15].Text == "1")
{
textBoxTotalContainer[0][15].Text = textBoxSubTotalContainer[0][15].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][15].Text;
}
if (textBoxQuantityContainer[0][16].Text == "")
{
textBoxTotalContainer[0][16].Text = "";
}
else if (textBoxQuantityContainer[0][16].Text == "1")
{
textBoxTotalContainer[0][16].Text = textBoxSubTotalContainer[0][16].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][16].Text;
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
UpdateDatas();
UpdatePrice();
}
}
}
Here is my problem: "i am confuse, when i try assign a value for "textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text", the code is working. but, when i tried "textBoxAllTotalContainer.Text = textBoxTotalContainer[0][one(1)]", the screen stucked in there (I have to stop debugging from Visual Studio)"
textBoxTotalContainer contains 17 textboxes, therefore, i used [0][0] to [0][16], and textBoxAllTotalContainer contains 1 textbox, so i just used textBoxAllTotalContainer.Text
Note: "Keyword (one), it suppose to be "1" <-- number, i wrote (one), because when i tried changed the [0][(Here supposed to be "1")], the text automatically changed to linked image"
Issue: **"When i tried the code
else if (textBoxQuantityContainer[0][0].Text == "1")
{
textBoxTotalContainer[0][0].Text = textBoxSubTotalContainer[0][0].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text;
}
the textbox for alltotalcontainer is on (Total on Rp, the textbox is on after words) in the image shown above, the textboxes for subtotal is on (Sub Total on Rp), the textboxes for totalcontainer is on (Total on Rp, the textboxes are on below words), the textboxes for quantitycontainer is on (Quantity). In code above, shown that textbox 1 on textboxtotalcontainer are same with the textbox 1 on subtotalcontainer, and alltotalcontainer is same with totalcontainer.**
But, below code it not working, and the screen stucked on there (the mouse dissappear and i cant do anything, unless i press SHIFT + F5 on Visual Studio):
else if (textBoxQuantityContainer[0][1].Text == "1")
{
textBoxTotalContainer[0][1].Text = textBoxSubTotalContainer[0][1].Text;
textBoxAllTotalContainer.Text = textBoxTotalContainer[0][1].Text;
}
Thanks
You get that error when you try to access an element in a collection that doesn't exist.
For example, if you have a list of strings...
List<string> myList = new List {"one", "two", "three"};
...and try to access the fourth element with myList[3], you'll get that error.
Either textBoxAllTotalContainer[0][2] doesn't exist, or textBoxTotalContainer[0][2]. Can't tell from the error in your picture. Place a breakpoint on that line and check both objects.
Looks like your stop condition is wrong.
you have the following code for initializing textBoxAllTotalContainer:
//****TextBox for Total All****
for (int y = 0; y <= 1; y++)
{
textBoxAllTotalContainer.Add(new List<TextBox>());
textBoxAllTotalContainer[0].Add(new TextBox());
textBoxAllTotalContainer[0][y].Size = new Size(175, 50);
textBoxAllTotalContainer[0][y].Location = new Point(1150, 575);
textBoxAllTotalContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
Controls.Add(textBoxAllTotalContainer[0][y]);
}
Note that the loop is itterating only twice.
Therefore textBoxAllTotalContainer[0][0] and textBoxAllTotalContainer[0][1] are created.
but when you try to access textBoxAllTotalContainer[0][2] you hit the exception because the location is really out of the list's range as the exception specifies.

C# 2.0 Fastest way to parse Excel spreadsheet [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reading Excel files from C#
What is the fastest way to read large sets of data from excel from Csharp. Example code would be great . .
In our desktop environment, I have reached the best mix between performance, flexibility and stability by using Excel via COM.
Access to Excel is always via the same thread.
I use late-binding (in VB.Net) to make my app version independent.
The rest of the application is developed in C#, only this part and some other small parts are in VB, because they are easier in VB.Net.
Dim workBook As Object = GetObject(fileName)
Dim workSheet As Object = workBook.WorkSheets.Item(WorkSheetNr)
Dim range As Object = workSheet.Cells.Item(1, 1)
Dim range2 As Object = range.CurrentRegion
Dim rrow As Integer = range2.Row ' For XL97, first convert to integer. XL97 will generate an error '
Dim rcolumn As Integer = range2.Column
Dim top As Object = workSheet.Cells.Item(rrow, rcolumn)
Dim bottom As Object = top.Offset(range2.Rows.Count - 1, range2.Columns.Count - 1)
range = workSheet.Range(top, bottom)
Dim values As Object(,)
values = range.Value
Here you have a 2-dimensional array containing the values from Excel. The last statement gets the data from Excel to .Net.
Since the limits on the size of a Excel sheet, these cannot get very large, so memory should not be a problem.
We have done some tests on performance, on multiple systems. It is optimized to create as few as possible (out-of-process) COM calls.
This way was the one that has given us the best performance, specially since the data is directly in an array, and access to this data is faster as going through a dataset.
Slow in this solution is starting Excel. But if you need to process multiple files, right after each other, the cost of starting Excel is made only once.
Also I would not use this solution in a server environment.
public class ExcelHeaderValues
{
public static string CUSIP = "CUSIP";
public static string ORIG = "ORIG";
public static string PRICE = "PRICE";
public static int COLUMNCOUNT = 3;
}
public class ExcelParser
{
private ArrayList collOutput = null;
string sSheetName = String.Empty;
string[] strValidColumn;
int validRowCnt = 0;
string sColumnPositions = String.Empty;
OleDbCommand ExcelCommand;
OleDbDataAdapter ExcelAdapter;
OleDbConnection ExcelConnection = null;
DataSet dsSheet = null;
string path = string.Empty;
string identifier = string.Empty;
public ExcelParser()
{
collOutput = new ArrayList();
}
public void Extract()
{
bool headermatch = false;
string strCusip = string.Empty, strOrig = string.Empty, strPrice = string.Empty, strData = string.Empty;
string strCusipPos = string.Empty, strPricePos = string.Empty, strOrigPos = string.Empty;
string strColumnHeader = String.Empty;
int reqColcount = 0;
string[] strTemp;
bool bTemp = false;
bool validRow = false;
DataTable schemaTable = GetSchemaTable();
validRowCnt = 0;
foreach (DataRow dr in schemaTable.Rows)
{
if (dsSheet != null)
{
dsSheet.Reset();
dsSheet = null;
}
strCusipPos = string.Empty;
strOrigPos = string.Empty;
strPricePos = string.Empty;
if (isValidSheet(dr))
{
sColumnPositions = string.Empty;
validRowCnt = 0;
foreach (DataRow dataRow in dsSheet.Tables[0].Rows)
{
sColumnPositions = string.Empty;
if (headermatch == false)
{
sColumnPositions = string.Empty;
foreach (DataColumn column in dsSheet.Tables[0].Columns)
{
strColumnHeader = dataRow[column.ColumnName].ToString().ToUpper().Trim();
strColumnHeader = strColumnHeader.ToUpper();
if (strColumnHeader == ExcelHeaderValues.ORIG.ToUpper() || strColumnHeader == ExcelHeaderValues.CUSIP.ToUpper() || strColumnHeader == ExcelHeaderValues.PRICE.ToUpper())
{
bTemp = true;
validRow = true;
reqColcount = ExcelHeaderValues.COLUMNCOUNT;
}
if (bTemp)
{
bTemp = false;
sColumnPositions += column.ColumnName + "^" + strColumnHeader + ";";
}
}
strValidColumn = sColumnPositions.Trim().Split(';');
if (validRow == true && reqColcount == strValidColumn.Length - 1)
{
headermatch = true;
break;
}
validRowCnt++;
}
}
if (headermatch == true)
{
try
{
if (dsSheet.Tables[0].Rows.Count > 0)
{
if (strValidColumn.Length > 0)
{
for (int i = 0; i < strValidColumn.Length - 1; i++)
{
if (strValidColumn[i].ToUpper().Contains("CUSIP"))
{
strTemp = strValidColumn[i].ToString().Split('^');
strCusipPos = strTemp[0].ToString();
strTemp = null;
}
if (strValidColumn[i].ToUpper().Contains("PRICE"))
{
strTemp = strValidColumn[i].ToString().Split('^');
strPricePos = strTemp[0].ToString();
strTemp = null;
}
if (strValidColumn[i].ToUpper().Contains("ORIG"))
{
strTemp = strValidColumn[i].ToString().Split('^');
strOrigPos = strTemp[0].ToString();
strTemp = null;
}
}
}
for (int iData = validRowCnt; iData < dsSheet.Tables[0].Rows.Count; iData++)
{
if (strCusipPos.Trim() != "")
strCusip = dsSheet.Tables[0].Rows[iData][strCusipPos].ToString().Trim();
if (strOrigPos.Trim() != "")
strOrig = dsSheet.Tables[0].Rows[iData][strOrigPos].ToString().Trim();
if (strPricePos.Trim() != "")
strPrice = dsSheet.Tables[0].Rows[iData][strPricePos].ToString().Trim().ToUpper();
strData = "";
if (strCusip.Length == 9 && strCusip != "" && strPrice != "" && strOrig != "" && !strPrice.ToUpper().Contains("SOLD"))
strData = strCusip + "|" + Convert.ToDouble(strOrig) * 1000000 + "|" + strPrice + "|||||";
if (strData != null && strData != "")
collOutput.Add(strData);
strCusip = string.Empty;
strOrig = string.Empty;
strPrice = string.Empty;
strData = string.Empty;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
headermatch = false;
sColumnPositions = string.Empty;
strColumnHeader = string.Empty;
}
}
}
private bool isValidSheet(DataRow dr)
{
bool isValidSheet = false;
sSheetName = dr[2].ToString().ToUpper();
if (!(sSheetName.Contains("$_")) && !(sSheetName.Contains("$'_")) && (!sSheetName.Contains("Print_Titles".ToUpper())) && (dr[3].ToString() == "TABLE" && ((!sSheetName.Contains("Print_Area".ToUpper())))) && !(sSheetName.ToUpper() == "DLOFFERLOOKUP"))
{
if (sSheetName.Trim().ToUpper() != "Disclaimer$".ToUpper() && sSheetName.Trim().ToUpper() != "Summary$".ToUpper() && sSheetName.Trim().ToUpper() != "FORMULAS$" )
{
string sQry = string.Empty;
sQry = "SELECT * FROM [" + sSheetName + "]";
ExcelCommand = new OleDbCommand(sQry, ExcelConnection);
dsSheet = new DataSet();
ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
isValidSheet = false;
try
{
ExcelAdapter.Fill(dsSheet, sSheetName);
isValidSheet = true;
}
catch (Exception ex)
{
isValidSheet = false;
throw new Exception(ex.Message.ToString());
}
finally
{
ExcelAdapter = null;
ExcelCommand = null;
}
}
}
return isValidSheet;
}
private DataTable GetSchemaTable()
{
DataTable dt = null;
string connectionString = String.Empty;
connectionString = GetConnectionString();
ExcelConnection = new OleDbConnection(connectionString);
try
{
ExcelConnection.Open();
dt = ExcelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
private string GetConnectionString()
{
string connStr = String.Empty;
try
{
if (path.ToLower().Contains(".xlsx"))
{
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + path + "';" + "Extended Properties='Excel 12.0;HDR=No;IMEX=1;'";
}
else if (path.ToLower().Contains(".xlsm"))
{
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + path + "';" + "Extended Properties='Excel 12.0 Macro;HDR=No;IMEX=1;'";
}
else if (path.ToLower().Contains(".xls"))
{
connStr = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "';Extended Properties='Excel 8.0;HDR=No;IMEX=1;'";
}
else
{connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + path + "';" + "Extended Properties='HTML Import;IMEX=1;HDR=No;'";
}
}
catch (Exception ex)
{
throw ex;
}
return connStr;
}
}

Categories

Resources