1. Bundling changes together.

    Date: 02/21/09     Keywords: cms, database, sql, web

    First off, I'll state that I'm working with C# 2008 express edition, SQL Server 2008 Professional edition, and .NET 3.5.

    Second, I'll state that one of the ideas behind this project is to do it without .Net's auto-binding. If it can't be done without auto-binding, it's not going to get done at all, for reasons having to do with extreme customization of the data access code that will be happening way, way down the line. So I need an answer in code, not in designer. Thanks.

    Okay, what I'm doing seems to me to be simple and obvious, but either .Net doesn't agree with me or I'm looking in the wrong place. I have a simple form. It contains a datagridview and three buttons. One button works perfectly--it's the one that closes the form, and we don't need to discuss it here. The other two buttons, however, are giving me fits. They are labelled "save changes" and "cancel".

    The datagridview is bound to a DataTable. This is pretty standard code, I think:

    SqlConnection cnErasmus = new SqlConnection();
                //populate the datatable with the data already in the table.
                tblAuthorType = PopulateDataSet(cnErasmus);
                //Attach DataTable to datagrid.
                dgvAuthorType.DataSource = tblAuthorType;

    (note: it used to be a dataset. I forgot to change the name of the routine.)

    In case you want/need to see the actual filling of the DataTable, I'll put it

            private DataTable PopulateDataSet(SqlConnection cnErasmus)
                string strAuthorTypeSelectQuery = "SELECT AuthType FROM AuthorType";
                using (cnErasmus)
                    using (SqlCommand cmSelectCommand = new SqlCommand(strAuthorTypeSelectQuery, cnErasmus))
                        using (SqlDataReader theReader = cmSelectCommand.ExecuteReader())

    All of this is completely normal. At least, I think it is. Now, the tough part seems to be logging changes. The obvious way to do so, to me (I'm a database guy more than a programmer) is through a transaction object:

    /******************SqlTransaction trnchangeAuthorTypeData = 
                //Must assign both transaction object and connection
                //to Command object for a pending local transaction.
                SqlCommand cmTransactionCommand = cnErasmus.CreateCommand();
                cmTransactionCommand.Connection = cnErasmus;
                cmTransactionCommand.Transaction = trnchangeAuthorTypeData;
                //Whether transaction is committed or rolled back depends
                //on which button the user presses...********************/

    These are the lines just after setting the dgv's data source.

    I've seen this done at least a dozen times in various places on the web, but in every case, the example has some sort of hardcoded INSERT or DELETE statement immediately following, and then a try/catch block with the appropriate transaction.Commit() or transaction.Rollback() statements. The important bit for me is that in every example I've found, all the statements follow one another. It's all very procedural. I want the Commit() or Rollback() to be fired based on which of those buttons gets pressed; the Cancel button will cause a Rollback() and the Save button will cause a Commit(). To me, this seems perfectly logical. My problem is that, unfortunately, trnchangeAuthorTypeData goes out of scope the second we hit the end of that block of code, and so is nowhere to be found when I get to btnSave_Click or btnCancel_Click. Setting it the normal way (trnChancgeAuthorTypeData = new SqlTransaction()) raises an error based on the protection level of SqlTransaction, so I can't declare it the way I declare every other variable. (While no resource I've consulted specifically explains this odd behavior, they all confirm that this is by design.)

    So, after all that, my question is: how do I separate BeginTransaction(), Commit(), and Rollback() into three separate routines? If that's not possible, as I have spent the last twelve hours ascertaining, is there some way other than transactions to make sure that, when the Cancel button is pressed, the system will roll back all changes since either (a) the form was opened or (b) the Save button was last pressed, and that can be done in that manner (with the transaction-analogue starting in the same routine where the datagridview is bound, and finishing in one of the button-press routines)?


    Source: http://csharp.livejournal.com/101781.html

  2. Writing namespace to an XML stream

    Date: 02/11/09     Keywords: html, xml

    I'm trying to write XHTML to an XmlWriter. I'm having trouble generating the standard line:
    http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

    With "writer" as my XmlWriter I have
    writer.WriteAttributeString("xmlns", "http://www.w3.org/1999/xhtml");
    writer.WriteAttributeString("lang", "en");
    writer.WriteAttributeString("xml:lang", "en");

    but writer.WriteAttributeString("xmlns", "http://www.w3.org/1999/xhtml") generates an XmlException: "The prefix '' cannot be redefined from '' to 'http://www.w3.org/1999/xhtml' within the same start element tag."

    What am I missing? How do I get that namespace defined?


    Source: http://community.livejournal.com/csharp/101629.html

  3. Writing namespace to an XML stream

    Date: 02/11/09     Keywords: html, xml

    I'm trying to write XHTML to an XmlWriter. I'm having trouble generating the standard line:
    http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

    With "writer" as my XmlWriter I have
    writer.WriteAttributeString("xmlns", "http://www.w3.org/1999/xhtml");
    writer.WriteAttributeString("lang", "en");
    writer.WriteAttributeString("xml:lang", "en");

    but writer.WriteAttributeString("xmlns", "http://www.w3.org/1999/xhtml") generates an XmlException: "The prefix '' cannot be redefined from '' to 'http://www.w3.org/1999/xhtml' within the same start element tag."

    What am I missing? How do I get that namespace defined?


    Source: http://csharp.livejournal.com/101629.html

  4. add refrence on pre-build

    Date: 02/06/09     Keywords: sql

    First of all, please excuse me for my english.
    I have VS2005 Pro. Is there any simple way to programmatically add reference to project on pre-build?
    I have a problem with SqlCe reference and bad-known "ProjectAssemblies" folder. So all I need is to add reference before build and remove it after the build. At the moment I'm doing it manually.
    May be some VS macro? If not, then may be it could be done by writing a program which will edit solution file before building?
    Thanks in advance.

    Source: http://community.livejournal.com/csharp/100958.html

  5. Enumerate COM port devices

    Date: 02/06/09     Keywords: no keywords

    I am looking for an easy way to enumerate COM port devices (Devices that I see in Windows Device Manager in Ports section). What I found so far is how to get all available com ports, but it doesn't provide any device information.

    Source: http://community.livejournal.com/csharp/100411.html

  6. a couple of C# questions

    Date: 02/02/09     Keywords: java

    Are there good non-MS on-line C# language references that you like?

    Is there a Java like syntax for implements in C#. I have a class that I want to inherit from the base class and I want it to implement an interface as well.


    Source: http://community.livejournal.com/csharp/100198.html

  7. Picturebox with Zoom?

    Date: 01/18/09     Keywords: no keywords

    I'm working on a specialized image viewer. I need to allow viewers to zoom in and then pan around the image. The stock PictureBox control doesn't seem to support doing this. Most ideally, I need to do this with a vector image. I'm starting with PDF but can convert to pretty much anything; WMF, EPS, whatever.

    Any ideas on where to find a component or assembly that'll do this? I've found plenty, but they are multiple hundreds of dollars and are oriented at those needing to do robust image processing, which I don't; just viewing.


    Source: http://community.livejournal.com/csharp/100028.html

  8. New Connection button causes Choose Data Source to shut down

    Date: 12/17/08     Keywords: database

    Okay, this is odd. Visual C# 2008 Express Edition on XPSP3.

    I create a new form and drag a DataGridView onto it. Expand the little arrow to get the DataGridViewTasks dialog. Expand "Choose data source." One data source I have already created in this project is listed. But I need another one, so I click "Add Project Data Source...". Select "Database" from Choose Data Source Type and hit Next. Up comes the Choose Your Data Connection dialog box. The only data connection listed is Northwind.sdf (which is not the data connection I've already created and that is showing in the previous box). I click the "New Connection..." button and instead of getting the New Connection dialog, the Data Source Configuration Wizard closes and I'm back out at the DataGridViewTasks dialog.

    This started happening last night. Before that, I was able to create data sources in this project without a problem. I have rebooted the computer multiple times since it started with no effect at all.

    Anyone seen anything like this before? Thanks.

    Source: http://community.livejournal.com/csharp/99749.html

  9. static data

    Date: 12/17/08     Keywords: database

    I'm working on a project idea that involves creating an army builder application of sorts. I essentially want to build and manage a database of my collection of figures. The catch is that I want this database to build off of a "static database" of the figures available for the game. This "static database" also needs to be able to be updated when new figures are released.

    So my question is this... What is the best way to build this "static database"?

    Source: http://community.livejournal.com/csharp/99580.html

  10. FaultImportOptions?

    Date: 12/04/08     Keywords: web, microsoft, google

    I have just created web service in VS2008. I then created a project to act as a client for this service and tried to import a Service Reference using VS2008. I am getting the following error:

    Microsoft Visual Studio
    Failed to add Service Reference 'ServiceReference1.Reference'.
    Error:Could not load type 'System.ServiceModel.FaultImportOptions' from assembly 'System.ServiceModel, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

    Please use Error List window to view the full error message.


    A Google search for FaultImportOptions brings up an MSDN page in Portugese, but nothing else. Browsing the System.ServiceModel assembly reveals no FaultImportOptions class or property.

    Any ideas?

    Source: http://community.livejournal.com/csharp/99117.html

  11. Visual Studio && Design Patterns

    Date: 11/25/08     Keywords: no keywords

    Several years ago (i.e. somewhere in the 1990's), I found a 'plugin' for the JBuilder IDE, that provided a big library of design patterns. You could page through the patterns (with UML class diagrams of the pattern), or you could search for a pattern. Then, with just a few clicks, you could add the pattern to your project.

    I miss something like that today. Does anyone know, if there exists such an add-in for the Visual Studio? It doesn't matter if it is payware, as long as there's an evaluation version.

    Source: http://community.livejournal.com/csharp/98946.html

  12. Reference Accessor

    Date: 10/10/08     Keywords: no keywords

    Hello guys. Do any of you know how get a property accessor return a reference. I'm sure it can be done, because the framework does it.

    Here's an example :

    public class Upper {
    	public struct SInner {
    		private string m_str;
    		public string str {
    			get { return m_str; }
    			set { m_str = value; }
    	private SInner m_Inner = new Inner();
    	public SInner Inner {
    		get { return m_Inner; }
    	public void func() {
    		Inner.str = "value";

    The assignment in func() causes a compile error. This is because i need a reference to m_Inner to be returned, not a copy of the value that simply resets the previous value.

    And, yes, there is a reason for it. I know the example seems unnecessarily complex, but it's being dumbed down.

    Thanks for any help.


    Nevermind, I figured it out. Since Inner was a struct, it was a ValueType. Thus, Inner.str was a value (sort of.) I changed Inner to be a class, and that allowed forced it to be returned by reference.

    If that's confusing, just comment. I'll explain it better.

    Source: http://community.livejournal.com/csharp/98794.html

  13. Accessing an XML-RPC service

    Date: 08/11/08     Keywords: xml, yahoo

    I'm trying to access an XML-RPC service from C# that I'm building. I've had absolutely no response from my message on the XML-RPC.net YahooGroup, so I started looking at the only other .Net XML-RPC class library out there: XmlRpcCS, which is only confusing me further.

    The cause of my problems appears to be that XML-RPC.Net seems to require all its proxy objects to be structs. I, however, would like to use class objects, so I can add other functionality into the classes (constructors, the ability to have properties that are masked from the XML-RPC output and so on).

    Now if this were XML serialisation, I would use the attributes that control XML serialisation, such as [XmlIgnore]. Without rewriting half of the class library, though (which would seem to defeat the purpose of using it!), I can't do something like that.

    Does anyone here have any experience of using an XML-RPC library for .Net?

    (Cross-posted to '[info]'ms_dot_net.)

    Source: http://community.livejournal.com/csharp/97906.html

  14. Managed DirectX

    Date: 07/18/08     Keywords: technology, microsoft

    I use Managed Direct 3D for my graphic engine. But Microsoft will turn off support of this technology. Which technology you advice to me? XNA or something else?

    Source: http://community.livejournal.com/csharp/97171.html

  15. appilcation settings - change directory path

    Date: 07/16/08     Keywords: no keywords

    sorry for my english :)
    I'm using Settings tab of project properties (windows application) for creating user settings. But user settings are written into Documents and settings\userId\Local Settings\Application data\appName\appName_blahblahblah\\..
    I want them to be written into file which is located in program directory. Are there any opportunity to do it by changing some settings in VS or small code changes, or it will be easier to write my own settings manager?

    Source: http://community.livejournal.com/csharp/96656.html

  16. Saving variables between sessions

    Date: 06/27/08     Keywords: no keywords

    In short: How do I save variables between sessions?

    In long: I have several C# books, but they only briefly touch on the subject, or delve into databasing, which isn't really what I need. I've skirted by so far by so far by working mainly in projects that don't require the carrying over of settings. I'm mostly looking for an online resource that will teach the concept, not so much just a "do this" sort of deal.

    Thanks for any help you can offer!

    Source: http://community.livejournal.com/csharp/96308.html

  17. Vista/2k3 C# Packet Capturing

    Date: 06/26/08     Keywords: no keywords

    I am trying to find some relatively simple C# packet capture code for capturing incoming/outgoing packets on a single host running Vista and/or Windows 2003 (though it has to work on XP as well).

    I had a very robust RawSocket implementation that did everything I needed very nicely, but Vista broke it (it now only captures incoming packets).

    I don't mind having to use WinPCap, but even so, all of the C# WinPCap wrappers I've tried have failed miserably attempting to discover NICs in Vista.

    If anyone has any information or a link to any information on this topic, I'd appreciate it a great deal.

    The final alternative would be to wrap WinPCap into C# myself, but I don't want to reinvent the wheel -- it's already been done. And I'm starting to tire of P/Invokes in this particular application.

    Any help appreciated.

    Source: http://community.livejournal.com/csharp/96174.html

  18. BackgroundWorker

    Date: 03/15/08     Keywords: no keywords

    I'm playing around with the BackgroundWorker class. Since I've never used it before I was hoping to get some critique. The code works fine but it's probably not using best practices (throughout). I'm using a form with a listbox, button, and timer. Please assume that 'Files' will never contain duplicate names. I don't think any lock() blocks are necessary but I might be wrong.

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Threading;
    using System.Windows.Forms;
    namespace ThreadTest
        public partial class Form1 : Form
            private const int THREADMAX = 3;
            private ArrayList Files, Running;
            private BackgroundWorker[] Bgw;
            private Loader Ldr;
            public Form1()
                Files = new ArrayList(10);
                Bgw = new BackgroundWorker[THREADMAX];
                Running = new ArrayList(THREADMAX);
                Ldr = new Loader();
                for (int x = 0; x < 10; x++) {
            private void button1_Click(object sender, EventArgs e)
                string s;
                while (Files.Count != 0) {
                    s = Files[0].ToString();
                    if (Running.Count < THREADMAX) {
                        for (int x = 0; x < THREADMAX; x++) {
                            if (Bgw[x] == null || !Bgw[x].IsBusy) {
                                Bgw[x] = new BackgroundWorker();
                                Bgw[x].DoWork += new DoWorkEventHandler(Ldr.Load);
                                Bgw[x].RunWorkerCompleted += new RunWorkerCompletedEventHandler(Done);
                                listBox1.Items.Add("Loading " + s);
            private void Done(object sender, RunWorkerCompletedEventArgs e)
                listBox1.Items.Add("Load complete for " + e.Result.ToString());
            private void timer1_Tick(object sender, EventArgs e)
                if (Files.Count == 0 && Running.Count == 0) {
        public class Loader
            public void Load(object sender, DoWorkEventArgs e)
                e.Result = e.Argument.ToString();


    Source: http://community.livejournal.com/csharp/94902.html

  19. MRE's and multithreading

    Date: 03/08/08     Keywords: no keywords

    I have worker threads where the running state is controlled by a ManualResetEvent object. It worked fine until I added a method call while the MRE is Reset (threads paused) and just before it is Set back. Now it appears that the threads don't resume. Is manipulating a "suspended" object messing up the MRE? Apparently it is; I would just like some insight.


    Source: http://community.livejournal.com/csharp/94601.html

  20. Show/Hide password

    Date: 03/04/08     Keywords: asp

    I have an ASP.NET application that I need to allow the user to view the input of a textbox with the textmode set to password by checking/unchecking a checkbox. I tried to just change the textmode to singleline and repost but that didn't work. Has anyone ever accomplished this. I am sure it is an easy implementation but I think I may be over complicating the issue.


    Source: http://community.livejournal.com/csharp/94287.html

Previous page  ||  Next page

antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home