|
You're not going to find a walkthrough with all of those things, you need to break the problem down. If your issue is creating a DB using code first then google for a solution to that. That solution will work regardless of if it is a console app, a website or a WPF application.
create database ef code first - Google Search[^]
|
|
|
|
|
i know the basic method but i am having problems in finding exact syntax for my requirement .i have done extensive searches in google.I was hoping for more specific resources
|
|
|
|
|
Quote: but i am having problems in finding exact syntax for my requirement
Still sounds like: give me the codez.
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
Member 14520635 wrote: i am having problems in finding exact syntax
Post what you have and we can then advise you if there are better/correct methods of getting where you need to be.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
CP has many useful examples. Get to (home)work.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Console.WriteLine("-----Enter at least 10 numbers-----");
int[] num = new int[10];
int input = 0;
while (input <= 10)
{
Console.Write("Enter a whole number: ");
num[input] = Convert.ToInt32(Console.ReadLine());
input++;
}
Console.Write("\nThis are the list of Even Numbers: ");
int Even = 0;
while (Even < 10)
{
if (num[input] % 2 == 0)
Console.Write(num[input]);
Even++;
}
Console.Write("\nThis are the list of your Odd Numbers: ");
int Odd = 0;
while (Odd < 10)
{
if (num[input] % 2 == 1)
Console.Write(num[input]);
Odd++;
}
Console.ReadLine();
|
|
|
|
|
num array has 10 elements with indices from 0 to 9.
Change the line
while (input <= 10) to
while (input < 10)
This you could have easily spotted if you had launched a debug session.
|
|
|
|
|
A debug session. Are you crazy? Then he would have to press F5. Much easier to post all the source code here, and press F5 to see if there is a repl... ohh.. wait. Never mind.
|
|
|
|
|
Phil.o is exactly right with what your problem is: array indexes in c# (and most modern languages) run from zero to N-1 where N is the number of elements. An attempt to use index N or any negative number is automatically outside the array and will generate a error.
But ... a couple of things:
0) It's a very good idea to always use curly brackets round even single lines inside an if :
if (num[input] % 2 == 0)
{
Console.Write(num[input]);
}
Even++;
If you don't, then you can get problems because the indentation is just to make teh code more readable to us, it means nothing to the compiler. If you add a line which is meant to be inside the if without curly brackets, it looks like it should work, but it won't:
if (num[input] % 2 == 0)
Console.Write(num[input]);
num[input] = 0;
Even++;
If you always use the brackets, it's obvious and you can't get it wrong:
if (num[input] % 2 == 0)
{
Console.Write(num[input]);
num[input] = 0;
}
Even++;
1) Don't comment what the instruction is doing:
int[] num = new int[10]; We know it's declaring an array, because we can read the code: it's declaring an integer array variable called num and assigning a new instance of 10 elements.
Your comment tells us nothing useful; it add nothing to the code. In fact it tells us less than the code does, and it's more likely to get "out of step" with the code if you change the array to a List (which is a kind of array that you can add and remove elements from - you'll meet them later).
Pretty much every comment in there does the same thing: document what the code is doing, not why you are doing it.
A better comment in this case is no comment at all, but use a different name for the variable:
int[] userInputs = new int[10]; That tells you what the array actually contains, without needing a comment.
This is called "self documentation" and it has the major advantage that it's much, much more likely to be kept up to date when code changes.
2) Don't use Convert to parse user inputs: users make mistakes, and Convert will cause your app to fail if they do. While this is only ten numbers, it's still going to be very annoying to type nine of them successfully and then miskey on the tenth and have the app crash out as a result!
Instead, use the TryParse methods:
int[] userInputs = new int[10];
int index = 0;
while (index < userInputs.Length)
{
Console.Write("Enter a whole number: ");
string input = Console.ReadLine();
if (int.TryParse(input, out userInputs[index]))
{
index++;
}
else
{
Console.WriteLine($"\"{input}\" is not an integer!");
}
}
3) Are you sure you want to use input in your final pair of loops?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
modified 4-Jul-19 3:08am.
|
|
|
|
|
Do notice on your Odd and Even loops, the value
num[input] is always the same number as input variable is not modified for either loop. You may want
input[Even] input[Odd] for subsequent loops.
But I never wave bye bye
|
|
|
|
|
<pre>
using Microsoft.Office.Interop.Excel;
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelTestConsoleApp1
{
class Program
{
public static string filePath = @"f:\ExcelTest\Tst_Excel.xlsx";
static void Main(string[] args)
{
CreateExcelFile();
ReadExcelFile();
Console.ReadLine();
}
private static void CreateExcelFile()
{
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
Console.WriteLine("Excel is not installed on this system... ");
return;
}
object misValue = System.Reflection.Missing.Value;
Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "FirstName";
xlWorkSheet.Cells[1, 2] = "LastName";
xlWorkSheet.Cells[2, 1] = "Greg";
xlWorkSheet.Cells[2, 2] = "Gonzales";
xlWorkBook.SaveAs(filePath, XlFileFormat.xlExcel12, misValue, misValue, misValue, misValue,
XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);
xlWorkBook.Close();
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Excel file created successfully... ");
Console.BackgroundColor = ConsoleColor.Green;
}
private static void ReadExcelFile()
{
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("\nReading the Excel File... ");
Console.BackgroundColor = ConsoleColor.Green;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int totalRows = xlRange.Rows.Count;
int totalColumns = xlRange.Columns.Count;
string firstValue, secondValue;
for (int rowCount = 1; rowCount <= totalRows; rowCount++)
{
firstValue = Convert.ToString((xlRange.Cells[rowCount, 1] as Excel.Range).Text);
secondValue = Convert.ToString((xlRange.Cells[rowCount, 2] as Excel.Range).Text);
Console.WriteLine(firstValue + "\t" + secondValue);
}
xlWorkBook.Close();
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Console.WriteLine("End of File... ");
}
private static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
Console.WriteLine("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
xlWorkBook.SaveAs(filePath, XlFileFormat.xlExcel12, misValue, misValue, misValue, misValue,
XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);
Every time I try to run this code I get this error…
Exception Unhandled
System.Runtime.InteropServices.COMExceptions:'This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type.'
<pre>
|
|
|
|
|
At a guess? You have an old version of Excel installed.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Quote: xlExcel12 50 Excel Binary Workbook *.xlsb
public static string filePath = @"f:\ExcelTest\Tst_Excel.xlsx";
XlFileFormat.xlExcel12,
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
|
hey all, beginner coder here! I have a project that I am going to attempt - I need to schedule an automated copy of files from several (10) client pcs to 1 main server. Any inputs on if I should use C# to build an executable that I could schedule on task scheduler to complete this? Or what would be the best to use? Thanks.
|
|
|
|
|
Yes: [^]
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
hi, i have some problems to use httpexception but, in my visual studio httpexception not found, can anyone help my problem to handle httpexception please?
|
|
|
|
|
|
i have a question, does HttpTransportBindingElement have an exception? if there is a way to use the exception like what?
|
|
|
|
|
It's a class. Classes don't throw exceptions - properties and methods *IN* that class do. If you want to find out what execptions are thrown by the methods and properties in that class, intellisense will tell you, and so will google.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I am trying to converting the HTML string to PDF file using IText, It through the following exception
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
string htmltext = "my html string";
byte[] pdf = null;
var workStream = new MemoryStream();
using (var pdfWriter = new PdfWriter(workStream))
{
pdfWriter.SetCloseStream(false);
using (var document = HtmlConverter.ConvertToDocument(htmltext, pdfWriter))
{
pdf = workStream.GetBuffer();
}
}
workStream.Position = 0;
return pdf;
|
|
|
|
|
Assuming this is iTestSharp you are using, not IText:
string htmlText = File.ReadAllText(@"D:\Test Data\APage.html");
using (MemoryStream stream = new System.IO.MemoryStream())
{
using (StringReader sr = new StringReader(htmlText))
{
using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f))
{
using (PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream))
{
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
File.WriteAllBytes(@"D:\Test Data\APage.pdf", stream.ToArray());
}
}
}
}
Provided your HTML is well formed, that should work fine.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Call it several times, remembering where you found it last...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|