Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
C

Coding123456

@Coding123456
About
Posts
7
Topics
2
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Cannot show multiple values for combobox DisplayMember?
    C Coding123456

    I realise this is an old post but it might help someone else out. I needed to do a similar thing but I didn't want to concatinate fields in the sql used to fill the datatable as it was contained in an existing stored procedure. So I filled the datatable with the records I wanted then basically added another column in the datatable which was my concatinated display value. I then used this new column as the DisplayMember. assuming we have a datatable which is populated with "AccountCode" and corresponding "AccountName" eg "ABC" , "Freds workshop" "XYZ" , "Bills Lighting"

    'Add a new calculated column to the datatable
    Dim DataCol As DataColumn = New DataColumn("Display")
    DataCol.DataType = System.Type.GetType("System.String")
    DataCol.Expression = "AccountCode + (' - ') + AccountName"
    dt.Columns.Add(DataCol)

    'Now setup the combobox
    cmbAvailableCompanies.DataSource = dt
    cmbAvailableCompanies.DisplayMember = "Display"
    cmbAvailableCompanies.ValueMember = "AccountsCode"

    It will now display "ABC - Freds Workshop" and "XYZ - Bills Lighting" in the combobox. The only issue I have found is if I turn sorting of the combobox on the SelectedValue of the combobox gets out of sync with the Displayed value. I haven't worked this out yet but for the time being I can live with it.

    Visual Basic question

  • Setting the DataTable object to Nothing
    C Coding123456

    Dave Kreskowiak wrote:

    I think you have left out some things in your description. Setting the datatable reference to Nothing destroys the table, colums, data, everything. You cannot reuse that object. Now you're saying you WANT to reuse the datatable and its columns, but no data. Which is it??

    Setting the datatable to Nothing I agree it will destroy the table. Which is fine as I don't want to reuse it's columns or data. I just want to be able to test if the datatable is Nothing and if so run some code. Or sometime later when if it does contain data, run different code. To put it a different way, if dt is nothing I know there is no data associated with it, otherwise I can read data from the datatable. I am using its 'nothingness' as a flag Thanks for your comments to date I appreciate your interest. I realise that I may use one word that inadvertently changes the the exact meaning of what I am trying to describe.

    Visual Basic csharp visual-studio com performance tutorial

  • DataTable Reset method
    C Coding123456

    The MSDN documentation states that the Reset method 'Resets the DataTable to its original state' Now I may be a bit paranoid here thinking there could be a few interpretations of 'original' but here goes. Does anyone know what is classified as the 'Original State'? If the datatable contained data and then the Reset method was called. Do they mean it is the state when it was declared which has the structure of the datatable object but no data, or some other state?

    .NET (Core and Framework) question

  • Setting the DataTable object to Nothing
    C Coding123456

    Hi Colin, you say that

    Colin Angus Mackay wrote:

    Setting the reference to Nothing makes it unreachable (so long as there are no other references to it.) If the object implements the IDisposable interface then you should, as a matter of good practice, call Dispose on the object.

    When I set the datatable to Nothing I am making the data unreachable which is what I want to do and it also gives me a condition I can test for to control program flow i.e. if dt is nothing then... I don't want to dispose it as I may want to do something like this;

    If dt is nothing then
    return "Default Name"
    else
    return cstr(dt.rows(0).item("Name"))
    end if

    Colin Angus Mackay wrote:

    I don't see a problem with that. If you set it to nothing and there is a consirerable amount of time before assigning new data then do so because in the intervening time the Garbage Collector can get it. If it is the very next statement where you assign data I don't really see much point.

    So you are saying when I set the dt=nothing the data it pointed to will get cleaned up and dt still hangs around safely for me to reuse. This is reasssuring. The time to assign new data may be immediate or after some time. It is really not the very next statement I have just simplified the code. Thanks for your reply

    Visual Basic csharp visual-studio com performance tutorial

  • Setting the DataTable object to Nothing
    C Coding123456

    Thanks for your suggestion Dave and your right I am dumping old data but also setting the datatable to a state where I can test it and control program flow. I did't go into too much detail in my original post but the Datatable is inside a class that stays around while the program is executing. I use various properties of the class to retreive and manipulate the data in the datatables (more than one) inside the class. As the datatables stay around for the life of the class and therefore the program, I can't really just create a new one as my properties need to reference the same datatable (which contains data or I set to Nothing indicating no data) to make sense of the data and the program needs to reference the same class. If the datatable happens to be disposed (as it contained no data) as in your example I would get an error when I reference it.

    Visual Basic csharp visual-studio com performance tutorial

  • Setting the DataTable object to Nothing
    C Coding123456

    Hi Christian, I'm not trying to free memory. I'm setting the Datatable to nothing so I can test for it being nothing later in the code so I can control program flow based on if I loaded it with data or not. I agree with your suggestion though if I wanted to free memory.

    Visual Basic csharp visual-studio com performance tutorial

  • Setting the DataTable object to Nothing
    C Coding123456

    Hi All, I am interested to hear the pros and cons of two approaches and reusing a DataTable object or perhaps someone has a better idea. I have a DataTable object that I reuse. By this I mean I load it with data (using da.fill(dt)) use the data in the program, I then reload it with different data and use this new data. It does not go out of scope. Sometimes I don't want it to hold data as there is none to hold so I assign the DataTable to nothing (dt = Nothing). I do this so I can use the condition if dt is nothing then ... Using .NET apparently the garbage collector does not destroy the object as long as I don't go out of scope, so when I load it with data again it holds the new values. This all works fine but in the microsoft documentation it has a phrase that makes me wary using this approach. It states "For example, if an object is set to Nothing inside a procedure and the next line of code creates an object of the same name, the first object may not yet be destroyed and a reference to the new object might incorrectly return the first object." The full reference is here : http://msdn.microsoft.com/en-us/library/7cx5cffd(VS.71).aspx[^] When I assign new data to the object by using the dataadapter fill command I suppose I am just assigning a reference to the memory that holds the data so I don't know if the above really applies. For a simple example without all the fluff, consider; dim dt as new Datatable da.fill(dt) (other code) dt = nothing (other code) da.fill(dt) (other code) I'm not entirly comfortable with this approach as it seems bad to set it to nothing if I actually intend to reuse it again and again. My alternative to the above if it is really bad is instead of setting it to nothing to use the dt.reset method to clear any data out of the datatable for example; dim dt as new Datatable da.fill(dt) (other code) dt.Reset (other code) da.fill(dt) I can then use, if dt.tables.count=0 orelse dt.tables(0).rows.count=0 then ..... to test for no data elsewhere in the code. I am assuming that reset clears out all data and when microsoft say 'Resets the system.data.datatable to its original state' I assume the original state is the state it was at when it was created? Any Thoughts on this would be appreciated

    Visual Basic csharp visual-studio com performance tutorial
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups