Am working in a project where I should give a crystal reports RPT file as input and get the properties of that report.
I am facing a problem in loading the report itself so I have used isLoaded() function to check whether the report is loaded or not.
I have used the following code:
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.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace sampleretreival
{
public partial class Form1 : Form
{
public string flName;
public Form1()
{
InitializeComponent();
Retrieve.Enabled = false;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstFiles.SelectedItems.Count > 0)
{
Retrieve.Enabled = true;
}
else
{
Retrieve.Enabled = false;
}
}
private void Browse_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Multiselect = false;
openFileDialog1.Filter = "Crystal Report Files | *.rpt";
openFileDialog1.ShowDialog();
flName = openFileDialog1.FileName;
if (flName.Length != 0)
{
lstFiles.Items.Insert(0, flName);
Retrieve.Enabled = true;
}
else
{
MessageBox.Show("Please select the crystal report files for analysis.", "SAMPLE", MessageBoxButtons.OK, MessageBoxIcon.Information);
Browse.Focus();
}
}
private void Retrieve_Click(object sender, EventArgs e)
{
int a=1;
ReportDocument rpt = new ReportDocument();
rpt.Load(flName);
int count = 5;
if (rpt.IsLoaded)
{
MessageBox.Show(count.ToString());
}
else
{
MessageBox.Show(a.ToString());
}
}
}
}
After compiling, I clicked the Browse button to select the report from the disk but when I click Retrieve button, the program keeps on running. I am not getting any output or any error.
Related
I created a little program to create a log file to record people's ID number, so far it runs good, no issues or errors, but recently I notice after running for three days it froze another program, until I force closed it. Can anyone take a look to the code to see if there is anything is wrong with it or to improve the code. Thank you.
The programs works with .NET Frameworks 3.5 and is for a Windows XP system, if is possible to make it work with a lower .NET Framework to reduce the installation of additional files.
MainWin form creates a fullscreen window to mask/cover some elements from other software. Is set as topmost to be always be on the top of everything. It has a transparent section with in a text file, then it minimize the window and finally activates a timer. When the timer finish, its maximaze the window again. It has a button to open the LoginWin form and a button to clear the data from the serieBox and return the cursor to the textbox.
LoginWin form is a window to input login information to open the LogFileWin form.
LogFileWin form is a window to read the saved data from the text file in a richTextBox, this data is from the MainWin form. It has a close button and a button to open FolderBrowserDialog to save the text file in another location or to a removable storage device.
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;
namespace LogSerie
{
public partial class MainWin : Form
{
public MainWin()
{
InitializeComponent();
}
private void MainWin_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
private void serieBox_KeyDown(object sender, KeyEventArgs e)
{
this.serieBox.MaxLength = 10;
if (e.KeyCode == Keys.Enter)
{
if ((serieBox.Text != ""))
{
if (serieBox.Text == "WLMANTO")
{
StreamWriter B = new StreamWriter("LogfileOperator.txt", true);
B.WriteLine(DateTime.Now + " " + label1.Text + " " + serieBox.Text);
B.Close();
serieBox.Clear();
this.WindowState = FormWindowState.Minimized;
timerManto.Enabled = true;
}
else
{
StreamWriter A = new StreamWriter("LogfileOperator.txt", true);
A.WriteLine(DateTime.Now + " " + label1.Text + " " + serieBox.Text);
A.Close();
serieBox.Clear();
this.WindowState = FormWindowState.Minimized;
timerOperador.Enabled = true;
}
}
}
}
private void timerOperador_Tick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
timerOperador.Enabled = false;
}
private void timerManto_Tick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
timerManto.Enabled = false;
}
private void logButton_Click(object sender, EventArgs e)
{
LoginWin openForm = new LoginWin();
openForm.ShowDialog();
}
private void borrarBut_Click(object sender, EventArgs e)
{
serieBox.Clear();
serieBox.Select();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LogSerie
{
public partial class LoginWin : Form
{
public LoginWin()
{
InitializeComponent();
}
private void LoginWin_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
private void entrarBut_Click(object sender, EventArgs e)
{
if ((usuBox.Text != "") && (contraBox.Text != ""))
{
if ((usuBox.Text == "ADMIN") && (contraBox.Text == "PASS"))
{
LogFileWin openForm = new LogFileWin();
openForm.TopMost = true;
openForm.ShowDialog();
usuBox.Clear();
contraBox.Clear();
this.Close();
}
else
{
MessageBox.Show("Login Incorrect", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void cancelBut_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace LogSerie
{
public partial class LogFileWin : Form
{
public LogFileWin()
{
InitializeComponent();
}
private void LogFileWin_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
private void logfileButton_Click(object sender, EventArgs e)
{
string path = #"C:\LogfileOperator\LogfileOperator.txt";
StreamReader stream = new StreamReader(path);
string filedata = stream.ReadToEnd();
richTextBox1.Text = filedata.ToString();
stream.Close();
}
private void closeBut_Click(object sender, EventArgs e)
{
this.Close();
}
private void usbBut_Click(object sender, EventArgs e)
{
string fileName = "LogfileOperator.txt";
string sourcePath = #"C:\LogfileOperator";
using (FolderBrowserDialog ofd = new FolderBrowserDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo fileInfo = new FileInfo(fileName);
sourcePath = Path.Combine(ofd.SelectedPath, fileInfo.Name);
File.Copy(fileName, sourcePath, true);
MessageBox.Show("Logfile Saved", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
If you're forced to use this old OS and old framework, you're going to have weird bugs like this. You can maybe work around it by reducing resource usage.
You're constantly creating new copies of your forms LoginWin and LogFileWin, which require OS resources. Instead create one instance of each form and re-use them.
There's also not any exception handling in your code, so you could have problems if files don't exist, or permissions change, or all sort of things. You need to have exception handling and that will give you more information about problems when they occur.
To re-use a form, create an instance as a private field in your class:
public partial class LoginWin : Form
{
// store the LogFileWin form so that we can re-use it
private LogFileWin _logFileWin;
public LoginWin()
{
InitializeComponent();
_logFileWin = new LogFileWin(); { TopMost = true; }
}
private void LoginWin_Load(object s, EventArgs e) { TopMost = true; }
private void entrarBut_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(usuBox.Text) ||
string.IsNullOrEmpty(contraBox.Text))
{
return;
}
if ((usuBox.Text == "ADMIN") &&
(contraBox.Text == "PASS"))
{
// add a reset function to the form
// that makes it ready to display again
_logFileWin.Reset();
// show the dialog
_logFileWin.ShowDialog();
usuBox.Clear();
contraBox.Clear();
this.Close();
}
else
{
MessageBox.Show("Login Incorrect",
"Message",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void cancelBut_Click(object s, EventArgs e) { Close(); }
}
For exception handling:
public partial class LogFileWin : Form
{
public LogFileWin()
{
InitializeComponent();
}
private void LogFileWin_Load(object s, EventArgs e) { TopMost = true; }
// new function to reset the the dialog to be shown again
public void Reset() { richTextBox1.Text = string.Empty; }
private void logfileButton_Click(object s, EventArgs e)
{
var path = #"C:\LogfileOperator\LogfileOperator.txt";
try
{
// reading from a file can fail, so this
// needs to be wrapped in a try catch
using (var stream = new StreamReader(path))
{
richTextBox1.Text = stream.ReadToEnd();
}
}
catch (Exception ex)
{
var m = String.Format("Unable to read '{0}'; {1}",
path, ex.Message);
MessageBox.Show(message,
"File read error",
MessageBoxButtons.Ok,
MessageBoxIcon.Error);
}
}
private void closeBut_Click(object s, EventArgs e) { this.Close(); }
// we can re-use the folder browser dialog;
// don't need to create a new one every time
FolderBrowserDialog _ofd = new FolderBrowserDialog();
private void usbBut_Click(object sender, EventArgs e)
{
var sourceFileName = "LogfileOperator.txt";
var destFolder = #"C:\LogfileOperator";
var destFileName = string.Empty;
try
{
if (_ofd.ShowDialog() == DialogResult.OK)
{
destFileName = Path.Combine(_ofd.SelectedPath,
sourceFileName);
// every time you do anything with a
// file, it needs to be in a try/catch
// in case the file doesn't exist,
// or the user doesn't have permission.
File.Copy(sourceFileName, destFileName, true);
MessageBox.Show("Logfile Saved",
"Message",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
// show a message to the user informing them
// of the error and why it occurred.
var m = string.Format("Copy '{0}' to '{1}' failed; {2}",
sourceFileName,
destFileName,
ex.Message);
MessageBox.Show(m,
"File copy error",
MessageBoxButtons.Ok,
MessageBoxIcon.Error);
}
}
}
First at all i am new at visual C#. I am trying to make a simple login form with only password login, but when i press button nothing happens. I want to check if there is a user in db which is online.
I dont know where i am making the mistake.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SportStat.Forme
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnUlaz_Click(object sender, EventArgs e)
{
ulaz();
}
private void ulaz()
{
if (txtPassword.Text == "")
{
MessageBox.Show("Morate upisati lozinku", "caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
return;
}
var sql = "select * from users where password='" + txtPassword.Text + "'";
var dt = sustav.puniDt(sql);
if (dt.Rows.Count == 0)
{
MessageBox.Show("Neispravna lozinka", "caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
txtPassword.Focus();
return;
}
if ((string)dt.Rows[0]["password"] != txtPassword.Text)
{
MessageBox.Show("Neispravna lozinka", "caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
txtPassword.Focus();
return;
}
sustav i = new sustav();
i.idUser = (int)dt.Rows[0]["idUser"];
i.pswd = (int)dt.Rows[0]["pswd"];
i.prezimeIme = (int)dt.Rows[0]["prezimeIme"];
i.idKlub = (int)dt.Rows[0]["idKlub"];
int count = i.pswd;
if (count == 1)
{
MessageBox.Show("Login Successful!");
this.Hide();
frmMain fm = new frmMain();
fm.Show();
}
this.Close();
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
Can someone help me with this problem.
Can't comment, too low rep..
Try to put another button on the form and call ulaz(); from that new button.
Sometimes, if you delete button and add them again, or copy paste code, methods get autoassign another name.
For example:
private void btnUlaz_Click(object sender, EventArgs e)
{
ulaz();
}
private void btnUlaz_Click_1(object sender, EventArgs e)
{
ulaz();
}
First one wont work.
I don't know if this is the case in your problem but it's worth trying..
I am creating a clock for clocking into a business. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
String Code;
String Name;
String InOut;
Boolean Luke = true;
String csvPath = "C:/users/luke/documents/C#/csvProject.csv";
StringBuilder Header = new StringBuilder();
StringBuilder csvData = new StringBuilder();
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
Header.AppendLine("Timestamp, Name");
File.AppendAllText(csvPath, Header.ToString());
textBox1.Font = new Font("Arial", 30, FontStyle.Bold);
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Code = Code + button.Text;
textBox1.Text = Code;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void button13_Click(object sender, EventArgs e)
{
//clear
Code = null;
textBox1.Text = Code;
}
private void button10_Click(object sender, EventArgs e)
{
//in or out
DateTime timeStamp = DateTime.Now;
if (Code == "123")
{
Name = "Luke";
}
Button button = (Button)sender;
csvData.AppendLine(timeStamp + "," + Name + "," + button.Text);
File.AppendAllText(csvPath, csvData.ToString());
Code = null;
textBox1.Text = Code;
}
private void button14_Click(object sender, EventArgs e)
{
}
}
}
My layout consists of a number pad, in button, and out button. When the user presses the in button after they enter their code, the program should write in the CSV file: Timestamp, Name, In. When I tested the code by clocking in, the program writes one row correctly. When I clock in and then clock out, it creates two rows of me clocking in and one row of me clocking out. I was wondering if anyone could help me find what is going wrong in the code. Thanks.
You need to empty csvData after writing it to the file.
im kinda new to programming and As university project,i have to write a program which changes a file info like a virus and then undo the changes just like anti virus.
i wrote the code for changing attribute on read only,But what about Hidden or system file ?
and what is the way for undoing it !
where im going wrong in coding ??
Here is my main form code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog fDialog;
void button1_Click(object sender, EventArgs e) // Browse button
{
fDialog = new OpenFileDialog();
fDialog.Title = "Open a Text File";
fDialog.Filter = "TXT Files|*.txt|doc Files|*.doc";
fDialog.InitialDirectory = #"C:\";
if (fDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(fDialog.FileName.ToString());
}
textBox1.Text = fDialog.FileName;
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)//the path showing text box
{
}
private void button2_Click(object sender, EventArgs e)//read-only button
{
fDialog.ReadOnlyChecked = true;
}
private void button3_Click(object sender, EventArgs e) //Hidden button
{
}
}
}
I am making a simple music player implementing the WMPlib to play media files ....
I am trying to open the file using a open file dialog ... the dialog comes and able to select the file but an exception comes when I try to assign the filename to Player.URL
at the line
Player.URL = openFileDialog1.FileName;
the error says
Object reference not set to an instance of an object.
Can anyone please give me a clue on how to assign the filename to the player.URL
the complete code is as follows....
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;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer Player;
public Form1()
{
InitializeComponent();
}
private void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
Player.MediaError +=
new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
Player.URL = url;
Player.controls.play();
}
private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
{
this.Close();
}
}
private void Player_MediaError(object pMediaObject)
{
MessageBox.Show("Cannot play media file.");
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";
openFileDialog1.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
PlayFile(Player.URL);
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
MessageBox.Show(openFileDialog1.FileName);
Player.URL = openFileDialog1.FileName;
}
}
}
Make sure that you create an instance of WMPLib.WindowsMediaPlayer before using it. Right now it seems that you are clicking 'open file' button and trying to assign returned file name to null object.
Try to use openFileDialog1 like this:
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
Player.URL = openFileDialog1.FileName;
}
in button1_Click()