Purpose of Using Statement(which define when an object will be disposed)

25 02 2008

C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object’s resources.

A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.

The object can be declared in the using statement, as shown above, or before the using statement, like this:

Font font2 = new Font(“Arial”, 10.0f);
using (font2)
{
    // use font2
}

Multiple objects can be used in with a using statement, but they must be declared inside the using statement, like this:

using System;

class C : IDisposable
{
    public void UseLimitedResource()
    {
        Console.WriteLine(“Using limited resource…”);
    }

    void IDisposable.Dispose()
    {
        Console.WriteLine(“Disposing limited resource.”);
    }
}

class Program
{
    static void Main()
    {
        using (C c = new C())
        {
            c.UseLimitedResource();
        }
        Console.WriteLine(“Now outside using statement.”);
        Console.ReadLine();
    }
}

Note:- the scope of using directive is:

The scope of a using directive is limited to the file in which it appears.





Ado.net Feature

25 02 2008

Mars:-Multiple Active Result Set

Multiple Active Result Sets (MARS) is a new feature that works with SQL Server 2005 to allow the execution of multiple batches on a single connection. To access multiple result sets on previous versions of SQL Server using SqlDataReader objects, a separate SqlConnection object must be used with each SqlCommand object. However, when MARS is enabled for use with SQL Server 2005, each command object used adds a session to the connection.

How to Enable Mars:-

The MARS feature is disabled by default. It can be enabled by adding the “MultipleActiveResultSets=True” keyword pair to your connection string. “True” is the only valid value for enabling MARS. The following example demonstrates how to connect to an instance of SQL Server and how to specify that MARS should be enabled

Example:-

C#.Net:-

string connectionString = "Data Source=MSSQL1;" +      "Initial Catalog=AdventureWorks;Integrated Security=SSPI" +     "MultipleActiveResultSets=True";   Vb.Net:-
Dim connectionString As String = "Data Source=MSSQL1;" & _     "Initial Catalog=AdventureWorks;Integrated Security=SSPI" & _     "MultipleActiveResultSets=True"

 You can disable MARS by adding the "MultipleActiveResultSets=False" keyword pair to your connection string. "False" is the only valid value for disabling MARS. The following connection string demonstrates how to disable MARS.

 

Disable Mars:-

 

string connectionString = "Data Source=MSSQL1;" +      "Initial Catalog=AdventureWorks;Integrated Security=SSPI" +     "MultipleActiveResultSets=False";

 

 





My new blog

15 02 2008




blog

15 02 2008




blog

15 02 2008

any doubts and information see this http://dotnetindia.blogspot.com/





Dotnet interview Questions site

15 02 2008




Static text and Data binding in Dropdown list

15 02 2008

In many application we want to bind dropdown list using dataset and datareader but we want static and databind that means first item is ’select’ after that binding data this is the senarios

Sqlconnection con=new sqlconnection(“connectionstring”);

sqlcommand cmd=new sqlcommand(“select eno from tbl_emp”,con);

sqldatareader reader=cmd.executereader();

dropdownlist1.datatextfield=”eno”;

dropdownlist1.datasource=reader;

dropdownlist1.databind();

after that—–>

dropdownlist1.items.insert(0,”Select”);





what new in vs2008

15 02 2008




Send mails using SMTP

5 02 2008

public void mail(string string1, string body1)
{
//Response.Write(string1);
MailMessage mail = new MailMessage();
mail.Subject = “Sales Order Information”;
mail.From = new MailAddress(“srm@biforst.com”);
mail.To.Add(string1);
mail.IsBodyHtml = true;
mail.Body = body1;

SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential(“username”, “password”);
// client.Port = 587;
// client.EnableSsl = true;
client.Host = ” mail.biforst.com”;
client.Send(mail);





Find Duplicate rows in database query

5 02 2008

select * from sampletest group by eno,ename having count(*)>1