How do you retrieve two tables of data at the same time by using data reader

Retrieve two tables of data at the same time by using data reader


Include 2 select statements either in a stored procedure or in a select command and call the ExecuteReader() method on the command object. This will automatically fill the DataReader with 2 Tables of data.

The datareader will always return the data from first table only. If you want to get the second table then you need to use ReaderObject.NextResult() method. The NextResult() method will return true if there is another table. The following code shows you how do it.



//Create the SQL Query with 2 Select statements
string SQLQuery = "Select * from Customers;Select * from Employees;";
//Create the Connection Object
SqlConnection ConnectionObject = new SqlConnection(ConnectionString);
//Create the Command Object
SqlCommand CommandObject = new SqlCommand(SQLQuery, ConnectionObject);
//Open the connection
ConnectionObject.Open();
//Execute the command. Now reader object will have 2 tables of data.
SqlDataReader ReaderObject = CommandObject.ExecuteReader();
//Loop thru the tables in the DataReader object
while (ReaderObject.NextResult())
{
while (ReaderObject.Read())
{
//Do Something
}
}
//Close the Reader
ReaderObject.Close();
//Close the Connection
ConnectionObject.Close();




See More:


Read More

Cookies in asp.net - All about HttpCookies

Cookies in asp.net 


Asp.net cookies is a small piece of information stored on the client machine.The HttpCookie class is under System.Web namespace. It contains properties and methods necessary to work with individual cookies.Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. Length of the text stored in cookies is 4096(4kb) in general but could vary from browser to browser.

Cookies are small text files that a is transferred by the site you visit or internet service provider and saved to your computer's hard drive with the help of web browser. You need to allow your site to save the file to your system. The site/ service provider recognizes your web browser and capture, remembers and stores specific information.

With the help of cookies, only the website can remember your Wishlist items, products stored in your shopping cart, your recent activity list and preferences that ultimately help us improve our service and your browsing experience. With the help of cookies, we compile site traffic and site visitors’ interaction data.

Remember, third party sites are not provided your personal information stored with us, but we may use the information and contact external websites to improve the services we offer and assist you better.

Cookies can be enabled or disabled through your browser settings. In case you face any issues look for browser help menu and search how to modify cookies settings. Turning off cookies will refrain you from features that help improve site experience and services provided to you. You can still place orders over the telephone by contacting customer service.


Cookies in asp.net



Persistent and Non-persistent cookies in ASP.NET



There two type of cookies in ASP.NET

Persistent cookies: Asp.net cookies are stored on your computer hard disk.They stay on your hard disk and can be accessed by web servers until they are deleted or have expired.

public void PersistentCookies(string name, string value)
{
 HttpCookie cookie = new HttpCookie(name);
 cookie.Value = value;
 cookie.Expires = Convert.ToDateTime("12/12/2008");
 Response.Cookies.Add(cookie);
}



Non-persistent cookies: Asp.net cookies are saved only while your web browser is running.  They can be used by a web server only until you close your browser.They are not saved on your disk.


public void NonPersistentCookies(string name, string value)

{
    HttpCookie cookie = new HttpCookie(name);
    cookie.Value = value;
    Response.Cookies.Add(cookie);
}

Read More

How to call javascript function in anchor tag href

Call javascript function in anchor tag href


<html>
<head>
<title>Call javascript function in anchor tag  href:Examples</title>
</head>
<body>
<p><a href="javascript:void(window.open('http://xyz.com/'))">
Open a new window.</a></p>
</body>

</html>


Read More

Call javascript function in body onload event : body onload javascript

Call javascript function in body onload event 




<html>
<head>
<title> body onload javascript</title>
<script type="text/javascript">

    function demo() {

        document.write("onload");
    }

</script>
</head>
<body onload="demo();">
</body>
Read More

Difference between Const and ReadOnly in c#

Difference between Const and ReadOnly



Const in c#
  • You have to initialize const variables while declaration.
  • You cannot reassign a const variable.
  • Compiler evaluates the const variables.
  • The static modifier is not allowed in a constant declaration.
  • A const field of a reference type other than string can only be initialized with null.


Example for const in c#.net



using System;
using System.Text;

namespace ProgramCAll
{

    class ProgramCall
    {
        /*A const field of a reference type other than string
        can only be initialized with null*/
        private const StringBuilder myName = null;

        public static void Main()
        {
            //You have to initilize Const varabiles while declartion
            const int myEmpId = 173524;

            //Valid scenario
            const int myNumber = 10 + 16;

            const string myName = "John";

            // Reassigning a const variable is not allowed.
            //Comment out below code to run the program
            myEmpId = 23456;

            Console.WriteLine(myEmpId);

            Console.Read();
        }
    }
}


Readonly in c#
  • readonly fields can be initialized only while declaration or in the constructor.
  • Once  you initialize a readonly field, you cannot reassign it.
  • You can use static modifier for readonly fields
  • readonly modifier can be used with reference types
  • readonly modifier can be used only for instance or static fields, you cannot use readonly keyword for variables in the methods.


Example for readonly modifier in c#.net


using System;
using System.Text;

namespace ProgramCall
{
    class MyClass
    {
        //readonly can only be used with instance or static variables
        public readonly int mynumber = 10;
        public readonly int addnumber = 10 + 15;

        //readonly modifier can be used with static varaibles       
        public static string myEmployer = "ProgramCall.Com";

        //readonly can be used with reference types
        public readonly StringBuilder name = new StringBuilder("John");

        /*  readonly varaible can be intilized in constructor but
             cannot be declared in constructor    */
        public readonly string myName;

        //readonly fields can be initlized in constructor
        public MyClass(string name)
        {
            myName = name;

        }

        private void myMethod()
        {
            /* readonly modifier cannot be used in method level variables
           the below line throws a error. */
            //  readonly int num = 5;
        }

    }

    //main class
    class ProgramCall
    {

        public static void Main()
        {
            MyClass obj = new MyClass("Bill");

            Console.Read();
        }
    }
}


Difference between const and readonly
  • const fields has to be initialized while declaration only, while readonly fields can be initialized at declaration or in the constructor.
  • const variables can declared in methods ,while readonly fields cannot be declared in methods.
  • const fields cannot be used with static modifier, while readonly fields can be used with static modifier.
  • A const field is a compile-time constant, the readonly field can be used for run time constants.
Read More

Why we use constructor in c# | All about Constructor

What is Constructor in c#


Constructors are the special type of methods that invoked when the first object of class is created. The name of constructor is same as class name. If you do not use an access modifier with the constructor it will still be public by default.


c# class constructor example




Example:

using System;

 public class Employee   //  class
{
     public Employee()   //Constructor
     {

     }

}

Some characteristics of constructors:


  1. Constructors are always public.
  2. Constructor can be overloaded
  3. Constructors do not have any return type.
  4. Constructor is automatically declared by CLR, whenever the class is instantiated.
  5. A constructor can have any number of parameters .

Why we use constructor in c#

Constructor is used to object initialization and memory allocation of the class.
Use to initialize private fields’ value of the class whenever instance or object of class is created.

Why we can not inherit constructor

Constructors are not members, so they are not inherited by derive classes, but the constructor of the base class can be invoked from the derive class.


Behaviour of constructors in case of inheritance

The role of constructor in case of inheritance, if we create an object of derive class it first call the base class constructor then derive class constructor. It means that first the base class to be initialize then derive class. If you want to see how the base class is initialize first, may you have to put debugger and do find the step by step process by using F11 key.

C# class constructor example



using System;

public class Employee   //  class
{
    public int age;
    public string name;
    public Employee()   //Constructor
    {

    }
}
class MainClass : Employee
{
    static void Main(string[] args)
    {
        MainClass obj = new MainClass();
        obj.age = 12;
        obj.name = "Jhon";
    }
}

Put debugger and see what happen next

c# class constructor example






Read More

Asp.net web forms tutorial

  Asp.net web forms tutorial | Asp.net  tutorial with c sharp

In this tutorial I am going to tell you about asp.net web forms and how you can create your own asp.net webforms. This is the first part of asp.net webforms tutorial so,today I am talking about how to  open website in visual stdio and create webform using asp.net.
Here,in this tutorial I am using Visual Studio 2015 you can take any version of VS it's depends on you.
Allright,first of all open the visual stdio or you can use shortcut to open VS open Run Dialog box by pressing window key + R, Type devenv and press enter key. After pressing enter you can see the start screen will apper in front of you.

Step 1: 
 Click on File------->New------->Website


Asp.net web forms tutorial

Step 2: After clicking website follow next step
            Visual c#----->Asp.Net Empty Website----->Ok

Asp.net web forms tutorial

Step 3: Click on view to see the Solution Explorer (ctrl+Alt+L).


Asp.net web forms tutorial

           

Step 4: Right click on Website ---->Add--->Add new item--->Visual c#--->Webform------>ok


Asp.net web forms tutorial


I hope,first part of Asp.net web forms tutorial will be helpfull for you.


bbbbbbbb




Read More

what is the use of static class in c#.net with example

what is the use of static |static class in c#|static class with example


If class having with static keyword it becomes static class. Static class can have static function static variable and static properties. We can't create an object of static class and also we can't have sub-classes of static class.(no inheritance)

Static class contains only static member and static function if they are not static we will not able to call any thing of static class.


what is the use of static class in c#.net with example


Static classes with static data members are nothing more than global variables. All static classes are sealed (you cannot derive them from a static class).

So, the question comes in mind, if we can't create object and can't inherit then what is the use of static class.(See Below)



using System;

 public static class Employee   // Static class
{
    public static string name;
    public static int age;
    public static string work()
    {
        return "Programming";
    }
}
class MainClass
{
    static void Main(string[] args)
    {
        Employee.work();      //Able to call by the class name
        Employee.age = 21;
        Employee.name = "SOMEONE NAME";
    }
}


Why we use static class:
When we have requirement to achive something like global in our application and we don't want to create an object of class again and again (because object creating is consumption of memory) then we have to go for static class. Simply we write class name and use (.) operator, we can call the members and functions available in static class.

Note: Static class is control by CLR  and CLR provides the memory to the static class it means that use have no control over the static class.
Static class is behaves like a service or helper classes.


Read More

Inheritance in oops


Inheritance in oops:


In this tutorial you will learn  Inheritance,Purpose of Inheritance and sample code. Getting the features of one class to another class is known as inheritance.

The class which is giving features to another class is known as base class or parent class.The class which getting the features from another class is known as derived class or child class.
The actual mean of using inheritance in our application is  ‘Code reusability’ and code optimization.

Similarly in Object Oriented  Programming,Inheritance is the process of inheriting properties of objects of one class by objects of another class.

In case of inheritance if we create an object of derive class or child class we are able to call 

our own method and variable and also we can call base class methods and variable,this is the cause we say that inheritance provide me code reusability.



Inheritance in oops



For Example:


using System;


class Manager              //Parent Class or Base Class
{
    public string Mgr_name;
    int Mgr_age;
    string Mgr_address;

    public string departmant()          
    {
        return "Assistant Manager";
    }
}
class Employee:Manager    //Inheritance betweentwo classes
                         //Employee is Derive class or child class
{
    int Emp_age;
    string Emp_name;
    static void Main(string[] args)    
    {
        Employee emp = new Employee();
        emp.Mgr_name = "ManagerName";
        emp.departmant();
        //we are able to call base class variable and methods
        emp.Emp_name = "EmpName";
        emp.Emp_age = 25;
        //we are able to call own(derive class) variable and methods
    }
}




Types of inheritance in oops

1) Single inheritance
2) Multilevel inheritance
3) Hierarchical inheritance
4) Multiple inheritance
5) Hybrid inheritance

Single inheritance:
In this inheritance, there is only one base class and one derived class.

Multilevel inheritance:
In this we have many base classes and many derived classes. The classes which are acts as a base class as well as derived class known as intermediate classes.

Hierarchical inheritance:
In this, there is no. of derived classes derived from one base class.

Multiple inheritance:
In this, one class derived from multiple base classes. Multiple inheritance is not supported in c# in classes. We can achieve this by interfaces.

Hybrid inheritance:
The combination of any two inheritances is known as hybrid inheritance.


Also Read:


Call javascript function in body onload event 




Read More