|
using System;
using System.Collections.Generic;
class Program {
static void Main( string[] args ) {
List<int> voltages = new List<int>() { 10, 4, 15, 7, 20 };
int n = 0;
foreach ( int voltage in voltages ) {
Console.WriteLine( "Voltage {0}: {1}", ++n, "".PadRight( voltage, '*' ) );
}
}
}
|
|
|
|
|
Thanks Gerry... I can use this. This is as simple as it can get.
sundownr
|
|
|
|
|
Hi All,
I am using Visual Studio 2008 to design windows form Application.
I created one form, that will Browse and select excel file and by clicking on Export button, all data will transfer to sql database.
But while debugging, it stop at code
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
For your review i am giving full code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.Common;
namespace CHD_CALL_REGISTER
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=INLGM84MV1;Initial Catalog=CHD;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
}
public Form1()
{
InitializeComponent();
}
private void buttonBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "C:\\";
openFileDialog1.Title = "Browse Excel Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "xls";
openFileDialog1.Filter = "Excel file (*.xls)|*.xls";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.SafeFileName;
buttonexport.Enabled = true;
}
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
buttonexport.Enabled = false;
}
}
private void buttonexport_Click(object sender, EventArgs e)
{
string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source" + textBox1.Text + ";Extended Properties\"Excel 8.0;HDR=YES\"";
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);
connection.Open();
using (DbDataReader dr = command.ExecuteReader())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con))
{
bulkCopy.DestinationTableName = "EMP_TABLE";
bulkCopy.ColumnMappings.Add("EMPCODE", "EMPCODE");
bulkCopy.ColumnMappings.Add("EMP_NAME", "EMP_NAME");
bulkCopy.ColumnMappings.Add("BUSINESS_CODE", "BUSINESS_CODE");
bulkCopy.ColumnMappings.Add("JOIN_DT", "JOIN_DT");
bulkCopy.ColumnMappings.Add("HQ_NAME", "HQ_NAME");
bulkCopy.ColumnMappings.Add("ZONE_ID", "ZONE_ID");
bulkCopy.ColumnMappings.Add("DESIG", "DESIG");
bulkCopy.ColumnMappings.Add("BAND", "BAND");
bulkCopy.ColumnMappings.Add("DEPT_NAME", "DEPT_NAME");
bulkCopy.ColumnMappings.Add("EMP_STATUS", "EMP_STATUS");
bulkCopy.ColumnMappings.Add("SUP1", "SUP1");
bulkCopy.ColumnMappings.Add("SUP1NAME", "SUP1NAME");
bulkCopy.ColumnMappings.Add("SUP1HQ", "SUP1HQ");
bulkCopy.ColumnMappings.Add("SUP2", "SUP2");
bulkCopy.ColumnMappings.Add("SUP2NAME", "SUP2NAME");
bulkCopy.ColumnMappings.Add("SUP2HQ", "SUP2HQ");
bulkCopy.ColumnMappings.Add("SUP3", "SUP3");
bulkCopy.ColumnMappings.Add("SUP3NAME", "SUP3NAME");
bulkCopy.ColumnMappings.Add("SUP3HQ", "SUP3HQ");
bulkCopy.WriteToServer(dr);
MessageBox.Show("Data Exported To Sql Server Successfully");
}
}
}
}
}
}
Please help me...thanks in advance...
Vishal
|
|
|
|
|
Member 11134954 wrote: But while debugging, it stop at code After which point it will throw an exception with some valuable information.
Your connection-string is broken. It should read DataSource= . It'd be cleaner if you use a string.format to get your variable in there.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
Its get stop debug at below line.
// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
|
|
|
|
|
And what does the exception say?
|
|
|
|
|
Yes, but that line is correct. Copy/paste below line, should at least give a new error.
string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties\"Excel 8.0;HDR=YES\"", textBox1.Text);
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
After using your line/code. it still give same error but after i changed the line as below it proceed ahead.
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\"";
Now it got stuck at line:
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
Exception message says:
The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.
|
|
|
|
|
That line again does not cause the problem; it is the SQL statement that is being executed that generates the error. Did you put the sheetname in brackets?
Find the code that says "SELECT .. FROM [Sheet1$]", that's where the problem is.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I am using this line:
OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);
Does it seem to be invaline code?
|
|
|
|
|
Nope, looks correct. Same as the example here[^].
Is there a sheet with that name inside the workbook?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, sheet name is correct as Sheet1 in a excel file.
|
|
|
|
|
If you run the example code from the previous link, does it show the values?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
That's what I would do, but there are other options as well.
|
|
|
|
|
When i export excel file with increase cell width because my data Not show completely into cell
How To implement In this code To Increase cell Width in cell File
My Export Excel Code Following :-
public ActionResult ExportToExcel(int MID)
{
MModule objModule = new MModule();
objModule.LoadModule(MID);
DatabaseObject objData = new DatabaseObject();
string file = DateTime.Now.Ticks + "_" + objModule.Title + ".xlsx";
System.IO.File.Copy(Server.MapPath("/Excel/Export/template.xlsx"), Server.MapPath("/Excel/Export/" + file));
string filename = Server.MapPath("/Excel/Export/" + file);
objData.Query = "SELECT * FROM " + objModule.TableName + "";
DataTable dtReport = objData.GetTable();
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Mode=ReadWrite;Extended Properties=\"Excel 12.0 Xml;Readonly=False;IMEX=0;\"");
con.Open();
System.Data.OleDb.OleDbCommand cmd = con.CreateCommand();
string tableString = "";
string ColString = "";
for (int i = 0; i < dtReport.Columns.Count; i++)
{
tableString += "[" + dtReport.Columns[i].Caption + "] varchar(255),";
ColString += "[" + dtReport.Columns[i].Caption + "],";
}
tableString = tableString.Substring(0, tableString.Length - 1);
try
{
ColString = ColString.Substring(0, ColString.Length - 1);
//***************** Create Excel Sheet Table *********************//
cmd.CommandText = "CREATE TABLE ["+ objModule.Title +"] ( " + tableString + ");";
cmd.ExecuteNonQuery();
string ValString = "";
for (int j = 0; j < dtReport.Rows.Count; j++)
{
ValString = "";
for (int i = 0; i < dtReport.Columns.Count; i++)
{
ValString += "'" + dtReport.Rows[j][i] + "',";
}
ValString = ValString.Substring(0, ValString.Length - 1);
cmd.CommandText = "Insert Into ["+ objModule.Title +"] (" + ColString + ") Values (" + ValString + ");";
cmd.ExecuteNonQuery();
}
cmd.Dispose();
con.Dispose();
con.Close();
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excelApplication.Workbooks.Open(filename,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
excelApplication.DisplayAlerts = false;
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]).Delete();
workbook.Close(true, filename, null);
excelApplication.DisplayAlerts = true;
}
catch (Exception ex)
{
}
//return File(filename, "application/msexcel");
return File(filename, "application/x-excel", DateTime.Now.Ticks + "_Report.xlsx");
}
|
|
|
|
|
You don't increase the width of a cell. You can't do that in Excel and you can't do that in code.
You change the width of the COLUMN, not the cell.
C# Excel change column width[^]
|
|
|
|
|
How to create the windows application for Data recovery using c#.net which recovers all the deleted file from hard disk or removable disk
|
|
|
|
|
If you have to ask this question, you're really not prepared to write it.
You MUST know the layout of the NTFS file system on disk and how it works. Start reading the Windows Internals books and a bunch of these[^].
There is just not enough space in a few forum posts to tell you about everything you have to know to write a tool like this.
[EDIT]
I thought this question looked familiar. No matter how many times you ask this question you are NOT going to have it all explained to you and you probably will NOT find any C# examples. You'll probably find one or two examples in C/C++. This is not a utility everyone just goes out and writes for fun.
|
|
|
|
|
|
Gentleman:
I would like to raise a browser window as a way to view a PDF. Right now, the application raises the window and propogates an internet address perfectly, however it does not launch a default PDF application as a way to view a file, and if I force it manually (which would defeat the purpose of the application) it will appear in an external window. The window must be internal to the application. I have made it work with some aftermarket solutions such as Apitron but their solutions are rather pricy, so I thought the best way to handle it would be to use a browser in an application window that could be stacked on other windows. If I could use a plug in to run a PDF viewer of any kind inside that window, it would serve as an acceptable solution. SO ... how do I add a plugin to Gecko so that it will run a PDF file when the page is loaded? Thank you in advance...Pat
|
|
|
|
|
I'd like to apply voice recording function to specific application which uses threads. There are START and STOP buttons on application besides other forms.
I can start the recording method but the main application is not responding and I cannot stop the recording process.
I would appreciate any solution to resolve this issue.
private void barButtonItemStartRec_ItemClick(object sender, ItemClickEventArgs e)
{
barButtonItemStartRec.Enabled = !(barButtonItemStopRec.Enabled = true);
var dlg = new MainForm();
BackgroundWorker recWorker = new BackgroundWorker();
recWorker.DoWork += dlg.StartRec;
recWorker.RunWorkerAsync();
}
private void barButtonItemStopRec_ItemClick(object sender, ItemClickEventArgs e)
{
barButtonItemStartRec.Enabled = !(barButtonItemStopRec.Enabled = false);
var dlg = new MainForm();
dlg.StopRec();
}
|
|
|
|
|
You are creating a new mainform, and stopping recording there - you are not stopping the recording in the "mainform" that you created in the StartRec-method. Change the local variable to a member-field and only create the mainform once.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
fyi: due to delay of CP site here I did not see your post before I started mine.
«At the still point of the turning world. Neither flesh nor fleshless;
Neither from nor towards; at the still point, there the dance is
...
Neither ascent nor decline. Except for the point, the still point,
There would be no dance, and there is only the dance»
T.S. Elliot, The Four Quartets: "Burnt Norton"/xml>
|
|
|
|
|
A good thing, as your explanation is more elaborate.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
There are several "red flags" in my head looking at your code:
1. you create instances of 'MainForm in both the EventHandlers, and those are assigned to local variables ('dlg): that means the references to those instances are effectively discarded the moment the EventHandler code is finished executing.
So, the instance of 'MainForm created in the first EventHandler has no relationship to the instance of MainForm created in the second EventHandler.
2. similarly 'recWorker is created, and the reference to it will be discarded.
3. there's so much one can't know looking at this code:
a. what code is in the 'StartRec and 'StopRec methods in 'MainForm.
b. we don't know in what context (Form, Class) these two EventHandlers are defined.
An obvious change to try is:
1. create one instance of MainForm and keep a reference to it.
2. create one instance of 'recWorker and keep a reference to it.
Suggestions:
1. might as well set the Enabled property of the recording Button to 'false when it gets a click: if it's not enabled it will never get a click.
2. set the Enabled property of the recording Button to 'true when you recording terminates
3. what about disabling the stop Button initially, and when recording is terminated.
«At the still point of the turning world. Neither flesh nor fleshless;
Neither from nor towards; at the still point, there the dance is
...
Neither ascent nor decline. Except for the point, the still point,
There would be no dance, and there is only the dance»
T.S. Elliot, The Four Quartets: "Burnt Norton"/xml>
|
|
|
|
|