Welcome to RP's venture...Here you can find some of the relative topics like ASP.Net, C#.Net,VB.Net, SQL Server, AJAX, Javascripts, Sharepoint, WPF, WCF, Silverlight, MVC, General knowledge, Jokes, Fun, Technical, Non-Technical etc.
0

ASP.NET restarts when a folder is created, renamed or deleted.

Posted by Rajendra Prasad Panchati on Monday, June 21, 2010
This code appears to resolve the issue, when added to Application_Start() in Global.asax:

PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public |  BindingFlags.Static);
object o = p.GetValue(null, null);
FieldInfo f = o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
object monitor = f.GetValue(o);
MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic);
m.Invoke(monitor, new object[] { });  

With these changes, I can create/modify/delete folders without causing the application to restart.
Not clear if this is the best solution -- Don't know if there will be unwanted side effects due to calling StopMonitoring.


|
0

Composite Primary Keys

Posted by Rajendra Prasad Panchati on Wednesday, June 16, 2010
Let's start things off with what I feel is a good interview question:


How would you define what a primary key of a table is?

a.    An auto-generated numeric or GUID column in the table that uniquely identifies each row
b.    A non-nullable column in the table that uniquely identifies each row
c.    None of the above

I suspect that many people will answer (a), and quite a few will answer (b).  If you answer (c), though, you are correct!  Why?  Because a primary key is not a single column, it is a set of columns.  Many people who have designed large, complicated systems are simply not aware of this.

I once worked with a consultant who kept claiming that importing data into his system was complicated, because his database used “primary keys”.  It was very confusing (yet humorous) trying to discuss things with him because he kept confusing primary keys with identity columns.  They are not the same!  An identity column may be a type of primary key, but a primary key is not an identity column; it is a set of columns that you define that determine what makes the data in your table unique.  It defines your data. It may be an identity column, it may be a varchar column or a datetime column or an integer column, or it may be a combination of multiple columns.

When you define more than one column as your primary key on a table, it is called a composite primary key.  And many experienced and otherwise talented database programmers have never used them and may not even be aware of them. Yet, composite primary keys are very important when designing a good, solid data model with integrity.

This will be greatly oversimplifying things, but for this discussion let's categorize the tables in a database into these two types:
  • Tables that define entities
  • Tables that relate entities
Tables that define entities are tables that define customers, or sales people, or even sales transactions.  The primary key of these tables is not what I am here to discuss.   You can use GUID columns, identity columns, long descriptive text columns, or whatever it is you feel comfortable to use as primary keys on tables that define entities.  It’s all fine by me, whatever floats your boat as they say.  There are lots of discussions and ideas about the best way to determine what the best primary key of these tables should be, and pros and cons of all of the various approaches, but overall, that is not really what I am addressing.

Tables that relate entities, however, are a different story.

Suppose we have a system that tracks customers, and allows you to assign multiple products to multiple customers to indicate what they are eligible to order.  This is called a many-to-many or N:N relation between Customers and Products.  We already have a table of Products, and a table of Customers.  The primary key of the Products table is ProductID, and the Customers table is CustomerID.  Whether or not these “ID” columns are natural or surrogate, identity or GUID, numerical or text or codes, is irrelevant at this point.

What is relevant and important, and what I am here to discuss, is how we define our CustomerProducts table.  This table relates customers to products, so the purpose of the table is to relate two entities that have already been defined in our database.  Let’s also add a simple “OrderLimit” column which indicates how many of that product they are allowed to order.  (This is just a simple example, any attribute will do). How should we define this table?

For some reason, a very common answer is that we simply create a table with 4 columns: One that stores the CustomerID, one that stores the ProductID we are relating it to, the Order Limit, and of course the primary key column which is an identity:

Create table CustomerProducts
(
    Customer_ProductID int identity primary key,
    CustomerID int references Customers(CustomerID) not null,
    ProductID int references Products(ProductID) not null,
    OrderLimit int not null
)

This is what I see in perhaps most of the databases that I’ve worked with over the years.  The reason for designing a table in this manner?  Honestly, I don’t know! I can only surmise that it is because of the lack of understanding what a primary key of a table really is, and that it can be something other than an identity and that it can be comprised of more than just a single column.  As I mentioned, it seems that many database architects are simply not aware of this fact.

So then, what is the problem here?  The primary issue is data integrity.  This table allows me to enter the following data:

CustomerProductID    CustomerID    ProductID    OrderLimit
1                    1             100          25
2                    1             100          30

In the above data, what is the order limit for customerID #1, productID #100?  Is it 25 or 30?  There is no way to conclusively know for sure.  Nothing in the database constrains this table so that we only have exactly one row per CustomerID/ProductID combination. Remember, our primary key is just an identity, which does not constrain anything.

Most database designs like this just assume (hope?) that the data will be always be OK and there will be no duplicates.  The UI will handle this, of course!  But even if you think that only one single form on one single application ever updates this table, you have to remember that data will always get in and out of your system in different ways.  What happens if you upgrade your system and have to move the data over?  What if you need certain transactions restored from a back up?  What if you ever need to do a batch import to save valuable data entry time?  Or to convert data from a new system that you are absorbing or integrating?

If you ever write a report or an application off of a system and simply assume that the data will be constrained a certain way, but the database itself does not guarantee that, you are either a) greatly over-engineering what should be a simple SQL statement to deal with the possibility of bad data or b) ignoring the possibility of bad data completely and setting yourself up for issues down the road.  It's possible to constrain data properly, it's efficient, it's easy to do, and it simply must be done or you should not really be working with a database in the first place -- you are forgoing a very important advantage it provides.

So, to handle that issue with this table design, we need create a unique constraint on our CustomerID/ProductID columns:

create unique index cust_products_unique on CustomerProducts (CustomerID, ProductID)

Now, we are guaranteed that there will only be exactly one row per combination of CustomerID and ProductID.  That handles that problem, our data now has integrity, so we seem to be all set, right?

Well, let’s remember the definition of what a primary key really is.  It is the set of columns in a table that uniquely identify each row of data.  Also, for a table to be normalized, all non-primary key columns in a table should be fully dependent on the primary key of that table.

Consider instead the following design:

Create table CustomerProducts
(
    CustomerID int references Customers(CustomerID) not null,
    ProductID int references Products(ProductID) not null,
    OrderLimit int not null,
    Primary key (CustomerID, ProductID)
)

Notice here that we have eliminated the identity column, and have instead defined a composite (multi-column) primary key as the combination of the CustomerID and ProductID columns.  Therefore, we do not have to create an additional unique constraint.  We also do not need an additional identity column that really serves no purpose.  We have not only simplified our data model physically, but we’ve also made it more logically sound and the primary key of this table accurately explains what it is this table is modeling – the relationship of a CustomerID to a ProductID.

Going back to normalization, we also know that our OrderLimit column should be dependent on our primary key columns.  Logically, our OrderLimit is determined based on the combination of a CustomerID and a ProductID, so physically this table design makes sense and is fully normalized.  If our primary key is just a meaningless auto-generated identity column, it doesn’t make logical sense since our OrderLimit is not dependent on that.

Some people argue that having more than one column in a primary key “complicates things” or “makes things less efficient” rather than always using identity columns.  This is simply not the case.   We’ve already established that you must add additional unique constraints to your data to have integrity, so instead of just:
  1. A single indexed composite primary key that uniquely constrains our data
we instead need:
  1. An additional identity column
  2. A primary key index on that identity column
  3. An additional unique constraint on the columns that logically define the data
So we are actually adding complexity and overhead to our design, not simplifying!  And we are requiring more memory and resources to store and manipulate data in our table.

In addition, let's remember that a data model can be a complicated thing.  We have all kinds of tables that have primary keys defined that let us identify what they are modeling, and we have relations and constraints and data types and the rest.  Ideally, you should be able to look at a table's primary key and understand what it is all about, and how it relates to other tables, and not need to basically ignore the primary key of a table and instead investigate unique constraints on that table to really determine what is going on!  It simply makes no sense and adds unnecessary confusion and complication to your schema that is so easily avoided.

Some people will claim that being able to quickly label and identify the relation of a Product to a Customer with a single integer value makes things easier, but again we are over-complicating things.  If we only know we are editing CustomerProductID #452 in our user interface, what does that tell us?  Nothing!  We need to select from the CustomerProducts table every time just to get the CustomerID and the ProductID that we are dealing with in order to display labels or descriptions or to get any related data from those tables.  If, instead, we know that we are editing CustomerID #1 and productID #6 because we are using a true, natural primary key of our table, we don’t need to select from that table at all to get those two very important attributes.

There are lots of complexities and many ways to model things, and there are many complicated situations that I did not discuss here.  I am really only scratching the surface.  But my overall point is to at least be aware of composite primary keys, and the fact that a primary key is not always a single auto-generated column.   There are pros and cons to many different approaches, from both a logical design and physical performance perspective, but please consider carefully the idea of making your primary keys count for something and don’t automatically assume that just tacking on identity columns to all of your tables will give you the best possible database design.

And, remember -- when it comes to defining your entities, I understand that using an identity or GUID or whatever you like instead of real-world data has advantages.  It is when we relate entities that we should consider using those existing primary key columns from our entity tables (however you had defined them) to construct an intelligent and logical and accurate primary key for our entity relation table to avoid the need to create extra, additional identity columns and unique constraints.

|
0

Encrypting your ViewState in ASP.NET 2.0

Posted by Rajendra Prasad Panchati on Wednesday, June 16, 2010

Introduction

In the previous release of ASP.NET, the page developer could turn encryption on and off at the application level through a config setting. When validation was set to 3DES, ViewState was encrypted before being rendered in the page.
<configuration>
<system.web>
<machineKey validation="3DES" />
</system.web>
</configuration>

Main

In ASP.NET 2.0 the support for controlling and utilizing encryption has been expanded. Encryption settings can now be controlled separately for each page. In addition, the controls on the page can request that encryption be used for the ViewState, but even this request can be overridden by the page setting. The ViewStateEncryptionMode enumeration has three values: Auto, Always, and Never. The default value is Auto.
  • ViewStateEncryptionMode.Auto
    In this mode, ASP.NET will encrypt the ViewState for a page if any control on the page requests it. Note that this means all of the ViewState is encrypted, not just the ViewState for the control that requests it. A large part of the performance cost associated with encryption is in the overhead. So encrypting the whole ViewState is faster than doing separate encryption operations if more than one control makes the request.
  • ViewStateEncryptionMode.Never
    As you would expect, in this mode ASP.NET will not encrypt the ViewState, even if the application is set for encryption and controls on the page have requested it. If you know that no data involved in the page needs to be encrypted, then it may be safe to set the mode to Never. However, at this point it is rare for the documentation about a control to disclose what is being saved in ViewState, so you will want to be careful if there is a chance that sensitive data could be exposed.
  • ViewStateEncryptionMode.Always
    In this mode, ASP.NET does not wait for a control in the page to request encryption. ViewState is always encrypted. When working with sensitive data, it is a good practice to utilize encryption.
The mode is a property on page, but is set using either a page directive or in the web.config file for the application. 
<%@Page ViewStateEncryptionMode="Always" %>

Or

<configuration>
<system.web>
<pages ViewStateEncryptionMode="Always" />
</system.web>
</configuration>

It is simple for someone writing a custom control to request ViewState encryption. The name of the Page method to call is RegisterRequiresViewStateEncryption.

protected override void OnInit(EventArgs e) {
base.OnInit(e);
if(Page != null) {
Page.RegisterRequiresViewStateEncryption();
}
}

Control developers should be aware of the overhead and potential perf implications of using encryption, and should not take the decision to request encryption lightly. Notice that we refer to it as a request, even though the API name sounds like it is a mandate. If the control developers somehow know that the data being stored in ViewState must be encrypted, they could add code to throw an exception in the case that the page developer turns encryption off.

protected override void SaveViewState() {
if(Page != null) {
if(Page.ViewStateEncryptionMode == ViewStateEncryptionMode.Never) {
throw new Exception(“ViewStateEncryptionMode.Never not allowed when using the SensitiveDataList control.");
}
}
}

Conclusion

To reduce the chance of someone intercepting the information stored in the ViewState, it is good design to encrypt the ViewState. You could do this in previous releases of ASP.NET, but the support for encryption has been improved in ASP.NET 2.0, allowing you to set this on a page-by-page basis.

|
0

Reference Bookmarks

Posted by Rajendra Prasad Panchati on Wednesday, June 16, 2010
A1 Javascript : http://www.a1javascripts.com/search/search.cgi?Range=Any&Format=Standard&Terms=images+scrolling&menu1=Navigate+Network

|
0

New Sage Pay URLs

Posted by Rajendra Prasad Panchati on Wednesday, June 16, 2010
The URL's used to process transactions have changed and we would recommend that you update your URL's as soon as possible.  Below is a full list of the old Protx URL along with the new Sage Pay URL which you need to change over to.

Live URL's

Old URL'sNew URL's

Login

 
ukvps.protx.com/vspadminlive.sagepay.com/mysagepay

Form

 
ukvps.protx.com/vspgateway/service/vspform-register.vsplive.sagepay.com/gateway/service/vspform-register.vsp

Server

 
ukvps.protx.com/vspgateway/service/vspserver-register.vsplive.sagepay.com/gateway/service/vspserver-register.vsp
ukvps.protx.com/vspgateway/service/refund.vsplive.sagepay.com/gateway/service/refund.vsp
ukvps.protx.com/vspgateway/service/abort.vsplive.sagepay.com/gateway/service/abort.vsp
ukvps.protx.com/vspgateway/service/cancel.vsplive.sagepay.com/gateway/service/cancel.vsp
ukvps.protx.com/vspgateway/service/release.vsplive.sagepay.com/gateway/service/release.vsp
ukvps.protx.com/vspgateway/service/repeat.vsplive.sagepay.com/gateway/service/repeat.vsp
ukvps.protx.com/vspgateway/service/void.vsplive.sagepay.com/gateway/service/void.vsp
ukvps.protx.com/vspgateway/service/directrefund.vsplive.sagepay.com/gateway/service/directrefund.vsp
ukvps.protx.com/vspgateway/service/manualpayment.vsplive.sagepay.com/gateway/service/manualpayment.vsp

Direct

 
ukvps.protx.com/vspgateway/service/vspdirect-register.vsplive.sagepay.com/gateway/service/vspdirect-register.vsp
ukvps.protx.com/vspgateway/service/direct3dcallback.vsplive.sagepay.com/gateway/service/direct3dcallback.vsp
ukvps.protx.com/vspgateway/service/complete.vsplive.sagepay.com/gateway/service/complete.vsp

Test URL's

Old URL'sNew URL's

Login

 
ukvpstest.protx.com/vspadmintest.sagepay.com/mysagepay

Form

 
ukvpstest.protx.com/vspgateway/service/vspform-register.vsptest.sagepay.com/gateway/service/vspform-register.vsp

Server

 
ukvpstest.protx.com/vspgateway/service/vspserver-register.vsptest.sagepay.com/gateway/service/vspserver-register.vsp
ukvpstest.protx.com/vspgateway/service/refund.vsptest.sagepay.com/gateway/service/refund.vsp
ukvpstest.protx.com/vspgateway/service/abort.vsptest.sagepay.com/gateway/service/abort.vsp
ukvpstest.protx.com/vspgateway/service/cancel.vsptest.sagepay.com/gateway/service/cancel.vsp
ukvpstest.protx.com/vspgateway/service/release.vsptest.sagepay.com/gateway/service/release.vsp
ukvpstest.protx.com/vspgateway/service/repeat.vsptest.sagepay.com/gateway/service/repeat.vsp
ukvpstest.protx.com/vspgateway/service/void.vsptest.sagepay.com/gateway/service/void.vsp
ukvpstest.protx.com/vspgateway/service/directrefund.vsptest.sagepay.com/gateway/service/directrefund.vsp
ukvpstest.protx.com/vspgateway/service/manualpayment.vsptest.sagepay.com/gateway/service/manualpayment.vsp

Direct

 
ukvpstest.protx.com/vspgateway/service/vspdirect-register.vsptest.sagepay.com/gateway/service/vspdirect-register.vsp
ukvpstest.protx.com/vspgateway/service/direct3dcallback.vsptest.sagepay.com/gateway/service/direct3dcallback.vsp
ukvpstest.protx.com/vspgateway/service/complete.vsptest.sagepay.com/gateway/service/complete.vsp


Reference: https://www.sagepay.com/help/faq/new_sage_pay_urls?Ref=email&dm_i=8Z4,4NAW,19WPNY,EG3P,1

|
0

Find Last Day of Any Month – Current Previous Next

Posted by Rajendra Prasad Panchati on Wednesday, June 16, 2010
Following script demonstrates the script to find last day of previous, current and next month.

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
LastDay_PreviousMonth

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
LastDay_CurrentMonth

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))
LastDay_NextMonth

ResultSet:
LastDay_PreviousMonth
———————–
2010-05-31 23:59:59.000

LastDay_CurrentMonth
———————–
2007-06-30 23:59:59.000

LastDay_NextMonth
———————–
2007-07-31 23:59:59.000 

If you want to find last day of month of any day specified use following script.

DECLARE @dtDate DATETIME
SET @dtDate = '06/16/2010'
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))
LastDay_AnyMonth

ResultSet:
LastDay_AnyMonth
———————–
2010-06-30 23:59:59.000

|
0

CAST and CONVERT SQL Server 2005

Posted by Rajendra Prasad Panchati on Wednesday, June 16, 2010
Explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality.
Syntax
Using CAST:
CAST ( expressionAS data_type )
Using CONVERT:
CONVERT ( data_type [ ( length ) ] ,expression [ ,style ] )
Arguments
expression
Is any valid Microsoft® SQL Server™ expression. For more information, see Expressions.
data_type
Is the target system-supplied data type, including bigint and sql_variant. User-defined data types cannot be used. For more information about available data types, see Data Types.
length
Is an optional parameter of nchar, nvarchar, char, varchar, binary, or varbinary data types.
style
Is the style of date format used to convert datetime or smalldatetime data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types), or the string format when converting float, real, money, or smallmoney data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types).

|
0

Show month name c#

Posted by Rajendra Prasad Panchati on Wednesday, June 16, 2010
1) string MonthName = (new DateTime(2007, MonthNumber, 1)).ToString("MMMM");
2) System.Threading.Thread.Cu
rrentThread.CurrentCulture.DateTimeFormat.GetMonthName(MonthNumber);

|
0

How to: Respond to Button Events in a GridView Control

Posted by Rajendra Prasad Panchati on Friday, June 11, 2010
When a button is clicked in a GridView control, the RowCommand() event is raised. The GridView control has built-in functionality for operations such as edit, delete, and paging. You can also add buttons and use the RowCommand()event to add custom functionality to the control.
You can add custom functionality to a GridView control in the following ways:
  • By adding a ButtonField field to the GridView control.
  • By adding a Button, LinkButton, or ImageButton controls to a template in the GridView control.
You can use the CommandName() property of the event argument to identify the button's function in the event handler method. If you are working with ButtonField or TemplateField objects, you can also use the CommandArgument()property to identify the current row. When you are using a ButtonField object, the CommandArgument() property is set automatically to the row index. When you are using a TemplateField object, the CommandArgument() property is not automatically set by the control. In that case, if you have to determine the row index in the event handler, you can set the CommandArgument property of the button to the row index by using a data-binding expression.

To respond to button events in the GridView control

  1. Set the button's CommandName property to a string that identifies its function, such as "Print" or "Copy".
  2. If you are using the TemplateField object and have to access the row index in the event handler method, set the button's CommandArgument property to an expression that identifies the current row.
    The following example shows how you can set the CommandArgument property of a button in a TemplateFieldcolumn to the current row index. In the example, the column contains a Button control that displays a shopping cart.
    This language is not supported, or no code example is available.
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="AddButton" runat="server" 
          CommandName="AddToCart" 
          CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
          Text="Add to Cart" />
      </ItemTemplate> 
    </asp:TemplateField>
  3. Create a method for the RowCommand() event of the GridView control. In the method, do the following:
    1. Check the CommandName property of the event-argument object to see what string was passed.
    2. Retrieve the index of the row that contains the button by using the CommandArgument property, if required.
    3. Perform the appropriate logic for the button that the user clicked.
    The following example shows how you can respond to a button click in a GridView control. In the example, a button in a TemplateField column sends the command "AddToCart". The RowCommand() event handler determines which button was clicked. If it was the shopping cart button, the code performs the appropriate logic.
    This language is not supported, or no code example is available.
    protected void GridView1_RowCommand(object sender, 
      GridViewCommandEventArgs e)
    {
      if (e.CommandName == "AddToCart")
      {
        // Retrieve the row index stored in the 
        // CommandArgument property.
        int index = Convert.ToInt32(e.CommandArgument);
    
        // Retrieve the row that contains the button 
        // from the Rows collection.
        GridViewRow row = GridView1.Rows[index];
    
        // Add code here to add the item to the shopping cart.
      }
    
      }
    For an example that uses the ButtonField class, see the GridViewRowCommand() event documentation.

|

About Me