ASP.Net data table to excel shows date time format issue - asp.net

When am creating excel from datatatble in asp.net date time format is wrong. I just convert date time to 'dd-MM-yyyy', if the date is greater than 12 , it would write correctly , otherwise it would be mm-dd-yyyy format. Actuall am writing '01-11-2016' ie November 1st, but it shows '11-01-2016' in excel. My code is like this. Give me a solution.
int rowsTotal = 0, colsTotal = 0;
int i, j, k;
rowsTotal = dt.Rows.Count;
colsTotal = dt.Columns.Count;
for (i = 0; i <= rowsTotal - 1; i++)
{
k = 0;
for (j = 0; j <= colsTotal - 1; j++)
{
if (gv. Columns[j].Visible)
{
if (dt.Columns[j].DataType.Name == "DateTime")
{
DateTime dtDate = new DateTime();
excelWorksheet.Cells[i + 4, k + 1] = string.Format("{0:dd-MM-yyyy}", dt.Rows[i][j] != DBNull.Value ? Convert.ToDateTime(dt.Rows[i][j]) : dt.Rows[i][j]);
}
else
{
excelWorksheet.Cells[i + 4, k + 1] = dt.Rows[i][j];
}
k += 1;
}
}
}

Related

Setting Base Cases for Recursive Function

class Solution {
//given a location on the matrix, this function recursively find the deepest possible depth, which is the length of a side of a found square
private int isSquare(int row_index, int col_index, int depth, char[][] matrix) {
int last_row = row_index + depth;
int last_col = col_index + depth;
if (row_index >= matrix.length || col_index >= matrix[0].length) {
return 0;
}
if (last_row >= matrix.length || last_col >= matrix[0].length) {
return 0;
}
for (int i = col_index; i < last_col; i++) {
if (matrix[row_index][i] != '1') {
return 0;
}
}
for (int i = row_index; i < last_row; i++ ) {
if (matrix[i][col_index] != '1') {
return 0;
}
}
return Math.max(depth, isSquare(row_index, col_index, depth + 1, matrix));
}
public int maximalSquare(char[][] matrix) {
int max = 0;
for (int row = 0; row < matrix.length; row ++) {
for (int col = 0; col <matrix[0].length; col ++) {
int curr_depth = isSquare(row, col, 1, matrix);
if (curr_depth > max) {
max = curr_depth;
}
}
};
return max * max;
}
}
Hi, I was working on LeetCode 221, and it seems like that my solution is not passing test cases with output 1, where the biggest square on the given matrix is just 1 x 1. To me it looks like those depth 1 cases are not passing the two for loops in function isSquare, which is supposed to catch 0s in the square.
I tried LC debugging tool but it did not help much, and my base cases seem fine to me. Please let me know what is going on here. For the problem, https://leetcode.com/problems/maximal-square/
One of the depth 1 test cases that I am failing is below.
Input:
[["0","1"],["1","0"]]
Output:
0
Expected:
1

Last line of a datatable asp.net

I have a problem when I'm trying to a loop in a DataTable that a dataset contains.
I'm doing a loop like this:
for(int i = 0; i<ds.Tables[0].Rows.Count - 1 ; i++)
The problem is that I can't get the value of the last line with this one, but if I try to get rid of the "-1" and do a loop on the whole table, I'll have an out of range exception.
This out of range exception is because I have to check if the value of a line "i" is equal to the value of a line "i+1", like this:
if (ds.Tables[0].Rows[i]["Release_No"] != ds.Tables[0].Rows[i + 1]["Release_No"])
So if I do it in a loop, when the index is on the last line it will check if the last line is equal to i+1, and it's out of the table.
So I was trying to check if the index is on the last line, then just get the value of the last line, but it seems like it doesn't work.
if(ds.Tables[0].Rows.IndexOf(ds.Tables[0].Rows[i]) == ds.Tables[0].Rows.Count)
If anyone has an idea, let me know, and of course if it is not clear enough let me know, I'll give more information and more code.
Thanks for your help and your time!
Check if it's the last record, first.
I like to refactor code to read as close to sentence form as possible, explaining what you want it to do using named variables and methods, and that often gets me unlocked.
Try to make each line of code do one thing, and one thing only, like check if it is the last row:
var data = ds.Tables[0].Rows;
var lastRow = data.Count - 1;
for(int i = 0; i < lastRow ; i++)
{
if (i == lastRow){
// This is the last row. Handle the last row here.
}
else
{
// Handle all other rows here
var currentRecord = data[i];
var nextRecord = data[i + 1];
if (currentRecord["Release_No"] != nextRecord["Release_No"])
{
// Handle unique Releases...
}
}
}
Use less than or equal to like this
for(int i = 0; i<=ds.Tables[0].Rows.Count - 1 ; i++)
I hope this may get what you want.
Something like this is better ?
var lastRow = data.Count - 1;
var data = ds.Tables[0].Rows;
for(int i = 0; i< lastRow; i++)
{
testFirstCum = Convert.ToInt32(ds.Tables[0].Rows[i]["EDI_Accum_Quantity"]);
if ( i == lastRow)
{
if (DBNull.Value.Equals(data[i]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
else
{
var col = ds.Tables[0].Columns;
var currentRecord = data[i];
var nextRecord = data[i + 1];
if(currentRecord["Release_No"] != nextRecord["Release_No"])
{
for (int j = col[2].Ordinal; j < col.Count; j++)
{
if (DBNull.Value.Equals(data[i][j]))
{
quantity = 0;
}
else
{
quantity = Convert.ToInt32(data[i][j]);
testFirstCum = testFirstCum + quantity;
System.Diagnostics.Debug.WriteLine(quantity);
System.Diagnostics.Debug.WriteLine(testFirstCum);
}
}
}
}
}

Game Of Life ends quickly (Java)

I've created a basic version of the Game Of Life: each turn, the board is simulated by a 2D array of 1's and 0's, after which another class creates a drawing of it for me using the 2d array
I've read all the other questions here regarding this game, but no answer seems to work out for me....sorry if I'm beating a dead horse here.
I think I have a problem with my algorithm, thus maybe the board gets filled with the wrong amount of dead and alive cells and thus ends rather quickly (5-10 turns).
I've found an algorithm here to scan all the neighbors and even added a count = -1 in case it a cell in the grid scans itself as it's own neighbor, but I think I'm missing something here.
public static void repaint(board game, int size,int[][] alive, int[][] newGeneration)
{
int MIN_X = 0, MIN_Y = 0, MAX_X =9, MAX_Y =9, count;
for ( int i = 0; i < size; i++ )
{
for (int j = 0; j < size; j++) //here we check for each matrix cell's neighbors to see if they are alive or dead
{
count = 0;
if (alive[i][j] == 1)
count = -1;
int startPosX = (i - 1 < MIN_X) ? i : i - 1;
int startPosY = (j - 1 < MIN_Y) ? j : j - 1;
int endPosX = (i + 1 > MAX_X) ? i : i + 1;
int endPosY = (j + 1 > MAX_Y) ? j : j + 1;
for (int rowNum = startPosX; rowNum <= endPosX; rowNum++)
{
for (int colNum = startPosY; colNum <= endPosY; colNum++)
{
if (alive[rowNum][colNum] == 1)
count++;
}
}
if (alive[i][j] == 0 && count == 3) //conditions of the game of life
newGeneration[i][j] = 1; //filling the new array for the next life
if (alive[i][j] == 1 && count < 2)
newGeneration[i][j] = 0;
if (alive[i][j] == 1 && count >= 4)
newGeneration[i][j] = 0;
if (alive[i][j] == 1 && count == 3)
newGeneration[i][j] = 1;
}
}
game.setAlive(newGeneration); //we created a new matrix with the new lives, now we set it
SetupGUI(game,size); //re drawing the panel
}
}
What am I doing wrong? thanks for the help.

Make a Constraint to sorting an Array (Choco + java )

I am trying to develop a choco solver to the probelm of the Planning of telephone support center. in 12 hours from 8:00 o clock to 20:00.
variables and constraint :
Number of employees = 9
Minimum ans Maximum buisiness hours for every employee (h and H)
buisiness hours foe all employees : 42 hours <= total hours <= 42+C (C in my case equals 2)
Table of numbers of employee who work in every hour ( size of table =12 )
Contrainst who i cant'make :
I got to know the number of nuisiness hours for each employee but I can not put them in tracking hours :/
the result will be :
Final Result
but until now i got my result untill now
I think it's sort problem... please if you can just save my life and tell me what is the missing constraint in my code.
My code
package projetppc;
import java.util.Arrays;
import javax.swing.SortingFocusTraversalPolicy;
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solution;
import org.chocosolver.solver.variables.IntVar;
public class ProjetPPC {
public void modelAndSolve() {
int k = 9;
int htpj = 12;
int h = 4;
int H = 6;
int C = 2;
int HT = 42;
Model model = new Model(k + "- CAT");
int[] numOfemp = {1, 2, 4, 5, 5, 4, 5, 5, 3, 4, 2, 2};
IntVar[][] matrix = new IntVar[k][htpj];
for (int i = 0; i < k; i++) {
for (int j = 0; j < htpj; j++) {
matrix[i][j] = model.intVar("(" + i + "," + j + ")", 0, 1);
}
}
model.arithm(matrix[0][0], "=", 1).post();
int[] coeffs1 = new int[htpj];
Arrays.fill(coeffs1, 1);
// constraint 1 et 2
for (int i = 0; i < k; i++) {
model.scalar(matrix[i], coeffs1, "<=", H).post();
model.scalar(matrix[i], coeffs1, ">=", h).post();
}
int[] coeffs2 = new int[k];
Arrays.fill(coeffs2, 1);
IntVar[][] inversematrix = new IntVar[htpj][k];
for (int i = 0; i < k; i++) {
for (int j = 0; j < htpj; j++) {
inversematrix[j][i] = matrix[i][j];
}
}
// constraint
for (int i = 0; i < htpj; i++) {
model.scalar(inversematrix[i], coeffs2, "=", numOfemp[i]).post();
}
// constraint
IntVar[] alltable = new IntVar[k * htpj];
for (int i = 0; i < k; i++) {
for (int j = 0; j < htpj; j++) {
alltable[(htpj * i) + j] = matrix[i][j];
}
}
int[] coeffs3 = new int[k * htpj];
Arrays.fill(coeffs3, 1);
model.scalar(alltable, coeffs3, ">=", HT).post();
model.scalar(alltable, coeffs3, "<=", HT + C).post();
// solution
Solution solution = model.getSolver().findSolution();
if (solution != null) {
for (int i = 0; i < k; i++) {
System.out.println("employé " + i + " " + Arrays.toString(matrix[i]));
}
} else {
System.out.println("Pas de solution.");
}
}
public static void main(String[] args) {
new ProjetPPC().modelAndSolve();
}
}

Changing Data Structure in Data Table

I want to change the structure of my DataTable. My datatable shows data in following format:-
I pivoted my datatable, using following code.
public DataTable PivotTable(DataTable source)
{
DataTable dest = new DataTable("Pivoted" + source.TableName);
dest.Columns.Add(" ");
foreach (DataRow r in source.Rows)
dest.Columns.Add(r[0].ToString());
for (int i = 0; i < source.Columns.Count - 1; i++)
{
dest.Rows.Add(dest.NewRow());
}
for (int r = 0; r < dest.Rows.Count; r++)
{
for (int c = 0; c < dest.Columns.Count; c++)
{
if (c == 0)
dest.Rows[r][0] = source.Columns[r + 1].ColumnName;
else
dest.Rows[r][c] = source.Rows[c - 1][r + 1];
}
}
dest.AcceptChanges();
return dest;
}
After pivoting my Datatable shows record in following format:-
But I need result in following format:-
That is if there are 1 Sector it should show till 1 stop, if there are 3 Sector then it should show till 3 stop. It should increase or decrease automatically.
Please help me with the code.
Thanks

Resources