|
sure you can trick the owner.
But a better solution is to not own the dataform. Something like:
private btn1Click(object sender, EventArgs e){
this.Hide();
DataForm df = new DataForm();
df.ShowDialog();
this.Show();
}
|
|
|
|
|
can a visual c# project connect to a remote mysql database and execute sql statements but w/out using mysql connector/net?
thanks in advance!
oh,and not just a yes/no answer...doh doh doh... if yes,how can you do it or if not, what is the simplest way of using mysql connector??
|
|
|
|
|
I'm not sure if you can do it without a MySQL connector, but using one is not that tough...
Download the MySQL data connector dll[^]. Register it in your project. Do something like below...
DataTable customerTable = new DataTable();
using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection("server=serverName;uid=userID;pwd=password;database=databaseName"))
{
string validationQuery = "SELECT * FROM Table WHERE Contact_ID = '" + actNumber + "';";
MySql.Data.MySqlClient.MySqlDataAdapter odda = new MySql.Data.MySqlClient.MySqlDataAdapter(validationQuery, conn);
odda.Fill(customerTable);
}
Hogan
|
|
|
|
|
You can use .NET Connector or ODBC connector. However if you do not want dll, .NET connector is open source and you get source file, and you can include it inside your project (Not add project to existing solution. yust .cs files) to remove dependency. However connecting to MS SQL server, a connector is build in into .NET framework
|
|
|
|
|
If you do not want MySql connector, Create an ODBC connection to the server - Use OdbcConnection in your project.
|
|
|
|
|
Hi All,
I am having problem while creating a custom event. My aim is to fire "DataChnaged" event whenever property "SetData" is called in following class defination.
The problem is, I am getting a null event in this line "DataChanged(this, e);". Anyone can help me understanding where I am wrong,
public class DataChangedEventArgs : EventArgs
{
private String _strArgVal = "";
public DataChangedEventArgs(String Value)
{
_strArgVal = Value;
}
}
public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs e);
public class MyUserControlOne
{
String DataString = String.Empty;
public event DataChangedEventHandler DataChanged;
public MyUserControlOne()
{
DataString = "";
}
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
DataChanged(this, e);
}
public String SetData
{
set
{
if (value != DataString)
{
OnDataChanged(new DataChangedEventArgs("Event Fired !!!"));
} DataString = value;
}
}
}
Calling code,
class Program
{
static void Main(string[] args)
{
MyUserControlOne o = new MyUserControlOne();
o.SetData = "ABC";
}
} Thanks,
Arindam D Tewary
|
|
|
|
|
first you need to check if OnDataChanged DataChanged is not null, then throw event. NullReferenceException is thrown, because there isn't any method subscribed to that eventmodified on Monday, March 1, 2010 1:40 PM
|
|
|
|
|
Thanks Saksida. As per your comment I noticed I have not provided any method which subscribes to the event. Now after I have written a "MyUserControlOne_DataChanged" method and its working nicely. Thank you very much for poiting me the mistake.
Now this is working nicely !
public class DataChangedEventArgs : EventArgs
{
private String _strArgVal = "";
public DataChangedEventArgs(String Value)
{
_strArgVal = Value;
}
}
public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs e);
public class MyUserControlOne
{
String DataString = String.Empty;
private bool isEventWorking = false;
public event DataChangedEventHandler DataChanged;
public MyUserControlOne()
{
DataString = "";
this.DataChanged += new DataChangedEventHandler(MyUserControlOne_DataChanged);
}
protected void MyUserControlOne_DataChanged(object sender, DataChangedEventArgs e)
{
isEventWorking = true;
}
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
DataChanged(this, e);
}
public String SetData
{
set
{
if (value != DataString)
{
OnDataChanged(new DataChangedEventArgs("Event Fired !!!"));
} DataString = value;
}
}
public bool EventWorking
{
get { return isEventWorking; }
}
}
calling code:
static void Main(string[] args)
{
MyUserControlOne o = new MyUserControlOne();
o.SetData = "ABC";
o.SetData = "ABCD";
o.SetData = "ABCD";
Console.WriteLine(o.EventWorking);
}
Thanks,
Arindam D Tewary
|
|
|
|
|
It is worth changing your code slightly to cope with the case when you have no handler - if you think about it not handling an event is not an error, it is perfectly normal. As such, it should not throw an exception.
Arindam Tewary wrote:
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
DataChanged(this, e);
}
If you change your code to:
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
EventHandler eh = DataChanged;
if (eh != null)
{
eh(this, e);
}
}
Then a potential source of run time exception is removed. (The load of "eh" is to handle the admittedly rare case when the handler is removed between the test and the execution)You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
Hi OriginalGriff,
You are very much correct. I would be changing my code for sure. Thanks for your comment. Highly appreciate it. Thanks,
Arindam D Tewary
|
|
|
|
|
I would use:
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
if (DataChanged != null)
DataChanged(this, e);
}
I think this way it is easier to read and not declaring new variable
|
|
|
|
|
No, what Griff gave you is the correct way to do things; there is a (probably small) probability that another thread could remove the content of the event between your code checking it for null, and your code using it. A local copy fixes that.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
tham. Now i see the problem. It was same example in a book i was reading. Maybe it is time to burn that book
|
|
|
|
|
Arindam Tewary wrote: if (value != DataString)
{
OnDataChanged(new DataChangedEventArgs("Event Fired !!!"));
} DataString = value;
furthermore it is wrong to throw the event BEFORE the data change has occurred. A delegate is likely to read SetData to find out what the new data value is, it should not get the old value instead.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Hi Luc,
Thank you very much for your valuable input. Its great to get some input from MVPs
Thats surely bad design. I would change that immediately. As I was digged into that "NullException", so I overlooked that. Please if you want to provide any other input also, although origianl problem is solved, please provide.Thanks,
Arindam D Tewary
|
|
|
|
|
There are many articles and blogs that deal with events but most are a little overwhelming initialy.
I wrote this[^] article to provide a gentle tutorial which you may find useful.
|
|
|
|
|
Hi,
I'm trying to end 3 threads in a save way. I can get 2 threads to stop, but the last one doesn't seem to abort.
My try:
private volatile bool stopRequested = false;
this.stopRequested = true;
this.thdPageContent1.Join();
this.thdPageContent2.Join();
this.thdFileComparer.Join();
After the thdFileComparer thread, i got a messagebox that 'tells' me the threads are stopped:
MessageBox.Show("Threads stopped successfully");
Any idea what I might be doing wrong?
Thanks in advance!
|
|
|
|
|
The join method only waits for a thread to end... it does not abort the thread.
So, if your thread is in an infinite loop (or infinite wait), your join will be waiting infinitelly.
I must say I need to know the thread code to know what's wrong... but, if you are using some event (ManualResetEvent, for example) in the 3rd thread, you must set it. So, the thread can execute (and check the stopRequested variable).
|
|
|
|
|
Hi,
What do I need to do to end the thread after the Join?
The 3rd thread has a while loop and in side that while loop i got a nested for statement.
One for statement in another one.
I check for the volatile variable in my while loop. And once more inside the deepest for statement.
That should be enough right?
|
|
|
|
|
If you check the variable, it should end.
Are you sure there is nothing more happening?
Can't you put a break point in the last join and, instead of executing it, as all threads are stopped, check what's happening in the other thread? Maybe put some breakpoints at that moment and then allow the Join to go... you must see exactly what the other thread is doing.
|
|
|
|
|
Yes I'm sure. The variable gets checked alright. But it doesn't seem to stop it.
Here is that thread i'm talking about with the code:
private void FileComparerThread()
{
while (this.stopRequested != true)
{
for (int i = 0; i < Directory.GetFiles(this.dest).Length; i++)
{
string[] filePaths = Directory.GetFiles(this.dest, "*.txt");
for (int j = 0; j < filePaths.Length; j++)
{
if (this.stopRequested != true)
{
if (filePaths[i].ToString() != filePaths[j].ToString())
{
bool comparedFiles = this.FileCompare(filePaths[i].ToString(), filePaths[j].ToString());
if (comparedFiles)
Directory.Move(filePaths[j], Path.Combine(this.dest2, Path.GetFileName(filePaths[j])));
}
}
}
if(filePaths.Length > 3)
File.Move(filePaths[i], Path.Combine(this.dest, Path.GetFileName(filePaths[i])));
Thread.Sleep(5000);
}
}
}
And then I got a this.FileCompare() function that checks 2 files for equality thats it.
I've done some big time debugging. Its just looping those for statements.
Even though the stopRequested = true.
It's not comming out that for statement.
|
|
|
|
|
I think I know what's the problem.
You didn't return in the internal if.
So, stopRequested is true. Your method does not compare the files, but continue in it's loop, waits for 5 seconds and so on.
So, I think instead of:
if (this.stopRequested != true)
{
... some code...
}
You should use:
if (stopRequested)
return;
|
|
|
|
|
Hi,
You're right, About an hour ago i got an else statement after that if-statement with a return.
I'll use your suggestion. Saves me some line of codes :P
Thanks!
|
|
|
|
|
Yustme wrote: the last one doesn't seem to abort
Aborting a thread is a bad idea as it leaves things in an unknown state. But then maybe you're not really calling Thread.Abort() at all?
Yustme wrote: this.stopRequested = true;
This doesn't do anything, unless your thread is periodically checking that variable. But then you did not show such code, making me doubt you understand how your code is working.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Hi,
I didn't abort the thread. That was just 'speaking'.
I know, i check its state on different places periodically.
The code has grown too big to post it. I've reduced it now.
I didn't see your post yesterday!
|
|
|
|