Cannot find column 25 Exception coming - ms-access-2007

I am creating one report where data exported from my access database to excel here. I'm not using any datagridview , when user click one checkbox appropriate report he can able to download for this I have written code but when I run this code its giving me Cannot find column 25, I don't know why its showing me this?
In access table I have total 25 column :
string sql = null;
string data = null;
int i = 0;
int j = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
string connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
cnn.ConnectionString = connectionString;
cnn.Open();
sql = "SELECT * FROM Billing WHERE Bill_No and Bill_Date is null order by FormNo desc";
OleDbDataAdapter dscmd = new OleDbDataAdapter(sql, cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);
cnn.Close();
for (j = 0; i < ds.Tables[0].Columns.Count; j++)
{
xlWorkSheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].Caption; // Exception coming this line
}
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (j = 0; j < ds.Tables[0].Columns.Count; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}
System.Windows.Forms.SaveFileDialog saveDlg = new System.Windows.Forms.SaveFileDialog();
saveDlg.InitialDirectory = #"C:\";
saveDlg.Filter = "Excel files (*.xls)|*.xls";
saveDlg.FilterIndex = 0;
saveDlg.RestoreDirectory = true;
saveDlg.Title = "Export Excel File To";
xlWorkBook.Close(true, misValue, misValue);
MessageBox.Show("File Downloaded successfully...");
xlApp.Quit();
}

i got the answer here i change my for loop
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
xlWorkSheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].Caption;
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}

Related

inserting list data into table in itext7 pdf (xamarin android)

I have a list and I need to put the data of this list in a pdf file. I searched and found the itext7 library where you can create a table in the pdf. I wrote the following code:
var path2 = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
filePath = Path.Combine(path2.ToString(), "Client's_Account.pdf");
stream = new FileStream(filePath, FileMode.Create);
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdf2 = new iText.Kernel.Pdf.PdfDocument(writer);
Document document = new Document(pdf2,iText.Kernel.Geom.PageSize.TABLOID);
Paragraph header;
acc_info acc = new acc_info();
//foreach (var t in tableItems)
//{
// string txt = t.itembarcode + " " + t.itemsunitcode + " " + t.name + "\n";
// header = new Paragraph(txt);
// document.Add(header);
//}
iText.Layout.Element.Table table = new iText.Layout.Element.Table(5, false);
// table.SetWidth(400).SetFixedLayout();
table.AddCell(new Cell(1, 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Date")));
table.AddCell(new Cell(1, 2).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Reference")));
table.AddCell(new Cell(1, 3).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Description")));
table.AddCell(new Cell(1, 4).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Debit")));
table.AddCell(new Cell(1, 5).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Credit")));
for (int i = 0; i < user.tableItems_accInfo.Count; i++)
{
int j = 0;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].jvdate)));
j = j + 1;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].jvref)));
j = j + 1;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].desc)));
j = j + 1;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].jvdebit)));
j = j + 1;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].jvcredit)));
}
document.Add(table);
string txt = "\n \n Total Credit= " + tot_crdt.Text +"\n \n Total Debit= "+tot_dbt.Text+ "\n \n Total Balance= "+balance.Text;
header = new Paragraph(txt);
document.Add(header);
document.Close();
Toast.MakeText(this.Context, "PDF Generated", ToastLength.Short).Show();
but the table I get is so bad even if I change the pdf pagesize (A4, legal, tabloid...). what did I do wrong?
here's the table that I get:
thanks in advance
You seem to misinterpret the Cell constructor parameters. They don't represent the row and column in which the cell shall go but instead the number of rows and columns the cell shall span.
Instead of
new Cell(n, m)
for various n and m, therefore, you should use
new Cell()

c# EPPlus: how to fix - ShowFilter doesn't work

The issue with Table.ShowFilter, using EPPLUS library.
Created new ExcelTable in the sheet, but can't apply Table.ShowFilter = false.
The filters are still in the table.
TableStyle and StyleName work fine.
ExcelTable et = (ExcelTable)Table;
int firstRow = newRow;
int lastRow;
if (DataStructure.Data != null)
lastRow = newRow + DataStructure.Data.Count();
else
lastRow = newRow + 1;
int firstColumn = OriginalAddress.StartColumn;
int lastColumn = OriginalAddress.EndColumn;
ExcelRange rg = ws.Cells[firstRow, firstColumn, lastRow, lastColumn];
Guid guid = Guid.NewGuid();
string str_guid = guid.ToString();
string tableName = et.Name + "_" + str_guid;
ExcelTable tab = wsTarget.Tables.Add(rg, tableName);
// tab.ShowHeader = et.ShowHeader;
// tab.TableStyle = et.TableStyle;
// tab.StyleName = et.StyleName;
tab.ShowFilter = false;
Right table is desired result.
Please, help to fix!
Is this you want to mean?
tab.ShowHeader = false;
This very little example works here :
using (ExcelPackage xls = new ExcelPackage())
{
ExcelWorksheet ws2 = xls.Workbook.Worksheets.Add("f1");
OfficeOpenXml.Table.ExcelTable tab = ws2.Tables.Add(new ExcelAddressBase("a1:e5"), "table");
//tab.TableStyle = et.TableStyle;
//tab.StyleName = et.StyleName;
tab.ShowFilter = false;
FileInfo f = new FileInfo(#"d:\temp\test.xlsx");
xls.SaveAs(f);
}
Probably, the bug.
Adding ws2.DeleteColumn(1, 1) to the code, add filters to the table, even with tab.ShowFilter = false.
Workaround: put tab.ShowFilter = false right before file saving.
using (ExcelPackage xls = new ExcelPackage())
{
ExcelWorksheet ws2 = xls.Workbook.Worksheets.Add("f1");
OfficeOpenXml.Table.ExcelTable tab = ws2.Tables.Add(new ExcelAddressBase("d3:g8"), "table");
//tab.TableStyle = et.TableStyle;
//tab.StyleName = et.StyleName;
ws2.DeleteColumn(1, 1);
tab.ShowFilter = false;
FileInfo f = new FileInfo(#"test.xlsx");
xls.SaveAs(f);
}

Datatable Select all rows

I want to insert into table for specific columns into table using select * from datatable.But it is giving me an array .. is there any way I can get the Insert work.
all I want to do is
Insert into table1 (id,name,status) select * from datatable
query += "Insert into " + tableName.ToLower() + "";
query += "(";
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i != dt.Columns.Count - 1)
query += dt.Columns[i].ColumnName.ToLower() + ",";
else
query += dt.Columns[i].ColumnName.ToLower();
}
query += ")";
DataRow[] result = dt.Select();
query += "select * from " + result
You Can't use two sql Statements at once like that you have to separate them in two different objects or variables (It's sometimes allowed when you end each by a semicolon ; but not in your case )
try to get each cell value in the row
query += "Insert into " + tableName.ToLower() + "";
query += "(";
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i != dt.Columns.Count - 1)
query += dt.Columns[i].ColumnName.ToLower() + ",";
else
query += dt.Columns[i].ColumnName.ToLower();
}
query += ")";
query += "values ( ";
string temQry=query;//you need it more than once
DataRow[] result = dt.Select();
for (int i = 0; i < dt.Rows.Count; i++){
for (int j = 0; j < dt.Columns.Count; j++){
if ((j+1)<dt.Columns.Count){
query += result[i].item(j) +"," ;
}
else {
query += result[i].item(j);}
}
query=temQry; //recover the query for each new row
}

How to change the style of the excel chart in vsto word addin?

how to change the style and how make x axis and y axis bold with differnt colour?
Word.Chart wdChart = doc.InlineShapes.AddChart(Microsoft.Office.Core.XlChartType.xlColumnClustered, ref missing).Chart;
wdChart.Legend.LegendEntries(wdChart.Legend.LegendEntries().Count).Delete();
wdChart.Legend.Clear();
wdChart.ChartStyle = 311;
Word.ChartData chartData = wdChart.ChartData;
Excel.Workbook dataWorkbook = (Excel.Workbook)chartData.Workbook;
Excel.Worksheet dataSheet = (Excel.Worksheet)dataWorkbook.Worksheets[1];
//wdChart.ChartTitle.Text = String.Empty;
dataWorkbook.Application.Visible = false;
for (int i = 0; i < xData.Count; i++)
{
((Excel.Range)dataSheet.Cells.get_Range("A" + (i + 2).ToString(), missing)).FormulaR1C1 = xData[i].ToString();
((Excel.Range)dataSheet.Cells.get_Range("B" + (i + 2).ToString(), missing)).FormulaR1C1 = yData[i].ToString();
((Excel.Range)dataSheet.Cells.get_Range("C" + (i + 2).ToString(), missing)).FormulaR1C1 = zData[i].ToString();
}
wdChart.HasTitle = false;
dataWorkbook.Application.Quit();

How to do this in Visual basic?

How to do the "a++" and "b++" in Visual basic?
What is the another codes for there in Vb?
The names there are just example.
int a = 0;
int b = 0;
{
if (ans1.Text == "James")
{
a++;
}
else
{
b++;
}
if (ans2.Text == "Ryan")
{
a++;
}
else
{
b++;
}
if (ans3.Text == "Mac")
{
a++;
}
else
{
b++;
}
t1.Text = a.ToString();
t2.Text = b.ToString();
}
Like this:
a += 1
b += 1
(...)
Like this
DIM a as integer = 0
DIM b as integer = 0
If ans1.Text = "James" Then
a += 1
Else
b += 1
End If
If ans2.Text = "Ryan" Then
a += 1
Else
b += 1
End If
If ans3.Text = "Mac" Then
a += 1
Else
b += 1
End If
t1.Text = a.ToString()
t2.Text = b.ToString()
Your question has already been answered but I think it would be useful to see how you could simplify your code:
Dim correctAnswers As Integer = 0
Dim totalQuestions As Integer = 3'you need to modify this is you add more questions
'increment the number of correct answers for each one we find
correctAnswers += If(ans1.text = "James", 1, 0)
correctAnswers += If(ans2.text = "Ryan", 1, 0)
correctAnswers += If(ans3.text = "Mac", 1, 0)
'show the number of correct and incorrect answers
t1.Text = correctAnswers.ToString()
t2.Text = (totalQuestions - correctAnswers).ToString() 'show the number of incorrect questions
Neither the postfix nor prefix ++ are defined in Visual Basic.
Your only realistic option is to use a = a + 1 (or, in later BASICs, a += 1) instead (note the lack of a ; for a statement terminator). But note that this will not evaluate to the previous value of a and the entire construct is not an expression in the C / C++ sense. You could build a function to mimic a++ but that would be too obfuscating.