Thursday 16 May 2013

How to know whether record is updated or not

When you do an update, it's immediately return rows updated: It Return Number of Records Affected

@@ROWCOUNT - Returns the number of rows affected by the last statement and Return type of @@ROWCOUNT is int.

The following example executes an UPDATE statement and uses @@ROWCOUNT to detect if any rows were changed

UPDATE Tbl_EmpProfile  SET JobTitle = 'Executive' WHERE EMPID = 5001
IF @@ROWCOUNT = 0
PRINT 'Warning: No rows were updated';


You can set this @@rowcount to some other variable and use them for further processing.

Example,

DECLARE @rows AS INT
UPDATE Tbl_EmpProfile  SET JobTitle = 'Executive' WHERE EMPID = 5001
SET @rows = @@rowcount


using Stored Procedure, Register an out parameter for the stored procedure, and set the value based on @@ROWCOUNT if using SQL Server. Use SQL%ROWCOUNT if you are using Oracle.

if you have multiple INSERT/UPDATE/DELETEs, you'll need a variable to store the result from @@ROWCOUNT for each operation.

Friday 10 May 2013

Maintain the check marks in checkboxlists using session

To specify items that you want to appear in the CheckBoxList control, place a ListItem element for each entry between the opening and closing tags of the CheckBoxList control.

The CheckBoxList control also supports data binding. To bind the control to a data source, first create a data source, such as one of the DataSourceControl objects, that contains the items to display in the control. Next, use the DataBind method to bind the data source to the CheckBoxList control. Use the DataTextField and DataValueField properties to specify which field in the data source to bind to the Text and Value properties of each list item in the control, respectively. The CheckBoxList control will now display the information from the data source.


<asp:CheckBoxList ID="CheckBoxList1" runat="server">
    <asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item 3" Value="3"></asp:ListItem>
</asp:CheckBoxList>


To determine the selected items in the CheckBoxList control, iterate through the Items collection and test the Selected property of each item in the collection.


int count = CheckBoxList1.Items.Count;
ArrayList values = new ArrayList();

 for (int i = 0; i < count; i++)
 {
               if (CheckBoxList1.Items[i].Selected)
                {
                    values.Add(CheckBoxList1.Items[i].Value);
                }
  }

//save selected checkboxes in session state
Session["CheckBoxList"] = values;



//casting the value from the Session
ArrayList values = (ArrayList)Session["CheckBoxList"];

foreach (ListItem li in CheckBoxList1.Items)
{
                if (values.Contains(li.Value))
                    li.Selected = true;
                else
                    li.Selected = false;
}
   

Friday 3 May 2013

How to handle multiple data tables using Data Reader in ASP.NET


This is best used when you just want to fetch data in read only mode , populate your business entity and close the reader. This is really fast. But you cannot do update with data reader.


Data reader can hold data from multiple tables and data reader can hold more than one table.


If you want to Read/Update the data, You can Read/Update the data with data adapters but it is less faster when reading the data then Data reader.

When you read the results of a batch SQL statement, you can use the NextResult method to position the DataReader at the next result in the result set.


do check like

if(reader.NextResult())


By default, the data reader is positioned on the first result.


What is Static Class in C#?

If a class declaration includes the keyword static then that class is termed as static class. Static class is a specialized class that cannot have any objects.

First of all, the difference between a non-static and a static class in C# is simply that a static class cannot be instantiated. This means that you don’t use the new keyword to instantiate an instance of the class. Instead, you will access members of the static class by using the class name. Basically, a static class is intended to be a container for a set of methods that only work with supplied parameters and don’t have a need to store or retrieve data that is unique to them.

there are two main features of a static class, one is no object of static class can be created and another is, a static class must contain only static members, then it is important that what is the main benefit to create a static class, the main benefit of making static class, we do not need to make any instance of this class ,all members can be accessible with its own name.
























Static Members
A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

Static class members are declared using the static keyword before the return type of the member, for example:

public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }
    public static void Drive() { }
    public static event EventType RunOutOfGas;

    //other non-static fields and properties...
}


Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member. For example:

Automobile.Drive();
int i = Automobile.NumberOfWheels;


Characteristics of Static Class in C#
  • Members of Static Class Can Only Be Static
  • Static Class Can Contain Only Static Constructor
  • Static Class is Implicitly Sealed
  • Static Class Cannot Be Declared as Sealed
  • Static Class Cannot be Declared as Abstract
  • Static Class Should Inherit Only Object
  • Static Class Cannot be Inherited

When to Use Static Classes
Suppose you have a class CompanyInfo that contains the following methods to get information about the company name and address.

class CompanyInfo
{
    public string GetCompanyName() { return "CompanyName"; }
    public string GetCompanyAddress() { return "CompanyAddress"; }

}


These methods do not need to be attached to a specific instance of the class. Therefore, instead of creating unnecessary instances of this class, you can declare it as a static class, like this:

static class CompanyInfo
{
    public static string GetCompanyName() { return "CompanyName"; }
    public static string GetCompanyAddress() { return "CompanyAddress"; }
}

Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.