Wednesday, April 13, 2011

What is the best data structure for this in-memory lookup table?

I need to store a lookup table as an instance member in one of my classes. The table will be initialized when the object is constructed. Each "row" will have 3 "columns":

StringKey (e.g., "car")
EnumKey (e.g., LookupKeys.Car)
Value (e.g, "Ths is a car.")

I want to pick the data structure that will yield the best performance for doing lookups either by the StringKey or the EnumKey.

It's kind of awkward having 2 keys for the same dictionary value. I've never encountered this before, so I'm wondering what the norm is for this type of thing.

I could make a Key/Value/Value structure instead of Key/Key/Value, but I'm wondering what type of performance impact that would have.

Am I thinking about this all wrong?

From stackoverflow
  • Well ... "Wrong" is a harsh way of putting it. I think that because the most common dictionary is "single key to value", and a lot of effort goes into providing efficient data structures for that (maps), it's often best to just use two of those, sharing the memory for the values if at all possible.

  • You have two hashmaps.

    • One from StringKey to value.

    • One from EnumKey to value.

    You do not have to duplicate all the Value instances, those objects can be shared between the two hashmaps.

    If it's a LOT of items, you might want to use two treemaps instead of two hashmaps. But the essential principle ("Share the Values") applies to both structures. One set of Values with two maps.

    vg1890 : OK - so in my example, the "value instances" are just strings. I'll make 2 dictionaries (one with StringKey, one with EnumKey) whose values contain the same string reference variable. Does that sound right?
    S.Lott : Precisely. In Python that's all there is to it. In Java, there's a string.intern() which assures that all intern()'d strings are reduced to a common string pool, eliminating some possible redundancy.
    vg1890 : I'm using C#...do you know if .NET will make a copy of the string when I add it to each dictionary?
    S.Lott : Add a *reference* to each dictionary. String exists once. Many references to one string.
    vg1890 : Got it. The dictionaries get their own *references* to the string, but they all point to the same string object. string s = "joe"; dct1.Add("key", s); -- even though the parameter is being passed is called s, dct1.Add gets its own reference to "joe". Thanks!
  • Is it really necessary to key into the same structure with both types of key? You probably don't need to rebuild a complex data structure yourself. You could do some sort of encapsulation for the lookup table so that you really have two lookup tables if memory is not an issue. You could use this encapsulating structure to simulate being able to pull out the value from the "same" structure with either type of key.

    OR

    If there is some way to map between the enum value and the string key you could go that route with only having one type of lookup table.

  • LINQ's ILookup(TKey, TElement) interface may help. Assuming your Dictionary is something like:

    Dictionary<carKey, carValue> cars;
    

    You could use:

    ILookUp<carValue, carKey> lookup = cars.ToLookup(x => x.Value, x => x.Key);
    

    (...actually I think I might have slightly misread the question - but an ILookUp might still fit the bill, but the key/value set might need to be the key and the enum.)

  • If every value is guaranteed to be accessible by both types of keys, another idea would be to convert one type of key to another. For example:

    public Value getValue(String key)
    {
        dictionary.get(key); // normal way
    }
    
    public Value getValue(Enum enumKey)
    {
        String realKey = toKey(enumKey);
        getValue(realKey); // use String key
    }
    

    You could have your Enum implement a toKey() method that returns their String key, or maybe have another dictionary that maps Enum values to the String counterparts.

c# Drag & drop in listview

I've got a listbox from which I'm dragging into the ListView. Now I have groups in the ListView so when the item from the ListView is dropped at the point of the listviewgroup it has to add it under that group.

This is the code which handles the drop.

    private void lstvPositions_DragDrop(object sender, DragEventArgs e)
    {

        var group = lstvPositions.GetItemAt(e.X, e.Y);
        var item = e.Data.GetData(DataFormats.Text).ToString();
        lstvPositions.Items.Add(new ListViewItem {Group = group.Group, Text = item});

    }

I didn't find a function that could give the groupitem, so I used GetItemAt from which I also have access to the listviewgroup.

But GetItemAt always returns null.

Am I doing something wrong? Is there a better way to accomplish this?

From stackoverflow
  • First, I assume you're using a ListView, not a ListBox, as ListBox does not contain a GetItemAt member.

    To solve your problem, convert the point to local coordinates:

    private void lstvPositions_DragDrop(object sender, DragEventArgs e)
    {
       var localPoint = lstvPositions.PointToClient(new Point(e.X, e.Y));
       var group = lstvPositions.GetItemAt(localPoint.X, localPoint.Y);
       var item = e.Data.GetData(DataFormats.Text).ToString();
       lstvPositions.Items.Add(new ListViewItem {Group = group.Group, Text = item});
    }
    
    Gerbrand : Okay that worked.
  • Did that solution worked for u?

    because if u drop the in blank space in ListView

    lstvPositions.GetItemAt(..) will return nothing

    Gerbrand : This works good in my app. In my app there can't be blanks dropped. So I don't have this problem.

What are my binding options for a self hosted cross domain WCF service with remote thick clients?

I'm trying to build a WCF self hosted service (eventually in a windows service) that will receive binary and text base messages from remote thick clients that have no accounts on my hosted machine. I'm trying to figure out both my binding options and security options, and in reading the patterns and practices guides, my head has completely spun around at least once.

The clients would be authenticated against a custom SQL based method, so I'd like to be able to pass that info in the initial login request and then set an authorization token of some kind. (This part of the problem is probably outside the scope of the question, but I included it in case it might make a difference.)

Any thoughts at all would be very helpfull.

Ryan

From stackoverflow
  • The choice of binding and security option depends on the usage of your WCF service. Is it just for your rich client or are you planning to expose it to the world as API? If it's just for your rich app, does it run on LAN or over untrusted, unreliable Internet?

    With WCF you can configure the service to expose multiple endpoints with different bindings, for example both SOAP and REST. In general, I'd start with something stateless and lightweight like basicHttpBinding and webHttpBinding, passing user and password on every request. Once you have that up and running you can optimize cache authentication, provide binary endpoint etc.. only if it actually helps.

  • There's no need to have just one binding. Having said that if it's self hosted you're "on your own" here. I've never looked at what's involved.

Django Admin SelectMultiple Widget

In my model I have many to many relationship between 2 tables Users and Groups. In the admin interface I see the SelectMultiple widget for Groups. Actually, I am using filter_horizontal, I see the available groups and the selected groups in 2 separate lists. Is it possible to filter the list of available groups that I can see (based on some criteria). I do not want to show all the groups in the groups table. Thank you

From stackoverflow
  • In your form class, you can specify a custom queryset for the group-field, which then determines which Group-instances are available in the form:

    class UserForm(forms.ModelForm):
        # override the default groups field
        groups = forms.ModelMultipleChoiceField(
            queryset=Groups.objects.filter(YOUR_CONDITIONS),
            widget=forms.SelectMultiple,
        )
    
        class Meta:
            model = User
    
  • How do I change the group widget that I see in the admin

Searching for a string 'somewhere' in a database

Here's my problem: I'm looking at someone's Postgresql based database application for the first time and trying to find what is causing certain warnings/errors in the system's logfile. I don't know anything about the database schema. I don't know anything about the source code. But I need to track down the problem.

I can easily search for string contents of text file based code like php and perl, using the UNIX command 'grep'; even for compiled binaries I can use use the UNIX commands 'find' and 'strings'.

My problem is that some of the text produced in the logfile comes from the database itself. Checking the error logfile for the database yields nothing useful as there are no problems with the queries used by the application.

What I would like to do is exhaustively search all of the columns and all of the tables of the database for an string. Is this possible, and how?

Thanks in advance for any pointers. The environment used is Postgresql 8.2, but it would be useful to know how to do this in other flavors of relational databases as well.

From stackoverflow
  • I am not familiar with Postgresql, but I would think that, like SQL Server, it has meta-data tables/views that describe the schema of the database (for SQL Server 2005+, I'd be referring you to sys.tables and sys.columns). The idea would be to generate a series of ad-hoc queries based on the table schema, each one finding matches in a particular table/field combination and pumping matches into a "log" table.

  • I've used variants of this in the past.

  • It may not be optimal, but since I already know how to grep a text file I would just covert the database to a text file and grep that. Converting the database to a text file in this case would mean dumping the data using pg_dump.

    The quickest/easiest/most efficient way isn't always elegant...

    Joshua Berry : Quick and dirty: I like it. This solution works well for those of us more familiar with regex than SQL queries and functions. thanks!

What happens to an existing DB2 view, if the table is dropped ?

If we have created a view on an existing DB2 table and then drop the table. What will happen to the view ?

From stackoverflow
  • The view becomes invalid/inoperative. Attempts to select from it will fail.

    To try it:

    create table TEST_TABLE (
    TEST_COL INTEGER
    );
    
    INSERT INTO TEST_TABLE VALUES(1);
    
    SELECT * FROM TEST_TABLE;
    
    create view TEST_VIEW AS
    SELECT * FROM TEST_TABLE;
    
    SELECT * FROM TEST_VIEW;
    
    DROP TABLE TEST_TABLE;
    
    SELECT * FROM TEST_VIEW;
    

    The last statement gives the error:

    [IBM][CLI Driver][DB2/NT] SQL0575N  View or materialized query table
    "TEST_VIEW" cannot be used because it has been marked inoperative.
    SQLSTATE=51024
    
  • When a view is invalidated, as shown in the above example, DB2 will allow you to recreate that view without dropping it first. This makes it possible to re-run your view DDL files (or simply dump the TEXT column of SYSCAT.VIEWS and execute that).

Does the order of columns in a WHERE clause matter?

Hi,

Does the order of the columns in a WHERE clause effect performance?

e.g.

Say I put a column that has a higher potential for uniqueness first or visa versa?

From stackoverflow
  • With a decent query optimiser: it shouldn't.

    But in practice, I suspect it might.

    You can only tell for your cases by measuring. And the measurements will likely change as the distribution of data changes in the database.

  • If you are ANDing conditions the first not true will return false, so order can affect performance.

    Harry : I don't see why this was downvoted +1
    SpoonMeiser : Upvoting because you think a downvote was unfair, rather than because you believe the answer merits an upvote isn't great practice. I believe that a query optimiser will re-arrange the rules, rather than just employ lazy evaluation.
    mwigdahl : SpoonMeiser is correct; the optimizer (at least for SQL Server) uses more complex logic than simple C++-style evaluation.
  • It all depends on the DBMS, query optimizer and rules, but generally it does affect performance.

    If a where clause is ordered such that the first condition reduces the resultset significantly, the remaining conditions will only need to be evaluated for a smaller set. Following that logic, you can optimize a query based on condition order in a where clause.

  • For Transact-SQL there is a defined order of evaluation for the condition of the WHERE clause. The optimizer may be able to detect when the order may be rearranged and still be semantically equivalent, but I suspect that the transformations that it applies are relatively simple and it will be possible to construct a condition that performs suboptimially based on the ordering and grouping of the operators. Simplifying your search condition should improve the ability of the optimizer to handle it.

    Ex:

     WHERE (a OR b) AND (b OR c)
    

    could be simplified to

     WHERE b OR (a AND c)
    

    Clearly in this case if the query can be constructed to find if b holds first it may be able to skip the evaluation of a and c and thus would run faster. Whether the optimizer can do this simple transformation I can't answer (it may be able to), but the point is that it probably can't do arbitrarily complex transformations and you may be able to effect query performance by rearranging your condition.

    EDIT: With regard to your question about ordering based on uniqueness, I would assume that the any hints you can provide to the optimizer based on your knowledge (actual, not assumed) of the data couldn't hurt. Pretend that it won't do any optimization and construct your query as if you needed to define it from most to least selective, but don't obsess about it until performance is actually a problem.

    Quoting from the reference above:

    The order of precedence for the logical operators is NOT (highest), followed by AND, followed by OR. The order of evaluation at the same precedence level is from left to right. Parentheses can be used to override this order in a search condition. For more information about how the logical operators operate on truth values, see AND, OR, and NOT.

    Lieven : +1. The best explanation of "it depends" I've seen.
  • Unless I have missed something here, this question is not about the Query Optimizers interpretation of the precedence of logical operators but rather how the ordering of columns in the Where clause, based on selectivity, affects the query plan that is produced.

    The query optimizer will determine the most efficient way to select the data you have requested, irrespective of the ordering of the SARGS defined in the WHERE clause.

    The ordering is therefore determined by factors such as the selectivity of the column in question (which SQL Server knows based on statistics) and whether or not indexes can be used.

  • For SQL Server 2000 / 20005 / 2008, the query optimizer usually will give you identical results no matter how you arrange the columns in the WHERE clause. Having said this, over the years of writing thousands of T-SQL commands I have found a few corner cases where the order altered the performance. Here are some characteristics of the queries that appeared to be subject to this problem:

    1. If you have a large number of tables in your query (10 or more).

    2. If you have several EXISTS, IN, NOT EXISTS, or NOT IN statements in your WHERE clause

    3. If you are using nested CTE's (common-table expressions) or a large number of CTE's.

    4. If you have a large number of sub-queries in your FROM clause.

    Here are some tips on trying to evaluate the best way to resolve the performance issue quickly:

    1. If the problem is related to 1 or 2, then try reordering the WHERE clause and compare the sub-tree cost of the queries in the estimated query plans.

    2. If the problem is related to 3 or 4, then try moving the sub-queries and CTE's out of the query and have them load temporary tables. The query plan optimizer is FAR more efficient at estimating query plans if you reduce the number of complex joins and sub-queries from the body of the T-SQL statement.

    3. If you are using temporary tables, then make certain you have specified primary keys for the temporary tables. This means avoid using SELECT INTO FROM to generate the table. Instead, explicitly create the table and specify a primary KEY before using an INSERT INTO SELECT statement.

    4. If you are using temporary tables and MANY processes on the server use temporary tables as well, then you may want to make a more permanent staging table that is truncated and reloaded during the query process. You are more likely to encounter disk contention issues if you are using the TempDB to store your working / staging tables.

    5. Move the statements in the WHERE clause that will filter the most data to the beginning of the WHERE clause. Please note that if this is your solution to the problem, then you will probably have poor performance again down the line when the query plan gets confused again about generating and picking the best execution plan. You are BEST off finding a way to reduce the complexity of the query so that the order of the WHERE clause is no longer relevant.

    I hope you find this information helpful. Good luck!

How to add one day to a time obtained from time()

I have a time represented as the number of seconds elapsed since midnight, January 1, 1970, UTC (the results of an earlier call to time()). How do I add one day to this time?

Adding 24 * 60 * 60 works in most cases, but fails if the daylight saving time comes on or off in between. In other words, I mostly want to add 24 hours, but sometimes 23 or 25 hours.

To illustrate - the program:

#include <time.h>
#include <iostream>

int main()
{
  time_t base = 1142085600;
  for(int i = 0; i < 4; ++i) {
    time_t time = base + i * 24 * 60 * 60;
    std::cout << ctime(&time);
  }
  return 0;

}

Produces:

Sat Mar 11 08:00:00 2006
Sun Mar 12 09:00:00 2006
Mon Mar 13 09:00:00 2006
Tue Mar 14 09:00:00 2006

I want the times for March 12, 13, ... to also be 8 AM.


The answer provided by FigBug pointed me in the right direction. But I had to use localtime instead of gmtime.

int main()
{
  time_t base = 1142085600;
  for(int i = 0; i < 4; ++i) {
    struct tm* tm = localtime(&base);
    tm->tm_mday += i;
    std::cout << asctime(tm);
 }
 return 0;
}

Give me:

Sat Mar 11 08:00:00 2006
Sat Mar 12 08:00:00 2006
Sat Mar 13 08:00:00 2006
Sat Mar 14 08:00:00 2006

Which is what I want. Using gmtime gives me the times at 14:00:00

However, note that all days are Sat. Also, it goes to March 32, 33, etc. If I throw in the mktime function I am back where I started:

#include <time.h>
#include <iostream>

int main()
{
  time_t base = 1142085600;
  for(int i = 0; i < 4; ++i) {
    struct tm* tm = localtime(&base);
    tm->tm_mday += i;
    time_t time = mktime(tm);
    std::cout << asctime(tm);
 }
 return 0;
}

Gives me:

Sat Mar 11 08:00:00 2006
Sun Mar 12 09:00:00 2006
Mon Mar 13 09:00:00 2006
Tue Mar 14 09:00:00 2006

What am I missing???


OK, I have tried out FigBug's latest suggestion that is to use:

 std::cout << ctime(&time);

instead of asctime, but I get the same results. So I guess that my library and/or compiler is messed up. I am using g++ 3.4.4 on cygwin. I copied the files over to Solaris 5.8 and used g++ 3.3 there to compile. I get the correct results there! In fact I get the correct results whether I use ctime or asctime for output:

Sat Mar 11 08:00:00 2006
Sun Mar 12 08:00:00 2006
Mon Mar 13 08:00:00 2006
Tue Mar 14 08:00:00 2006

I also get the correct results (with both output functions) on Red Hut Linux with g++ 3.4.6.

So I guess that I have come across a Cygwin bug.

Thank you for all your help and advice....

From stackoverflow
  • I always had the best result with keeping the timestamps UTC and convert them to the specified timezone (including daylight saving) when you want to display the values.

    This saves a lot of hassle like this (and makes your program independent of time zones.

    Andrew Stein : The timestamps are UTC.
  • use gmtime() to convert the time_t to a struct tm

    add one to the day (tm_mday)

    use mktime() to convert the struct tm back to a time_t

    see time.h for more info

    Edit:

    I just tried it, this works:

    int main()
    {
      time_t base = 1142085600;
      for(int i = 0; i < 4; ++i) {
        struct tm* tm = localtime(&base);
        tm->tm_mday += i;
        time_t next = mktime(tm);
        std::cout << ctime(&next);
     }
     return 0;
    }
    
    FigBug : Can words with an underscore not be in italics?
    Jimmy : use some_word_with_underscores instead of *word*
    Jonathan Leffler : Or put a backslash in front of the underscore.
    Jonathan Leffler : Doesn't using gmtime() instead of localtime() mean that you ignore the very problem the question asks about? In effect, you are always going to add 86400, and never 90000 or 82800?
    Jonathan Leffler : Corollary: the algorithm using localtime() in place of gmtime() should work - if localtime() is sane.
    Andrew Stein : loacltime gives me what I want. I have posted this separately. Thanks...
    FigBug : Whoops, yes localtime() is correct.
    Andrew Stein : Just noticed that I don't get what I want. I have edited the posting accordingly.
  • Just add 24*60*60. It shouldn't fail during DST, since UTC won't ever use DST.

    If it is failing, then you are not using UTC somewhere in your code. Remove the timezone dependence.

    Andrew Stein : The point is that adding one day to say 3/31/2008 8:00:00 AM local time should give me 4/1/2008 8:00:00 AM local time even if DST changes locally overnight on 3/31.
    Pyrolistical : I don't understand, are you using unix timestamp in UTC or not? Unix time stamp has nothing to do with DST.
    Mark Ransom : The conversion from UTC to local time is where the DST hour should be added or subtracted - the UTC shouldn't be affected, as Pyrolistical says.
    Andrew Stein : I have added an example showing what I mean (local for me is US Central time zone),
    Pyrolistical : Then do what FigBug suggests below, but use localtime() instead
    Andrew Stein : Just posted that... Thanks
    Andrew Stein : Just noticed that I don't get what I want. I have edited the posting accordingly.
  • FigBug's solution will work almost every time, but it needs DST fix: tm->tm_isdst = -1

    A positive or 0 value for tm_isdst causes mktime() to presume initially that Daylight Savings Time, respectively, is or is not in effect for the specified time. A negative value for tm_isdst causes mktime() to attempt to determine whether Daylight Saving Time is in effect for the specified time.

    (quoted from mktime spec)

    int main()
    {
      time_t base = 1142085600;
      for(int i = 0; i < 4; ++i) {
        struct tm* tm = localtime(&base);
        tm->tm_mday += i;
        tm->tm_isdst = -1;        // don't know if DST is in effect, please determine
                                  // this for me
        time_t next = mktime(tm);
        std::cout << ctime(&next);
     }
     return 0;
    }
    

    Otherwise there will be a bug (example for Moscow Daylight Saving Time which starts 29 March 2009 01:59:59):

    int main()
    {
        // 28 March 2009 05:00:00 GMT ( local - 08:00 (MSK) )
        time_t base = 1238216400;
    
        std::time_t start_date_t = base;
        std::time_t end_date_t = base;
    
        std::tm start_date = *std::localtime(&start_date_t);
        std::tm end_date = *std::localtime(&end_date_t);
    
        end_date.tm_mday += 1;
    //    end_date.tm_isdst = -1;
    
        std::time_t b = mktime(&start_date);
        std::time_t e = mktime(&end_date);
    
        std::string start_date_str(ctime(&b));
        std::string stop_date_str(ctime(&e));
    
        cout << " begin (MSK) (DST is not active): " << start_date_str;
        cout << " end   (MSD) (DST is active):     " << stop_date_str;
    }
    

    Output:

    begin (MSK) (DST is not active): Sat Mar 28 08:00:00 2009
    end   (MSD) (DST is active):     Sun Mar 29 09:00:00 2009
    

can linq update and query atomically?

Hi,

I need to get 1000 rows from a database, and at the same time tag them as 'in process'. This way, another thread can not take the same 1000 rows and process them as well.

With linq i do something like this:

msgs = (from m in database.messages where (m.status == MESSAGESTATUSINIT) select m).Take(1000).ToList();

ideally I would at the same time set the status to MESSAGESTATUSPROCESSING. Of course this MUST be atomic.

any ideas? Or is it back to SQL?

thanks!

From stackoverflow
  • You can surround the code with a lock{}.

    Lock Keyword

    Toad : True, but I also want other processes (even on other computers) to not get these records. Also, it seems silly to foreach through 1000 records and set it to processing, when it can be done with 1 atomic sql statement
  • Linq itself is not going to offer this ability; Keep in mind Linq == Queries. Data updating happens elsewhere.

    You might be better off in this instance writing a stored procedure to return the results; The sproc can update the table and return the results atomically, and your application then will have a single call.

    eglasius : linq2sql isn't only queries, but you are correct it doesn't support this scenario. There are custom methods on some blogs to do batch updates, but they don't return a result set at the same time.
  • You can't do that in linq2sql. Use a stored procedure, and configure it on the designer to output Message. This way you can manipulate the loaded entities and commit changes normally.

    Check this for the how to do it (specifically Mapping the Return Type of SPROC Methods to Data Model Classes): http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

    Ian Suttle : That's how I'd do it as well.
    Toad : great! thanks!

Can a server in any case request a client for any service?

Can a server in any case request a client for any service?

From stackoverflow
  • How can a server request a client for a service? Its a client that will request the server for a service.

    Well what exactly is your question? Can you add more details?

    chaos : Unless we're talking about X11, in which case client means server and server means client...
  • If you are taking in respect of web application then no. Server will always provide response for any request made by the client.

  • What you can do is install some type of plug in or active x object on the client that runs on the client, and sends information back to the server. However, there are security concerns there. In any case, it is not the server requesting anything, it is the client that runs an application and then connects to the server again.

    If you can be more clear about what you need to accomplish I think you will get a clearer answer.

Please help me clean up my ASP.NET MVC tag soup

I'm creating a menu for my web site. It's got a hierarchical structure.

Here's what I want:

  • Great Grandparent
    • Grandparent
      • Parent
        • Sibling1
        • Sibling2
        • Sibling3
        • Self
          • Child1
          • Child2
          • Child3

HTML:

<ul>
    <li><a href="/389">Great Grandparent</a>
        <ul>
            <li><a href="/456">Grandparent</a>
                <ul>
                    <li><a href="/389">Parent</a>
                        <ul>
                            <li><a href="/986">Sibling1</a></li>
                            <li><a href="/983">Sibling2</a></li>
                            <li><a href="/418">Sibling3</a></li>
                            <li><strong>Self</strong>
                                <ul>
                                    <li><a href="/310">Child1</a></li>
                                    <li><a href="/953">Child2</a></li>
                                    <li><a href="/927">Child3</a></li>
                                </ul>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

Here's the code from my ASPX page. This works:

<% For Each Ancestor As Node In ViewData("Ancestors")%>
    <ul>
        <li><%=Html.RouteLink(Html.Encode(Ancestor.Name), "IdOnly", New With {.id = Ancestor.Id})%>
<% Next%>
            <ul>
<% For Each Sibling As Node In ViewData("Siblings")%>
                <li><%=Html.RouteLink(Html.Encode(Sibling.Name), "IdOnly", New With {.id = Sibling.Id})%></li>
<% Next%>
                <li><strong><%=Html.Encode(Model.Name)%></strong>
<% If Model.Children.Count > 0 Then%> 
                    <ul>
    <% For Each Child As Node In ViewData("Children")%>
                        <li><%=Html.RouteLink(Html.Encode(Child.Name), "IdOnly", New With {.id = Child.Id})%></li>
    <% Next%>
                    </ul>
<% End If%>
                </li>
            </ul>
<% For Each Ancestor As Node In ViewData("Ancestors")%>
        </li>
    </ul>
<% Next%>

That ASPX page seems pretty hard to read. How can I clean that up?

From stackoverflow
  • One thing I would recommend is checking the count of items supposed to be in "li" tags before you write the parent tags, other wise you could end up with empty "ul" tags which will not validate. If that doesn't make sense I'll elaborate later when I have some more time. As far as the formatting goes, perhaps some more indentation might make it easier to read.

    Zack Peterson : I check that there are more than zero children: <% If Model.Children.Count > 0 Then%>
    dhulk : @Zack That's good, all that's left then I'd say would be indentation. If you match the code with the HTML tags indentation-wise it should help with readability so you can easily tell which block goes with which markup.
  • The better solution would be to create a html helper if it doesn't already exist.

    Take a look at this question to get ideas on how to do this

    http://stackoverflow.com/questions/478927/asp-net-mvc-htmlhelper-extensions-for-yui-controls-yahoo-user-interfaces

Send C++ string to C# string. Interop.

I am new to inter process communication and need some help. I want to be able to send a string from a C++ program to a C# program. My problem is that the resultant string is gibberish. Here is my code:

Sending program (C++):

void transmitState(char* myStr)
{

    HWND hWnd = ::FindWindow(NULL, _T("myApp v.1.0"));
    if (hWnd)
    {
        COPYDATASTRUCT cds;
        ::ZeroMemory(&cds, sizeof(COPYDATASTRUCT));
        cds.dwData = 0;
        cds.lpData = (PVOID) myStr;
        cds.cbData = strlen(myStr) + 1;

        ::SendMessage(hWnd, WM_COPYDATA, NULL, (LPARAM)&cds);
    }
}

And the receiving program (C#) (I have already overridden the WndProc):

private void OnCopyData(ref Message m)
{
    COPYDATASTRUCT cds = new COPYDATASTRUCT();
    cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));

    String myStr;
    unsafe
    {
        myStr = new String((char*) cds.lpData);
    }

    label1.Text = myStr;
}
From stackoverflow
  • char* in C++ is ANSI character string (usually one byte per character), char* in C# is Unicode character string (like WCHAR* - two bytes per character).

    You in fact reinterpret_cast from char* to WCHAR*. This won't work. Use MultiByteToWideChar() on C++ side to convert.

  • Your string in C++ is ANSI. You need to convert to Unicode somewhere for C#. It's been a couple of years since I did interop, so someone else will have to tell you exactly how to do that.

  • You'll have to convert your character array from ASCII to Unicode somehow. Here is a page that may help do it from the C# side.

LDAP filter for searching students

I am fairly new to LDAP and AD. I want to create an LDAP filter to show all the students in the AD. But the problem is that the students are in different BASE DN:

OU=STUDENTS,OU=USERS,OU=SOE,OU=FOAE,OU=UNIVERSITY,DC=sepang
OU=STUDENTS,OU=USERS,OU=SOMLC,OU=FOAE,OU=UNIVERSITY,DC=sepang
OU=STUDENTS,OU=USERS,OU=SOCS,OU=FOS,OU=UNIVERSITY,DC=sepang

i.e for each student it is like

CN =khx72b,OU=STUDENTS,OU=USERS,OU=SOCS,OU=FOS,OU=UNIVERSITY,DC=sepang

As you can see students from different faculties are in different places. Given an username how can I search and find if the given user is in the directory?

The objectClass for all the students is 'user'.

Sorry if I'm wrong in anything said above, but my knowledge about LDAP is very small.

Thank you in advance.

From stackoverflow
  • As it seems you are searching for objects of type 'user' which are in OUs called 'STUDENTS' but otherwise have no common parent.

    This cannot be done in one step (i.e. with a single LDAP query).

    You must either retrieve all OUs named 'STUDENTS' and use them as Base DNs one by one, like you've already indicated.

    Or you find a property that all students share (a direct group membership, for example, or a special value somewhere) and use that as the filter. This is a more dangerous approach since nothing guarantees that every student actually has the feature you rely on - some might have been not entered into AD correctly.

  • Tomalak, is right, Microsoft does provide many attributes that you could use for this purpose such as "employeeType", "comment", "department", "company", "department", "divison", etc, but the problem with these is that they are not prepopulated with any information that can help you now. You can start using one of these for future purposes, but then you must maintain that practice in order for it to be consistent. I thing the easiest solution for you is to probably put each of the users into a group that is named similar to the OU name, which should be an really easy task if they're currently in the same OU. then once this is done you can easily create a LDAP query which will then look at the membership of that group like this:

    ((objectCategory=person)(objectClass=user)(memberOf=CN=STUDENTS GROUP,OU=USERS,OU=SOCS,OU=FOS,OU=UNIVERSITY,DC=sepang))
    

    Please note when using the "memberOf" in an LDAP filter the search value must be a complete string to a group, and so you CAN'T use a wildcard such as: memberOf=CN=STUDENTS GROUP*).

    You will still have to maintain a practice that you or someone or something (such as an automated schedule script task) which maintains the group membership to ensure that your LDAP query will be accurate.

    I did see this post which says what your trying to do is possible without having to do anything extra by "Matching Components of Distinguished Names", but I have never seen this before and I could not get it to work. Also take a look at this tutorial on ADO searches to learn more about these things work

what is the built-in javascript function to return the validator state in asp.net

This is a bit of a tough question to ask. I am using asp.net validators, but I want to tie them to a simple click event which I bind with jQuery.

I have found that I can use:

ValidatorValidate($("#<%= rfvFirst.ClientID %>")[0]);

to fire the rfvFirst validator, but I do not know how to get the actual false return somewhere to tell me that the form is not valid.

All I need is the function or variable or property that I can call on (client side) to tell me if the form is valid.

I hope that makes sense, thank you.

From stackoverflow
  • If your using validation groups you can do:

    if (Page_ClientValidate('Group Name'))
    {
     //Valid
    }
    
    Stuck In Baghdad : perfect, thank you!
  • Page_IsValid would tell you whether the Page is valid. you can also use the isvalid property of the validator to validate the individual validator.

IE cookie quirks

We are facing an issue where the following check fails intermittently in IE:

pseudocode:

setCookie("name","value", "1d");
if(getCookie("name") === "value"){ 
  return SUCCESS;
} else {
  return FAILURE;
}

this little routine checks if the user has cookies enabled. it shouldnt fail but it does. is it because IE can only handle a certain amount of cookies? (we set lots of cookies).

is there some other way to do a cookie check that is more reliable?

From stackoverflow
  • Yes, IE has a preset cookie limit per host. After that the program begins discarding cookies. Check here for more info...

    I'd recommend you use use cookies only to track the userid, for everything else you should use something like "Session State".

    mkoryak : do you know of a workaround for this issue?
    : Like I said, you should store your user data on the server. For instance, you can use "Session State", "View State" or in more extreme cases just write to a database.
  • As bastos.sergio said, use session.

    That said, if you have load balanced web servers, ignore session and pull the data straight from the database server when you need it.

    If it is a single web server, then use session.

    Regardless, take a look at the data you are storing. ALL cookies for a domain are sent back on every web request. All session data is also loaded up for every single web request.

    If you don't need the data for every web page, then you shouldn't cache it in cookies or session as it actually causes a greater performance penalty than just pulling it when you need to.

    This is why the only things that are typically stored in cookies or session are simple items like the user id.

    Also, I believe IE limits the site to 20 separate cookies for a total of 4KB of data.
    See http://support.microsoft.com/kb/306070

    UPDATE: Scratch the limit of 20. IE 5,6, and 7 support up to 50 cookies provided a particular update from August 2007 was applied. http://support.microsoft.com/kb/941495 However, the 4KB limit is still imposed.

    And see this blog entry which covers exactly why large amounts of cookies are a bad idea.

  • is it because IE can only handle a certain amount of cookies?

    Could be, but that limit is quite high — way more than you should be setting in practice. Don't put large amounts of data in cookies; they are sent on every HTTP request, slowing your application down. On IE you can store larger amounts of data in userData, and on newer browsers you can use DOM Storage, either way without the HTTP overhead. But relying on these isn't brilliant for compatibility. Do you really need to store that much on the client side? For a general-purpose web app this may be a sign that you are Doing It Wrong.

    is there some other way to do a cookie check that is more reliable?

    No, that's pretty much it. Note that you can fail to set the cookies-with-expires-date but succeed to set a session cookie (no expires date; expires when the browser is closed). This is because in IE when you block persistant cookies they are completely blocked, not downgraded to session cookies as on some other browsers. So for some purposes you need two checks.

How to diagnose ArgumentOutOfRangeException on SqlDbType?

We have some customers using our .NET 2.0 thick-client app that experience strange, intermittent errors reading data from a SQL 2000 SP4 Server, where the actions succeeded just moments earlier. We have some customers using SQL 2000 (and many using 2005) where these errors do not occur.

One thing I notice is that the app in our testing environments references System.Data 2.0.50727.3053; whereas the app references 2.0.50727.1433 on the customer's systems. What is the difference between these two revisions and could it be related to the errors described below?

Here is an example of the error's stack trace where the enumeration value is 8, but I have plenty more instances where the "out of bounds" enumeration value is 4 or 14 with the same exact stack trace. Are the enumeration values findable sometimes but not at other times? What about when the same portion of the app runs without errors?

TYPE: System.ArgumentOutOfRangeException
MSG: The SqlDbType enumeration value, 8, is invalid.
Parameter name: SqlDbType
SOURCE: System.Data
SITE: GetSqlDataType

at System.Data.SqlClient.MetaType.GetSqlDataType(Int32 tdsType, UInt32 userType, Int32 length)
at System.Data.SqlClient.TdsParser.CommonProcessMetaData(TdsParserStateObject stateObj, _SqlMetaData col)
at System.Data.SqlClient.TdsParser.ProcessMetaData(Int32 cColumns, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.NextResult()
at Ceoimage.Basecamp.Data.Document._GetDocumentModelWithCollections(IDataReader rdr)

Update: I just downloaded System.Data from one of the clients' workstations. They had two versions in the GAC, one in the GAC directory and one in the GAC_32 directory. In GAC, the version number is 1.14322.2365. In GAC_32, the version number is 2.0.50727.1433 as described above. In all three versions, however, the SqlDbType enumerable maps the same int values to the same types for those in the error messages:

DateTime = 4
Int = 8
UniqueIdentifier = 14

I am afraid the version might be a red herring: if the problem has to do with framework versions, shouldn't the problem happen 100% of the time rather than being transient?

From stackoverflow
  • Your client is on .NET 2.0 without 3.5 installed. Your test servers are 3.5. The only things added to the enumeration in 3.5 are:

    Structured
    Date
    Time
    DateTime2
    DateTimeOffset
    

    This does not mean that there are not some underlying changes.

    If possible, I would test the app under 2.0 without 3.5 installed and see what errors you are getting. That should give you a good place to start.

    To figure out the point of error, I would have to iterate through the enumeration and see what order the items are in the enum. Reflector is a great tool for doing this, but you will have to do it on both a 2.0 system and a 3.5 system to see the changes.

    flipdoubt : Thanks for the good start. Looking at Reflector, SqlDbType with the value of 8 is Int. My app would not work a lick if it has trouble reading Ints from the database. How in the world does it happen and how can it be so transient if it is tied to the framework version?

What's the correct way for a cross-browser HTML layout?

I just read codes of a web product that is supposed to support multiple modern browser(including FireFox 3.X, Safari 3.X and IE7+, but not including IE6-). The HTML code uses div instead of table to create table-like effects. Actually, the div's are organized like this:

   <div>
         <div>
              <div style="float:left" id="header1">...</div>
              <div style="float:left">...</div>
              <div style="float:left">...</div>
         </div>
         <div>
              <div style="float:left" id="header2">...</div>
              <div style="float:left">...</div>
              <div style="float:left">...</div>
         </div>

There is a piece of onload JavaScript code to read all "headerX" elements, calculate their max offsetWidth. Then assign the max offsetWidth to each of them. In this way, the div is well aligned to be like table.

Actually, I don't think this is a good way to go, but I'm told that the product is designed this way to make it cross-browser supported, because table behaves different in different browsers.

I'm not convinced. So, the question. Besides the approach mentioned above, is there any better to make table-like layout for all popular browsers?

From stackoverflow
  • I know this doesn't answer your question, but try to avoid table layouts, and learn how to write cross-browser compatible CSS to position the content of your page.

    See what's possible with CSS layouts at csszengarden

  • The HTML code uses div instead of table to create table-like effects.

    This is a common misconception - the old way that many people used to do their layouts were with tables, so they were actually using table instead of div for div-like effects. :)

  • What you're doing is called div-soup. Read up on semantic HTML and the CSS property display.

    Also, if the data is tabular, there's nothing wrong with using a table. To get identical layouts across different browsers, you might have to set the CSS-property border-collapse: collapse or the HTML attribute cellspacing="0"

  • I think it's impossible.

    Note however that if the height of the row is known you can define your table via first column - first row, second row, ... second column - ....

  • There are not many cases where you HAVE to use JavaScript for formatting/design. Only exception I can recall right now is reformatting the page when user resizes the window.

    This sort of thing can be 100% done with plain HTML + CSS. It may need conditional stylesheet for IE6 and mobile platforms, but every single web I made so far had the same stylesheet for IE7 and FF3 and worked ok with DIVs instead of tables. You just need to think, google, and improvise (in this order ;).

    However, I do not think this particular case can be achieved without a DIV for each table, row, and cell which means you will be doing pretty much the same thing as with <table>, <tr> and <td>. Displaying tabular data in tables is semantically fine so if you really need it as a table, not as a web layout, just save yourself the trouble and use the HTML table markup.

  • Even I was having similar thoughts about using tables for layout for long time. But later I figured lot of popular websites are using div based layouts. So I think that is the way to do it nowadays. You dont have to build one from scratch. There are lot of css frameworks available on web which can be used easily. Please find the list below.

    http://developer.yahoo.com/yui/
    http://code.google.com/p/blueprintcss/

    Here you have a list of css frameworks
    http://speckyboy.com/2008/03/28/top-12-css-frameworks-and-how-to-understand-them/

  • That is a great example of someone missing the whole point of avoiding the use of tables for layout. The primary problem with using tables for layout is that it makes it very difficult and tedious to change anything on a site wide scale later.

    By putting the CSS rules inside the HTML, they are combining the inflexibility of tables with dodgy and repetitive CSS. To top it all off, they are misusing javascript to recreate functionality that already exists in tables.

    There's not simple or "correct" answer to this question. Without knowing the specifics of your situation, all I can do is lay out a few basics:

    • Less code is almost always better.
    • If you find yourself trying to replicate table behaviour using css (or especially javascript) you may want to just use tables.
    • Less code is almost always better.
    • If you find yourself nesting tables inside of other tables, there's likely a better way to accomplish your goal.
    • Less code is almost always better.
    • Don't be afraid to change your design a bit if it lets you simplify your code a lot.

    The truth is that there are no magic bullets. There are tons of resources out there, but as with any worthwhile pursuit it takes time and dedication. Using a tool that hides the complexity may help in the short term, just be aware that you no longer have control of the trade-offs being made on your behalf behind the scenes.

    A few places to start:

  • I find that the div/table debate is best left for other people to waste time on. When designing a cross-browser layout, the philosophy I've found that works best is to treat both table and div as unique elements, each with its place on a web page. If you find yourself able to use divs effectively, then that's great (and if you can get them working well in all browsers, all the more power to you.) With more complex layouts, however, I find that tables can be useful as well, when you take care to specify cellpadding, cellspacing, borders, and such. I recently wound up working on a project where our final design strategy used a series of div elements which each encapsulated a table setup in order to get the design we wanted. Page was http://www.kafuka.org, if you're wondering what I mean.

    bobince : Not a particularly good example, I think, as it's all fixed-size (which could be done easily with pure CSS layout). Also the text is unreadably tiny and no content at all is visible without JavaScript (terrible for accessibility and SEO).
    Sohnee : I agree - that's a terrible example. The site doesn't even fit on a 1024x768 browser window and there's absolutely no lists of data - ergo no need for a table. That page could be much better with a css layout - it would also be a smaller download.
  • I'm told that the product is designed this way to make it cross-browser supported, because table behaves different in different browsers.

    Pfft. Tables behave no more differently across browsers than any other feature.

    Using JavaScript to emulate a table layout rather than just using a table is really perverse. It'll be slower and more clunky than just letting the browser do it, it'll fall apart without JS enabled, and it may not respond well to zooming (which breaks offsetWidth in some circumstances) or percentage-width rounding errors.

    There are cases where it can be appropriate to use JavaScript to augment layout, for tricks that neither tables nor CSS layout can do on their own, but this isn't really one of them.

    is there any better to make table-like layout for all popular browsers?

    Yes, use a table. The semantic issue of “does this markup really represent a table” is largely an academic matter. (And with your markup talking about headers, it sounds like you may even be using a real semantic table anyway!)

    There is little-to-no practical harm in sparing use of simple tables for layout as long as the top-to-bottom reading order makes sense. The idea of “tables are intrinsically evil” comes as a reaction to a time when most sites were authored using a horrific mess of nested, spanned and fixed tables resulting in unreadable and inaccessible markup.

    Sure, it would be ideal if you could put each page element in a div and use CSS to position each one relative to each other, the page size, etc. But the reality is CSS isn't that powerful, and some things can be achieved much more simply with a little conservative table use.

  • It looks like almost everyone misunderstood your question (or maybe I did).

    It sounds like the web product is trying to display tabular data using divs and their reason for it is that it is the best way to make it cross-browser compatible. Not only is this false, by bringing javascript into the equation they are instantly making it less compatible across your users. If a user has javascript disabled the divs will be a mess. It is best to use <table> for this situation, as it actually works rather well across browsers, which is why it used to be so attractive to design a layout.

    What most people are referring to in their answers is the old sin of making website layouts using tables, which I don't think your question really covers. Anyhow, this is improper because the <table> tag is not semantically relevant to hold layout content. It is always best to keep the semantic meaning of your HTML document. What this web product is doing is the same sin, but in a reverse way: they are using the <div> tag to display tabular data, when the <table> is there for that.

  • There is just no such thing.

    Every single broswer has its own quirks. You need to iteratively fight with them and learn to work around them.

    Saying that CSS layout is more cross-browser or more reliable than table layout is just being blind. No it isn’t. Believing in that CSS markup is more semantic and cool than table markup is a question of religion and has nothing to do with actual technical aspects of both approaches.

gethostbyname win32 error...

Hi all,

I am using the gethostbyname() function in the usual manner...

iaHost.s_addr = inet_addr(lpServerName);
if (iaHost.s_addr == INADDR_NONE)
{
 // Wasn't an IP address string, assume it is a name
 lpHostEntry = 0;
 lpHostEntry = gethostbyname(lpServerName);
}

to access my web site and return information. The variable, "lpServerName", can be any host, e.g., "google.com" or the loop-back, "127.0.0.1".

My system: Windows x64 or x32 (it doesn't matter the same error occurs) running on an AMD Opteron system, XAMPP is on my machine and I use it for debugging purposes, my compiler is Microsoft Visual c++ 2005.

Here's the problem: when I use the loop-back address, the code above works fine. When I try and access a web site exterior to my computer I get the following error: "Windows has triggered a breakpoint in test_gethostbyname.exe. This may be due to a corruption of the heap, and indicates a bug in test_gethostbyname.exe or any of the DLLs it has loaded. The output window may have more diagnostic information". The program, "test_gethostbyname.exe", contains only the parts of the code from a much larger project that apply to accessing the internet. Thus, I don't think the heap is being corrupted by anything I have done. I have tried the "getaddrinfo" command with the same results. I have searched the web for any kind of information on this problem with no joy (other than segmentation faults on unix systems).

Does anyone know of any problem with this command that would cause this error to occur?

Thanks in advance,

Jay

From stackoverflow
  • Is lpServerName:

    • not NULL terminated
    • NULL
    • already deleted

    My guess would be that it's not NULL terminated.

    Perhaps inet_addr will stop even if it doens't see a NULL termination, so that's why it works when you use a normal IP dotted string.

  • I see you are checking for INADDR_NONE but what about the other fail state INADDR_ANY

    http://msdn.microsoft.com/en-us/library/ms738563(VS.85).aspx

    What string are you passing to gethostbyname()? Odds are there is something wrong with the format of lpServerName.

  • I have played with whether or not the string is null terminated and the error has not gone away. The example I have compiled is from the Microsoft description of gethostname() which I would assume is functionally correct, but I still get the error.

    This morning I recompiled the example on another machine that is using the express version of visual c++ and the error does not happen. Thus, I am beginning to suspect that there might be a problem with the libraries libraries on my development machine, so I am going to reinstall the compiler and the platform SDK and see if this corrects the problem.

  • I have found the answer. The problem appears to be the libraries on my XP system. As I mentioned before, I compiled the program on another XP system without problem. I compiled the system on my development system with Windows 7 and Visual c++ 8 express and it worked.