|
Define your SQL connection as a static instance, this will allow u to check whether its open or not in a threadsafe manner (without having to resort to locks).
|
|
|
|
|
Don't resort to static methods to solve a problem that's pretty simple as leppie suggests. I mean, it may be a good way to go, but to use it simply to paint-over a small problem isn't a good way to fix anything. That's shoddy programming and will bite you in the ars at some point more than likely.
Instead of using DBConnect.State.ToString for a case-sensitive string comparison (not a good idea), use bitwise operators to determine if the connection is closed, something like this:
if ((DBConnect.State & ConnectionState.Closed) != 0) return; Several enumeration members can be set at once (this could always be true when an enumeration is attributed with the FlagsAttribute ) so you need to mask any values and see if they're set.
For instance, ConnectionState.Closed = 0, and ConnectionState.Broken = 16. These could both be set at the same time.
0 = 00000000
16 = 00010000 If both were set, the State property would be set to 16, or 00010000. If you use a bitwise AND to mask Closed , then it looks like this:
State = 00010000
Closed = 00000000
& = 00000000 = 0 Without AND'ing it, the result is 16 which does not equal 0.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Health,
Thank you, I got your point. That's why i'm having messages that connection is already open even though I already closed it or the other way.
Anyway, Thank guys for all your help. I was able to fix my problem by using the Monitor class. It's under System.Threading. I locked the connection before executing the insert statement and then unlock unlock it once it's done. In that case the succeeding insert will not be able to get the 1st connection until it's release.
for other guys who will have the same problem.
CODE:
DBCommand = new SqlCommand(strInsert,DBConnect);
Monitor.Enter(DBConnect);
try
{
if (DBConnect.State.ToString() == "Closed")
{
DBConnect.Open();
}
DBCommand.ExecuteNonQuery();
DBCommand.Connection.Close();
DBConnect.Close();
Monitor.Exit(DBConnect);
}
catch (Exception objerr)
{
System.Diagnostics.EventLog.WriteEntry("ossrop", "Insert Failed : " + objerr.Message);
}
PS: If you have comments with my solution, feel free to modify it so that other people whill read this email in the future will get the correct solution.
|
|
|
|
|
Yes, I do have a comment: you aren't using the Monitor correctly. If an exception is thrown, your Monitor is never signalled and your app will handle on the next request to lock the monitor!
Instead, use the lock statement like so:
lock (DBConnect)
{
} ...which, FYI, gets compiled as:
Monitor.Enter(DBConnect);
try
{
}
finally
{
Monitor.Exit(DBConnect);
} See the difference? If an exception occurs, no matter what the monitor is signalled and any exception is either handled within any nested catch blocks, or is passed up the caller stack.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
as above, I'm importing function from unrar.dll but I'm stucked in converting this..... thanks
|
|
|
|
|
|
|
Hi,
How to use "Custom actions" to run a SQL script in a file named aa.sql?
Thanks a lot!!
Alan
|
|
|
|
|
I don't know if this is best way , if you want to create database and its data and objects in your user machine,attach your database files to your project and copy it to user machine , then use sp_attach_db and run it from your application with ADO.NET classes to attach them to your server.
Mazy
"I think that only daring speculation can lead us further and not accumulation of facts." - Albert Einstein
|
|
|
|
|
Another way which I found it now is run osql utility and pass file name to it. See SQLServer Online Book for in frmation about it.
Mazy
"I think that only daring speculation can lead us further and not accumulation of facts." - Albert Einstein
|
|
|
|
|
Mazy:
Thank you very much for your solution,
but is it possible that I can use the "Custom Action" to run the SQL Script when I create the setup project(just like run some Exe files). I got some information from an article and it said it is ok to do so. but I don't know how. would you please tell me how to do it.
Thanks a lot!!
Alan Shen
|
|
|
|
|
It is possible, but there's many problems that may arise from doing this. For instance, osql.exe is only installed on a machine with SQL Server or MSDE installed. In your case, this may not be a problem, but it's something you must consider. Also, the executed might not even be in the PATH environment variable, so simply calling it won't work.
See the article, See the MSDE Deployment Toolkit in Action[^] on MSDN for a discussion about different ways about using Installer classes, if not using the tookit itself.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everyone,
I have been writing a new c# custom control and i was wondering when you specify properties, how do you change them to things like True/False, Text etc.
I was also wondering if it was possible to disable properties somehow.
I have this code for one property:
<br />
[Bindable(true), Category("Colours"),<br />
Description("Changes the colour on the left hand side of the button")]<br />
<br />
public Color LeftColour<br />
{<br />
get { return m_color1; }<br />
set { m_color1 = value; Invalidate(); }<br />
}<br />
Which would of course let you change a color, and add a description, but how could i go about changing the type to text or something, or disable it ?
Thanks for any replies
|
|
|
|
|
Have a look at TypeConvertor's.
There is a wealth of info in/on MSDN describing custom controls.
|
|
|
|
|
I want to read serial number of drive (flopy cd or spec drive)
i think there is a api for this but i dont know ,
please help me
Regards Amir jalaly
|
|
|
|
|
|
i want to connect tow computer with modem and send information throw tel line without internet .
may be i must intialize connect and something else...
please help me
Regards amir jalaly
|
|
|
|
|
how can connect tow computer with modem . i mean send information in tel line without internet.
i need intialize connection and send file .
Regards' Amir jalaly
|
|
|
|
|
Hey!
I have a little problem, in datagrids there are always a empty row at the bottom.. how can i get this to dissapear? i heard that it can be done by setting the whole datagrid to readonly.. but that doesnt work since i have a column that has to be writable.. i just don't want the user to be able to write a "new" row.. anyone that can help?
|
|
|
|
|
You have to set AllowNew to false on the underlying DataView:
CurrencyManager cm = (CurrencyManager)myGrid.BindingContext<br />
[myGrid.DataSource, myGrid.DataMember];<br />
((DataView)cm.List).AllowNew = false;
|
|
|
|
|
Does anyone know, or has anyone ever tried to change the color of the arrow button at the side of a listbox or combobox control? or is it possible?
Thanks,
Mausy
|
|
|
|
|
Hello!
I need some help here. I want to be able to alter components on a form from another form. I figure i should be using delegates in some way but i can't really get it to work.. can someone please write me a small example? something small like changing a label.text on form1 from a button on form2?
|
|
|
|
|
in Form2:
public event EventHandler ButtonClicked;
private void Button_Clicked(object sender, EvenArgs e)
{
if(this.ButtonClicked != null)
this.ButtonClicked(this, EventArgs.Empty);
}
in Form1:
subscribe to ButtonClicked - Event of Form2 somewhere before you show Form2, just like this:
Form2 form2 = new Form2();
form2.ButtonClicked += new EventHandler(this.ChangeLabel);
form2.Show();
private void ChangeLabel(object sender, EventArgs e)
{
this.label1.Text = "Button on Form2 has been clicked.";
}
|
|
|
|
|
ok here is my print page event
//files is a string[] which has my file name in it
private void PrintPage(object sender ,PrintPageEventArgs e)
{
System.IO.FileStream fs ;
for(int j=0;j!=files.Length;j++)
{
fs= new FileStream(files[j],FileMode.Open,FileAccess.Read);
Image newImage = Image.FromStream(fs);
e.Graphics.DrawImage(newImage, e.Graphics.VisibleClipBounds);
fs.Close();
e.HasMorePages=true;
}
e.HasMorePages=false;
}
but this only prints out the last file i send to it
how do i fix this to print out all of my files
chad
|
|
|
|
|
Because you're iterating through all your images in the handler that prints one page, simply drawing each image to the Graphics object such that the last one is the one that shows up.
You need to design your code so that each call to your PrintPage event handler uses the next file, perhaps by maintaining an index to which file you want to print and incrementing it after printing your file.
Microsoft MVP, Visual C#
My Articles
|
|
|
|