How do I insert data in Excel into database - c#

I have been beating my head on this for awhile, this is in import form, it imports lines from a excel spreadsheet to a datagridview then allows the user to make sure changes if needed, then adds or updates the db lines.
I have included sample rows from the spreadsheet, and the part of the code that checks the lines. My issue is that 4 of the rows do not flag as prev. found no matter how many times you run it.
Any input?
Sample excel file:
+-------+----------+-----------+------------------+-------+------------+--------+------------------------+
| Trans | Eff Date | Policy # | Insured Name | PIF | GWP | Agent% | Agent NB Comm |
+-------+----------+-----------+------------------+-------+------------+--------+------------------------+
| AV | Jul-10 | 186990710 | FLORECE, BOB | 0 | 0.00 | 10.000 | 0.00 |
| AV | Jul-18 | 193214729 | CIRONIDA, JOHN A | 0 | 0.00 | 10.000 | 0.00 |
| CH | Jun-26 | 186990710 | FLORECE, BOB | 0 | 51.97 | 10.000 | 5.20 |
| CH | Jul-10 | 186990710 | FLORECE, BOB | 0 | (64.83) | 10.000 | (6.48) - NOT MATCHED |
| CH | Jul-10 | 186990710 | FLORECE, BOB | 1 | 272.19 | 10.000 | 27.22 |
| CH | Jul-01 | 192182230 | URZ, SUSAN A | 0 | (50.70) | 10.000 | (5.07) - NOT MATCHED |
| CH | Jul-09 | 193089995 | STOMPKINS, JIM | 0 | 45.37 | 10.00 | 4.54 |
| CH | Jul-18 | 193214729 | CIRONIDA, JOHN A | 1 | 125.99 | 10.000 | 12.60 |
| CH | May-15 | 196666151 | STROP, JONA | 0 | 13.40 | 10.000 | 1.34 |
| CH | May-15 | 196666151 | STROP, JONA | 0 | (13.40) | 10.000 | (1.34) |
| CN | Jul-03 | 193435083 | MADDORE, SHARON | -1 | (1,097.81) | 10.000 | (109.78) - NOT MATCHED |
| NB | Jul-01 | 192182230 | URZ, SUSAN A | 3 | 570.10 | 10.000 | 57.01 |
| CH | Jul-10 | 197907782 | LANSKY, WAYNE | 0 | 4.34 | 10.000 | 0.43 |
| NB | Jul-14 | 184221262 | SMITH, DAN | 2 | 419.60 | 10.000 | 41.96 |
| CH | Jul-03 | 184532495 | MULLEN, ROBERT | 0 | (16.41) | 10.000 | (1.64) - NOT MATCHED |
+-------+----------+-----------+------------------+-------+------------+--------+------------------------+
code:
intDGRow = dgvImported.Rows.Count;
for (int i = 0; i < intDGRow; i++)
{
//set var values from the dgv
strPolicyNum = dgvImported.Rows[i].Cells["policynum"].Value.ToString().Replace(" ", string.Empty);
strEDate = DateTime.Parse(dgvImported.Rows[i].Cells["EffDate"].Value.ToString()).ToShortDateString();
strGWP = dgvImported.Rows[i].Cells["gwp"].Value.ToString().Replace(",", string.Empty);
strPIF = dgvImported.Rows[i].Cells["pif"].Value.ToString();
//check if row matches a current transaction in the DB - if so update matched column to value = matched - checks for match by doing a row count in the DB that match the fields - if only 1 row matches then the transaction is a match
if (dbAccess.RowCount("sales", "policynum = '" + strPolicyNum + "' AND effectivedate = #" + strEDate + "# AND pif = " + strPIF + " AND gwp = " + strGWP) == 1)
{
if (dbAccess.RowCount("sales", "policynum = '" + strPolicyNum + "' AND effectivedate = #" + strEDate + "# AND pif = " + strPIF + " AND gwp = " + strGWP + "AND matched = true") == 1)
{
dgvImported.Rows[i].Cells["Matched"].Value = "PrevMatched"; //update the matched column to reflect this item was previously matched
}
else
{
dgvImported.Rows[i].Cells["Matched"].Value = "Matched"; //update the matched column to reflect this item was matched
}//end sub if / else (prev matched)
}//end if
else
{
dgvImported.Rows[i].Cells["Matched"].Value = "UnMatched"; //update the matched column to reflect this item was not matched
}//end else
dgvImported.Rows[i].Cells["producer"].Value = dbAccess.ValueLookup("SELECT producer FROM sales WHERE policynum = '" + strPolicyNum + "'", "producer"); //lookup the producer for the policy number - should pull the last producer attached to the policy
dgvImported.Rows[i].Cells["SalesID"].Value = dbAccess.ValueLookup("SELECT saleskey FROM sales WHERE policynum = '" + strPolicyNum + "' AND effectivedate = #" + strEDate + "# AND gwp = " + strGWP, "saleskey"); //lookup the saleskey from sales table that matches the transaction
}//end for
}//end if file open
}//end file open button
if you need any additional details let me know.

Related

Linq caculate time difference and choose closest one

I have list of object like this one:
+-------------+------------+---------------------+
| ID | Value | Time |
+-------------+------------+---------------------+
| 1 | 1 | 2019-03-07 20:05:35 |
| 2 | 2 | 2019-03-07 20:06:09 |
| 5 | 5 | 2019-03-07 20:11:27 |
| 7 | 1 | 2019-03-07 20:13:30 |
| 8 | 0 | 2019-03-07 20:13:41 |
| 7 | 1 | 2019-03-07 20:17:00 |
| 8 | 0 | 2019-03-07 20:22:20 |
| 7 | 1 | 2019-03-07 20:23:05 |
| 8 | 0 | 2019-03-07 20:27:35 |
| 7 | 1 | 2019-03-07 20:27:37 |
| 8 | 0 | 2019-03-07 20:28:01 |
| 7 | 1 | 2019-03-07 20:37:19 |
| 8 | 0 | 2019-03-07 20:37:27 |
| 7 | 1 | 2019-03-07 20:37:54 |
| 8 | 0 | 2019-03-07 20:40:11 |
| 7 | 1 | 2019-03-07 20:44:00 |
| 8 | 0 | 2019-03-07 20:45:00 |
| 7 | 1 | 2019-03-07 20:47:41 |
| 7 | 1 | 2019-03-07 20:48:43 |
| 7 | 1 | 2019-03-07 20:48:51 |
| 8 | 0 | 2019-03-07 20:51:11 |
| 8 | 0 | 2019-03-07 20:54:46 |
| 8 | 0 | 2019-03-07 20:55:36 |
+-------------+------------+---------------------+
How to select records 15 minutes apart but records that are more close to the next time?
The result should be something like this:
+-------------+------------+---------------------+
| ID | Value | Time |
+-------------+------------+---------------------+
| 1 | 1 | 2019-03-07 20:05:35 |
| 8 | 0 | 2019-03-07 20:22:20 |
| 7 | 1 | 2019-03-07 20:37:19 |
| 8 | 0 | 2019-03-07 20:51:11 |
+-------------+------------+---------------------+
If the first time is 20:05:35 and next apart 15 min is 20:22:20. The closest time to that is 20:22:20 because difference between them is 00:02:14 and between 20:17:35 is difference 00:02:25. Is there any way to calculate difference and make decision which one is closer to choose?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication132
{
class Program
{
static void Main(string[] args)
{
List<Dates> dates = Dates.GetDates();
var results = dates.GroupBy(x => new DateTime(x.Time.Year, x.Time.Month, x.Time.Day, x.Time.Hour, 15 * (x.Time.Minute / 15), 0).AddMinutes(15))
.Select(x => x.OrderBy(y => x.Key.Subtract(y.Time)).First())
.ToList();
}
}
public class Dates
{
public int ID { get;set;}
public int Value { get;set;}
public DateTime Time { get;set;}
public static List<Dates> GetDates()
{
string input =
"| 1 | 1 | 2019-03-07 20:05:35 |\n" +
"| 2 | 2 | 2019-03-07 20:06:09 |\n" +
"| 5 | 5 | 2019-03-07 20:11:27 |\n" +
"| 7 | 1 | 2019-03-07 20:13:30 |\n" +
"| 8 | 0 | 2019-03-07 20:13:41 |\n" +
"| 7 | 1 | 2019-03-07 20:17:00 |\n" +
"| 8 | 0 | 2019-03-07 20:22:20 |\n" +
"| 7 | 1 | 2019-03-07 20:23:05 |\n" +
"| 8 | 0 | 2019-03-07 20:27:35 |\n" +
"| 7 | 1 | 2019-03-07 20:27:37 |\n" +
"| 8 | 0 | 2019-03-07 20:28:01 |\n" +
"| 7 | 1 | 2019-03-07 20:37:19 |\n" +
"| 8 | 0 | 2019-03-07 20:37:27 |\n" +
"| 7 | 1 | 2019-03-07 20:37:54 |\n" +
"| 8 | 0 | 2019-03-07 20:40:11 |\n" +
"| 7 | 1 | 2019-03-07 20:44:00 |\n" +
"| 8 | 0 | 2019-03-07 20:45:00 |\n" +
"| 7 | 1 | 2019-03-07 20:47:41 |\n" +
"| 7 | 1 | 2019-03-07 20:48:43 |\n" +
"| 7 | 1 | 2019-03-07 20:48:51 |\n" +
"| 8 | 0 | 2019-03-07 20:51:11 |\n" +
"| 8 | 0 | 2019-03-07 20:54:46 |\n" +
"| 8 | 0 | 2019-03-07 20:55:36";
List<Dates> dates = new List<Dates>();
string line = "";
StringReader reader = new StringReader(input);
while ((line = reader.ReadLine()) != null)
{
string[] lineArray = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
Dates newDate = new Dates()
{
ID = int.Parse(lineArray[0]),
Value = int.Parse(lineArray[1]),
Time = DateTime.Parse(lineArray[2])
};
dates.Add(newDate);
}
return dates;
}
}
}
The code move the time exactly at the 15 minute interval to next interval. The smallest resolution of time is 100ns (1 tick) so subtract 1 tick I believe is the correct solution
var results = dates.GroupBy(x => new DateTime(x.Time.Year, x.Time.Month, x.Time.Day, x.Time.Hour, 15 * (x.Time.Minute / 15), 0).AddMinutes(15).AddTicks(-1))
.Select(x => x.OrderBy(y => x.Key.Subtract(y.Time)).First())
.ToList();
To group by 15 minutes from start time use this
//tick is 100ns
const long TICKS_PER_15_MINUTES = 15 * 60 * 10000000L; //minutes, seconds, ticks
DateTime minTime = dates.OrderBy(x => x.Time).First().Time;
List<Dates> results = dates.GroupBy(x => (TICKS_PER_15_MINUTES * ((x.Time.Ticks - minTime.Ticks) / TICKS_PER_15_MINUTES) + TICKS_PER_15_MINUTES - 1))
.Select(x => x.OrderBy(y => x.Key - y.Time.Ticks).First())
.ToList();
Finally if you want closest 15 minutes from min time
//tick is 100ns
const long TICKS_PER_15_MINUTES = 15 * 60 * 10000000L; //minutes, seconds, ticks
DateTime minTime = dates.OrderBy(x => x.Time).First().Time;
List<Dates> results = dates.GroupBy(x => ((x.Time.Ticks - minTime.Ticks) % TICKS_PER_15_MINUTES) < (TICKS_PER_15_MINUTES / 2)
? TICKS_PER_15_MINUTES * ((x.Time.Ticks - minTime.Ticks) / TICKS_PER_15_MINUTES)
: (TICKS_PER_15_MINUTES * ((x.Time.Ticks - minTime.Ticks) / TICKS_PER_15_MINUTES)) + TICKS_PER_15_MINUTES)
.Select(x => x.OrderBy(y => Math.Abs( x.Key - y.Time.Ticks)).First())
.ToList();

Skip record based on single column value without iterating through each row openxml c#

I am having excel file like following structure:
ColA | ColB | ColC | ColD | ColE | ColF | ColG
1 | a | a | 23 | 12 | asd | 5
2 | s | v | 73 | 14 | qsd | 7
4 | t | b | 52 | 52 | avn |
2 | y | t | 13 | 71 | asx | 2
5 | d | e | 20 | 18 | vss |
1 | f | n | 63 | 72 | ann |
9 | g | a | 27 | 10 | aqz | 15
I want to read and get only those rows which does not have value in ColG.
I can iterate every rows and match each cell value of every row using following code:
for (int i = 0; i < rows.Count(); i++)
{
DataRow dataRow = dataTable.NewRow();
int cellCounter = 0;
foreach (Cell cell in rows.ElementAt(i))
{
dataRow[cellCounter] = GetCellValue(spreadSheetDocument, cell);
cellCounter++;
}
dataTable.Rows.Add(dataRow);
}
But it is very memory and time consuming process.
Can anyone help me out to achieve the same.
Thanks in advance.

Can't delete rows from DataTable based on another DataTable

I have two DataTables.
distinct:
+-------+-------+
| host | count |
+-------+-------+
| comp1 | 2 |
+-------+-------+
| comp2 | 3 |
+-------+-------+
| comp3 | 2 |
+-------+-------+
t:
+----+-------+
| id | host |
+----+-------+
| 1 | comp1 |
+----+-------+
| 2 | comp2 |
+----+-------+
| 3 | comp1 |
+----+-------+
| 4 | comp3 |
+----+-------+
| 5 | comp1 |
+----+-------+
| 6 | comp3 |
+----+-------+
I want to remove all rows from t which contains the computername having less than 3 in count column in distinct table. The code:
for (int i = 0; i < distinct.Rows.Count; i++)
{
if (Convert.ToInt32(distinct.Rows[i][1]) < 3)
{
foreach (DataRow row in t.Select())
{
if (row[1].ToString() == distinct.Rows[i][0].ToString())
{
row.Delete();
}
}
}
}
But it does nothing. If I compare the content of t before the above code and after, it's just the same. what am I missing here?
Please refer following link
How To Compare and delete datatable row using C#

itextsharp Generate a three column by two row table on a page

Wanting it to look something like this. I am then planning to put barcodes in each of the tables. Any help is appreciated.
_________ ________ ______
| | | | | |
| | | | | |
| | | | | |
| | | | | |
--------- -------- --------
_________ ________ ______
| | | | | |
| | | | | |
| | | | | |
| | | | | |
--------- -------- --------
Hope the following code helpful...
Document document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
int tableColumns = 3;
Table aTable = new Table(tableColumns);
int row = 1;
int col = 0;
foreach (Barcode s in codeslist)
{
// Create new barcode
Image imageCode128 = GetImageCode128(s);
// Create a new cell to host the barcode image
Cell cell = new Cell();
cell.Add(imageCode128);
aTable.AddCell(cell,row,col);
if (col == tableColumns-1)
{
col = 0;
row ++;
}
else col++;
}
// Add the completed table to the Document and close it.
document.Add(aTable);
document.Close();

Converting hierarchical data into an HTML unordered list Programmatically using C#

I've data which looks something like this.
| id | name | depth | itemId |
+-----+----------------------+-------+-------+
| 0 | ELECTRONICS | 0 | NULL |
| 1 | TELEVISIONS | 1 | NULL |
| 400 | Tube | 2 | NULL |
| 432 | LCD | 3 | 1653 |
| 422 | Plasma | 3 | 1633 |
| 416 | Portable electronics | 3 | 1595 |
| 401 | MP3 Player | 3 | 1249 |
| 191 | Flash | 2 | NULL |
| 555 | CD Players | 3 | 2198 |
| 407 | 2 Way Radio | 3 | 1284 |
| 388 | I've a problem with | 3 | 1181 |
| 302 | What is your bill pa | 3 | 543 |
| 203 | Where can I find my | 3 | 299 |
| 201 | I would like to make | 3 | 288 |
| 200 | Do you have any job | 3 | 284 |
| 192 | About Us | 3 | NULL |
| 199 | What can you tell me | 4 | 280 |
| 198 | Do you help pr | 4 | 276 |
| 197 | would someone help co| 4 | 272 |
| 196 | can you help ch | 4 | 268 |
| 195 | What awards has Veri | 4 | 264 |
| 194 | What's the latest ne | 4 | 260 |
| 193 | Can you tell me more | 4 | 256 |
| 180 | Site Help | 2 | NULL |
| 421 | Where are the | 3 | 1629 |
| 311 | How can I access My | 3 | 557 |
| 280 | Why isn't the page a | 3 | 512 |
To convert the above data into unordered list based on depth, I'm using the following code
int lastDepth = -1;
int numUL = 0;
StringBuilder output = new StringBuilder();
foreach (DataRow row in ds.Tables[0].Rows)
{
int currentDepth = Convert.ToInt32(row["Depth"]);
if (lastDepth < currentDepth)
{
if (currentDepth == 0)
{
output.Append("<ul class=\"simpleTree\">");
output.AppendFormat("<li class=\"root\"><span>root</span><ul><li class=\"open\" ><span><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
}
else
{
output.Append("<ul>");
if(currentDepth==1)
output.AppendFormat("<li><span>{0}</span>", row["name"]);
else
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
}
numUL++;
}
else if (lastDepth > currentDepth)
{
output.Append("</li></ul></li>");
if(currentDepth==1)
output.AppendFormat("<li><span>{0}</span>", row["name"]);
else
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
numUL--;
}
else if (lastDepth > -1)
{
output.Append("</li>");
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
}
lastDepth = currentDepth;
}
for (int i = 1; i <= numUL+1; i++)
{
output.Append("</li></ul>");
}
myliteral.text=output.ToString();
But the resulting unordered list doesnt seem to be forming properly(using which i am constructing a tree).For example "Site Help" with id '180' is supposed to appear as a direct child of "Televisions" with id '1',is appearing as a direct child of 'Flash' with id '191' using my code.so in addition to considering depth,I've decided to consider itemid as well in order to get the treeview properly.Those rows of the table with itemId not equal to null are not supposed to have a child node(i.e.,they are the leaf nodes in the tree) and all the other nodes can have child nodes.
Please could someone help me in constructing a proper unordered list based on my depth,itemid columns?
Update:
The 'itemid' column refers to the id of an item which is present in another table.This column just helps in identifying if an item has any sub items(i.e., 'name' in my data in this case has any other unordered lists under it).
It looks like the initial code wasn't taking account of the number of levels back up you needed to go from the "Can you tell me more" node to the "Site Help" node.
int lastDepth = -1;
int numUL = 0;
StringBuilder output = new StringBuilder();
foreach (DataRow row in ds.Tables[0].Rows)
{
int currentDepth = Convert.ToInt32(row["Depth"]);
if (lastDepth < currentDepth)
{
if (currentDepth == 0)
{
output.Append("<ul class=\"simpleTree\">");
output.AppendFormat("<li class=\"root\"><span>root</span><ul><li class=\"open\" ><span><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
}
else
{
output.Append("<ul>");
if (currentDepth == 1)
output.AppendFormat("<li><span>{0}</span>", row["name"]);
else
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
}
numUL++;
}
else if (lastDepth > currentDepth)
{
//output.Append("</li></ul></li>");
output.Append("</li>");
for (int i = lastDepth; i > currentDepth; i--)
{
output.Append("</ul></li>");
}
if (currentDepth == 1)
output.AppendFormat("<li><span>{0}</span>", row["name"]);
else
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
numUL--;
}
else if (lastDepth > -1)
{
output.Append("</li>");
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
}
lastDepth = currentDepth;
}
for (int i = 1; i <= numUL + 1; i++)
{
output.Append("</li></ul>");
}
myliteral.Text = output.ToString();
Stuart.

Categories