|
Thanks PIEBALDconsult,
I only need a memory representation (string) for XML. No need to write to a file. My question is, why even if I set UTF-8 property, but in my original question and code, UTF-16 header is displayed?
regards,
George
|
|
|
|
|
I'm guessing that it's because .net strings are two-byte Unicode, but I could easily be wrong.
|
|
|
|
|
Thanks PIEBALDconsult,
I agree C# is using UTF-16 as internal encoding approach, but why the XML header UTF-8 which is already set is overwritten by UTF-16?
regards,
George
|
|
|
|
|
Because doing otherwise would be wrong.
What problem are you trying to solve?
|
|
|
|
|
Thanks PIEBALDconsult,
I do not quite understand why I set UTF-8 header, but UTF-16 is output in my original sample. What is the internal operations which steals and changes my original header?
regards,
George
|
|
|
|
|
The XmlDocument.Save and XmlTextWriter operation will only write well-formed XML.
It knows that the StringWriter uses UTF-16 so it sets the proper encoding.
Encoding in UTF-16, but saying it's UTF-8 would yield mal-formed XML.
If you want UTF-8, write it to a file, a StringBuilder won't do it.
|
|
|
|
|
Can I set the encoding of StringWriter from UTF-16 to UTF-8?
regards,
George
|
|
|
|
|
NO, goddammit! You can't! .net strings are UTF-16, and that's it, end of story!
|
|
|
|
|
Thanks PIEBALDconsult,
I have solved this issue by using MemoryStream.
regards,
George
|
|
|
|
|
Hey guys
im trying to write a crystal report... the problem is that the data source comes from dataGridView.DataSource...
it moans "DataTable already belongs to another DataSet"... i'm thinking i need to send it by value(create a copy that doesn't belong to anything) instead of just passing it to the constructor of the reportViewerForm which is by reference... right?
if my assumtions are correct, how do i send (DataTable)dataGridView.DataSource the reportViewerForm by value?
ive got this now
FrmReportViewer frmReportViewer = new FrmReportViewer((DataTable)dataGridView.DataSource);
thanx
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
Haven't worked with CR, so I can't tell you how to fix that error - but I can tell you that you cannot pass objects by value in C#. At best, you can clone them and pass the clone.
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
I need to use SHGetFileInfo to display large icons.For small icon display it works fine. If i change the parameter to display large icon (SHGFI_LARGEICON) it does not display the file with large icon, it always displays small icon. Could you pls help me
hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
hImgLarge = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON]);
|
|
|
|
|
Have you defined the constants correctly?
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
Dave
|
|
|
|
|
After a bit of 'googling' is appears that SHGetFileInfo ... _LARGEICON returns the 32x32 icon, not any greater than that.
You need to call SHGetFileInfo to get the image list index, then SHGetImageList to get the larger icons.
Some code here[^] that may be useful.
Dave
|
|
|
|
|
I have a data grid that contains a checkbox in its one of the column. I have another column that needs to get populated with current date after we check the checkBox, as soon as we click on checkBox but not after we leave the CheckBox cell.Please help help me.
Thank you.
|
|
|
|
|
|
I tried every events but it gets populated only after we leave the cell after we click on it.Actually I want it to get populated right after we click on the checkbox.
Thank u.
|
|
|
|
|
Hi all,
I'm having a hard time dealing with some errors while using multiple threads to access a local MS SQL Express database.
I will try to explain what my programm does:
1.
There is a socket connection between this program(A) and an other program(B). Depending on the information received from B this program will query the database and update some values.
2.
There is also a thread running who checks the database, looks for updates, and sends them to via the socket to programB.
3.
Then there is also a button on the GUI who send custom commands to programmB. ProgramB will respond to that by sending data back to my program. (like section 1)
Depending on the amount of database access required I receive some errors like:
"System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed"
or
"There is already an open DataReader associated with this Command which must be closed first"
or
"The connection was not closed. The connection's current state is open"
If have been playing for a while and changing the code, but nothing helped. I guess it has something to do whith to different threads want to acces the same connection at the same time. Can anyone help me on this?
Thanks in advance!
Michiel
|
|
|
|
|
Open new connections as and when you need them, if you have a single connection you can only run a single command at a time and as you have found if that command is a data reader it can be running for some time.
You could of course queue up all the requests but that would slow everything down, your threads would all be waiting in line to get their data.
There is one other option which is MARS(Multiple Active Result Sets), this was added in MSSQL2k5 but it does have some limitaitons.
MARS[^]
|
|
|
|
|
The thread who is pooling the database does this every 0,5 second. I could make a separate SQLConnection which only will be used by this thread.
On the other site there is the Socket connection. I have not a very good indepth knowledge on how this works, with AsyncCallBacks etc... Are these callbacks (when data is received) handled by the same Thread. Or does each of them has its own thread. When they share the same thread I would probably have only 2 different sql connections: 1 for the Thread who pools the database and the second one for the socket connection, data callbacks.
When each callback is handled by an other (new) Thread, I probably need a new connection for each callback.
Could you let me know if this is the right way? Or am I missing something?
Thanks in advance!
Michiel
|
|
|
|
|
Michiel wrote: The thread who is pooling the database
You mean polling? Pooling is something quite different.
As I said in the first post I personally go for creating a connection when I need one and disposing it as soon as I'm done with it. This isn't appropriate for databases whic have limitaitons on the number of connections but with MSSQL Express this shouldn't be a problem.
|
|
|
|
|
let me give you some example code:
while(true)
{
}
This is in a short way what the thread does. Just quering/pooling the database for changes.
|
|
|
|
|
If I remember correctly, the current SQL Native Client (preferred when talking to SQL Server 2005) doesn't support connection pooling. This means you get a performance hit if you solve your problems by creating, opening and closing a separate connection each time you touch the database (each time you will cause negotiations between the client and SQL Server before you start doing actual work). If connection pooling was supported, this wouldn't be the case, meaning it would be the simplest solution and wouldn't be a performance issue (connection pooling makes it extremely fast to open/close new connections as a number of "closed" connections are kept open in the background, ready to use by the next attempt to open a new connection).
As is, it isn't always workable (depending on how often you touch the database, of course - if it's just once an hour you should of course connect and close each time).
My solution when doing what you're doing (I have a couple of apps that have similar structure) is as follows:
- One thread for listening to the TCP socket and accessing the database accordingly: Has its own SQL Server connection, opened when the thread starts and kept as long as it runs.
- One thread (the main UI thread, usually) that reacts to user interaction and accesses the database accordingly: Has its own SQL Server connection, opened when the application starts and kept as long as it runs.
- One thread that loops with a Thread.Sleep with some appropriate interval and checks the database for stuff to do: Has its own SQL Server connection, opened when the thread starts and kept as long as it runs. NOTE: If the checks and actions aren't time-consuming you could do them from a Timer object in the UI, using the connection of the UI thread (as the Windows Timer object lives on the UI thread and won't fire when some other UI event handler is running).
If your application is meant to stay running for long times and don't need to touch the database frequently, you could wrap your database code in a class that opens the actual connection on demand and closes it after some time of inactivity (purpose: minimize the number of open connections to the SQL Server). But if I understand your scenario correctly (reading between the lines), this isn't the case.
Finally, a short warning: Beware of classes that retain a reference to the connection and use it unexpectedly. You need to make sure that each distinct action on the database doesn't leave any SqlCommand, SqlDataReader or similar objects laying around (using blocks are Good Things). For instance, don't create a reader in the TCP socket listener thread and store it for use by the UI thread - you're bound to run into threading issues when the UI thread starts reading from it (that particular example is actually even worse than that - the reader will block the connection until it is closed, unless you have MARS enabled). Always consume your data (read it into variables, a disconnected DataTable or whatever) immediately. If you can't due to large volumes of data, you have to create a brand new connection for each such action.
Note: Reading your error messages, I think you may have the SqlDataReader issue. As long as one of them is open and alive, you can't do anything else on the same connection (not even on the same thread). Unless (which has already been said by others) you enable multiple result sets (MARS) - but that won't solve any other threading issues.
Was this helpful?
--
Peter
|
|
|
|
|
PeterTheSwede wrote: If I remember correctly, the current SQL Native Client (preferred when talking to SQL Server 2005) doesn't support connection pooling. This means you get a performance hit if you solve your problems by creating, opening and closing a separate connection each time you touch the database (each time you will cause negotiations between the client and SQL Server before you start doing actual work). If connection pooling was supported, this wouldn't be the case, meaning it would be the simplest solution and wouldn't be a performance issue (connection pooling makes it extremely fast to open/close new connections as a number of "closed" connections are kept open in the background, ready to use by the next attempt to open a new connection).
If you have MDAC installed it supports Connection Pooling just fine, which is why you always include MDAC along with the Native Client installer Heh it'd be extreamly painful making larger systems if it wasn't supported :/
|
|
|
|
|
Hmmm... I was under the impression that this was the case only under IIS. But a few minutes on Google I'm now convinced you are right! How did I miss that? Thanks for the enlightenment!
--
Peter
|
|
|
|