Saturday, February 19, 2011

SQLMembershipProvider - Adding membership to an existing database. Setting permissions

I am adding membership-related schemas to an existing database (lets call it myDatabase) following those instructions.

As a results the number of tables, views and stored procedures are being created in myDatabase.

The next step is to modify web.config for the application to use CustomizedMembershipProvider

<membership defaultProvider="CustomizedMembershipProvider">
      <providers>
         <add name="CustomizedMembershipProvider"
              type="System.Web.Security.SqlMembershipProvider"
              connectionStringName="MyDBConnectionString" />
      </providers>
    </membership>

Then we also need to specify the connection string like:

<connectionStrings>
     <add name="MyDB" MyDBConnectionString ="..." />
  </connectionStrings>

Here is my question:

  1. Should I use different connection string to the one the application uses? As is there a need to create a new user in the database with permissions related specifically to the membership objects?
  2. Once the connection string is specified with the User ID etc., do I need to grant permissions for that user for those newly created objects? Would that be for stored procedures only or also tables and views?

EDIT: I noticed that there was a set of roles created in the database along with the membership object. So it is a matter of assigning the user to the proper role(s). The roles are the likes of

aspnet_Membership_FullAccess
aspnet_Personalization_FullAccess
etc...

So the only the first part of the question remains in place. So is there a point in creating a new database user (so separate db connection)

From stackoverflow

Can I speed up the YUI layout manager, when I have a large table on the page?

I am using the YUI layout manager which seems to work at an OK speed. However if the page contains a large <Table> with about 500 rows, the YUI render() function takes about a minute longer to run.

When I open the same page without the layout manager it opens in less than a second.

My only concern is with IE 7. I tried it on firefox and it only took about three seconds.

Any ideas on what is taking so long? Can I somehow tell the layout manager to ignore the table?

From stackoverflow
  • I finally figured it out myself.

    The trick is to hide the content that should be ignored by the layout manager.

    Before calling render() set the style.display = 'none' for a tag that contains a large chunk of the page you don't need the layout manager to manage. Set it back to normal after with style.display = 'block'.

  • Do you mean the render() method of your large table is taking a long time?

    the YUI DataTable has a renderLoopSize property specifically for this kind of situation. It makes the table render to the DOM every X rows, rather than waiting till the end and rendering them all in 1 go.

    var myDataTable = new YAHOO.widget.DataTable("myContainer", 
                             myColumnDefs, 
                             myDataSource, 
                            {renderLoopSize: 100});
    

Big integer field in django models

In short: How do you specify a BIGINT in Django models?

In a current project I am doing using Django (0.97-svn release), I've all the integer fields in all the models defined as IntegerField. It was working immaculately during the development cycle where I was using SQLite for the DB backend. However, as soon as I rolled off to MySQL, I noticed that for one of the IntegerFields, MySQL was truncating data---apparently, for reasons still unknown to me, SQLite didn't complain. I looked at the schema and found out that an IntegerField in Django is represented as an INT(11) field in MySQL. Naturally, the reason MySQL was truncating data was because the data was more than 11-digit in length. To workaround, I had to manually update the column to, in this case, be a BIGINT(20). Django and MySQL coexist peacefully with that. But whenever I reset the application in which rests the model containing that BIGINT, Django again creates it as an INT(11). I've looked at Django docs, and not found anything. Have I overlooked something?

Thanks.

From stackoverflow
  • SQLite won't complain ever. it uses 'manifest typing', that is, the values have type, not the columns. It lets you store bigtext on a smallint collumn, or whatever you want! (except if you define an integer primary key, where it uses a 64-bit integer).

    that's a very convenient feature, but it makes a SQLite bad choice for developing if you're going to deploy with a different engine.

    for using a BIGINT, you'd have to create a custom field class. unfortunately, that part has changed on Django 1.0, so you'd have to rewrite it if/when you update.

  • Try a DecimalField.

  • BigIntegerField was added in changeset 11887, 2009-12-17 09:10:38 and is part of Django 1.2 and newer.

    bennedich : +1 for solving my issue.

How do I get the error icon used by DataGridImageCell?

I display some objects that have thumbnails in two ways: one in a DataGridView, and one in a custom control.

In the DataGridView, the thumbnail is displayed in a DataGridViewImageCell. If the thumbnail is missing, the cell displays an error icon.

I want to show that icon in my custom control, too. How do I get hold of it? Preferably without pinvoke, but beggars can't be choosers.

From stackoverflow
  • I think, this icon is available in "%visual_studio_directory"\Common7\VS2005ImageLibrary" directory.

    Simon : I don't see it - could you be more specific?
    TcKs : C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary\VS2005ImageLibrary\VS2005ImageLibrary\icons\WinXP\error.ico ...........\warning.ico
  • I'd recommend turning this problem on its head: make sure the grid never displays the error icon. You should be able to do this by catching the cell data display event. If the image is missing, display an image from your resource segment (and use the same image in the custom control, obviously).

    The problem with attempting to match the grid's built-in error image is that if the image gets changed in a future version, you'll have to modify your application.

Size of an open file object

Is there a way to find the size of a file object that is currently open?

Specifically, I am working with the tarfile module to create tarfiles, but I don't want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like objects, so I imagine a generic solution would work.

From stackoverflow
  • If you have the file descriptor, you can use fstat to find out the size, if any. A more generic solution is to seek to the end of the file, and read its location there.

  • $ ls -la chardet-1.0.1.tgz
    -rwxr-xr-x 1 vinko vinko 179218 2008-10-20 17:49 chardet-1.0.1.tgz
    $ python
    Python 2.5.1 (r251:54863, Jul 31 2008, 22:53:39)
    [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> f = open('chardet-1.0.1.tgz','rb')
    >>> f.seek(0,2)
    >>> f.tell()
    179218L
    

    Adding ChrisJY's idea to the example

    >>> import os
    >>> os.fstat(f.fileno())[6]
    179218L
    >>>
    
  • Well, if the file object support the tell method, you can do:

    current_size = f.tell()
    

    That will tell you were it is currently writing. If you write in a sequential way this will be the size of the file.

    Otherwise, you can use the file system capabilities, i.e. os.fstat as suggested by others.

Pass the same value of HiddenField between several ASP.NET pages

Would you explain me, please, how to pass the same value of HiddenField between several ASP.NET pages?

I think I could use RegisterHiddenField on every page in chain, but hope there is an easier solution.

Thanks!

From stackoverflow
  • If you are passing the HiddenField directly between a few pages, you could make the links between the pages button controls using one of Button, LinkButton, or ImageButton. Then you could use the PostBackUrl to navigate to the next page.

    <asp:LinkButton ID="Button1" runat="server"
                    PostBackUrl="~/TargetPage.aspx" Text="Next Page" />
    

    To access the value from the target page:

    if (Page.PreviousPage != null)
    {
        string MyHiddenValue = (HiddenField)Page.PreviousPage.FindControl("MyHiddenField");
    }
    

    You should probably check whether HiddenField exists etc.

    Alexander Prokofyev : This would work for next page, but how to refer the hidden field from third page in sequence? Using Page.PreviousPage.PreviousPage etc?

STL Migration issues (VS 2003 -> 2005)

I have just converted a project from Visual Studio 2003 to 2005 and although most of it 'converted' fine, I have a series of STL errors from the following line:

void SomeFn( std::vector<CSomeObject*>::iterator it,
std::vector<CSomeObject*>::iterator itBegin = NULL,
std::vector<CSomeObject*>::iterator itEnd = NULL );

The Visual Studio error is as follows:

c:\<path>\Headerfile.h(20) : error C2440: 'default argument' : cannot convert from 'int' to 'std::_Vector_iterator<_Ty,_Alloc>'
        with
        [
            _Ty=CObject *,
            _Alloc=std::allocator<CObject *>
        ]
        No constructor could take the source type, or constructor overload resolution was ambiguous

I can't see anything wrong with that code and it worked perfectly in VS 2003. Any ideas?

From stackoverflow
  • In 2003, std::vector<T>::iterator is just T *. In 2005, at least in debug mode, it is a class, and hence you can't use NULL for its value (NULL resolves to 0).

    You can use a default-constructed iterator instead:

    std::vector<CSomeObject*>::iterator itBegin = std::vector<CSomeObject*>::iterator()
    
  • Your program is incorrect as NULL cannot be converted as an iterator. I don't really know what you want these iterators to be initialized as. If you need an iterator guarantied not to be in a container but to be still "valid", you can use a default-constructor:

    typedef std::vector<CSomeObject*> myvector_t;
    void SomeFn( myvector_t::iterator it,
                 myvector_t::iterator itBegin = myvector_t::iterator(),
                 myvector_t::iterator itEnd = myvector_t::iterator() );
    

    Note, however, that if you do so, it, itBegin and itEnd won't be comparable in a meaningful way! Only iterators obtained from a given container are comparable meaningfully. In the end, I would recommend against using defaults values for itBegin and itEnd. If you really need to not have these, create another function without the arguments and do something meaningful. i.e.:

    typedef std::vector<CSomeObject*> myvector_t;
    void SomeFn( myvector_t::iterator it,
                 myvector_t::iterator itBegin,
                 myvector_t::iterator itEnd );
    void SomeFn( myvector_t::iterator it ); // No begin/end arguments
    

    Another problem of your program is the use of a vector to store pointers. This is really unsafe. Make sure you never erase elements from the vector without deleting the element first. You might also have problems with algorithms copying objects around. It is better to use smart pointers in vectors.