1DPGDCA6

download 1DPGDCA6

of 19

Transcript of 1DPGDCA6

  • 7/28/2019 1DPGDCA6

    1/19

    Q. 1 what is Net? Explain its features.

    Answer-NET is the framework for which we develop applications. It sits in between

    ourapplicationprograms and operating system. Applications developed for NET run

    inside NET and are controlled by NET. It supports both Windows and web

    applications. NET provides an object oriented environment. It ensures safe execution ofthe code by performing required runtime validations. For example, it is never possible to

    access an element of an array outside the boundary. Similarly, it is not possible to a

    program to write into another programs area, etc. The runtime validations performed

    by .NET makes the entire environment robust.

    Components of NET: -NET framework has two main components. They are:

    1. Common Language Runtime

    2. NET class library

    Features of NET: - The following are major features of NET. We will use

    these features throughout journey. Here is just a brief introduction to all key features of

    NET.

    Assemblies: - An assembly is either a DLL or EXE that forms a part of anapplication. It

    contains MSIL code that is executed by CLR. The following are other important points

    related to an assembly:

    The four parts of an assembly are: -

    Assembly Manifest - Contains name, version, culture, and information about referenced

    assemblies.

    Type metadata - Contains information about types defined in the assembly.

    MSIL MSIL code.

    Resources - Files such as BMP or JPG file or any other files required by application.

    Common Type System:- Common Type System (CTS) specifies the rules related

    to datatypes that languages must follow. As programs written in all languages are

    ultimately converted to MSIL, data types in all languages must be convertible to certain

    standard data types.

    Cross-language Interoperability: - NET provides support for language interoperability.

    However, it doesnt mean every program written in a language can be used by another

    language. To enable a program to be used with other languages, it must be created by

    following a set of rules called Cross Language Specifications (CLS).

    Q. 2 Explain method to create a new windows user interface.

    http://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=23001&ts=20040122055531&redir=http://www60.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWwP3CjwWFSkHil8j6RxgEsoCGLon%2B%2FxI4jylW1d2jb5lP1nCzYw9z8mLoOv7N3IlpZtDAQ8IbbhFRhpvwcIxzcmFOtzZu2HmxHNj53Oh368%2B%2F8rhdtbgvz6FWsqmqwkuKUlk%2BVslgzg5bZPRqIpLvB5jSxq5TTb0fcct7TDxB0WhdUKwJRmoWD036yuSENkfot%2BPlj%2FsZhxNwYMafurKVqg0Ui8xaYQVoOPnTMd2Olh3J4ywQoXiCCw%2F0refhWw%3D%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=23001&ts=20040122055531&redir=http://www60.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWwP3CjwWFSkHil8j6RxgEsoCGLon%2B%2FxI4jylW1d2jb5lP1nCzYw9z8mLoOv7N3IlpZtDAQ8IbbhFRhpvwcIxzcmFOtzZu2HmxHNj53Oh368%2B%2F8rhdtbgvz6FWsqmqwkuKUlk%2BVslgzg5bZPRqIpLvB5jSxq5TTb0fcct7TDxB0WhdUKwJRmoWD036yuSENkfot%2BPlj%2FsZhxNwYMafurKVqg0Ui8xaYQVoOPnTMd2Olh3J4ywQoXiCCw%2F0refhWw%3D%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64340&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWg22LsXwtpDwpdI%2Bu8YyWSRDAUTDv8kHDj3FW1d2jb5m%2Ftv5mp%2BUW6Iff7%2FQw8AXnl0KBiJYjPCNwSLtwcIxLtX6KR6H26DTYDu729occtttvfr%2FvNuwfpGxWgwrAsqUP4oyeXOywFwBXZXRoxyWcHFONk7znnTghwpuPFNiC2WhdcqxJwWoDGy7DyuaMtLPp7%2FEzjzP1noyY8IjWqrpUlSw1CSu1LXSCBZlTdYL%2FcvWGJZ5ZrOnfLB8kRls%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64340&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWg22LsXwtpDwpdI%2Bu8YyWSRDAUTDv8kHDj3FW1d2jb5m%2Ftv5mp%2BUW6Iff7%2FQw8AXnl0KBiJYjPCNwSLtwcIxLtX6KR6H26DTYDu729occtttvfr%2FvNuwfpGxWgwrAsqUP4oyeXOywFwBXZXRoxyWcHFONk7znnTghwpuPFNiC2WhdcqxJwWoDGy7DyuaMtLPp7%2FEzjzP1noyY8IjWqrpUlSw1CSu1LXSCBZlTdYL%2FcvWGJZ5ZrOnfLB8kRls%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64340&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWg22LsXwtpDwpdI%2Bu8YyWSRDAUTDv8kHDj3FW1d2jb5m%2Ftv5mp%2BUW6Iff7%2FQw8AXnl0KBiJYjPCNwSLtwcIxLtX6KR6H26DTYDu729occtttvfr%2FvNuwfpGxWgwrAsqUP4oyeXOywFwBXZXRoxyWcHFONk7znnTghwpuPFNiC2WhdcqxJwWoDGy7DyuaMtLPp7%2FEzjzP1noyY8IjWqrpUlSw1CSu1LXSCBZlTdYL%2FcvWGJZ5ZrOnfLB8kRls%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64340&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWg22LsXwtpDwpdI%2Bu8YyWSRDAUTDv8kHDj3FW1d2jb5m%2Ftv5mp%2BUW6Iff7%2FQw8AXnl0KBiJYjPCNwSLtwcIxLtX6KR6H26DTYDu729occtttvfr%2FvNuwfpGxWgwrAsqUP4oyeXOywFwBXZXRoxyWcHFONk7znnTghwpuPFNiC2WhdcqxJwWoDGy7DyuaMtLPp7%2FEzjzP1noyY8IjWqrpUlSw1CSu1LXSCBZlTdYL%2FcvWGJZ5ZrOnfLB8kRls%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64376&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj1UOyzCCRRBm%2F2SWAPrF2sQsCbYWO03umKEAkBQqf6OWr3GFyzJ7k93Pw%2F7WhomvWUgRAkjaOgwS4kHhQ4KIFzQjiX6%2Fc2PQP2hxLcPqz%2BBpz2889%2Bt%2FQ9vxfxgSamkrc7WB1Zcb%2BZfG7ieShkNGhZ0Jr5%2Bd84%2BCrT6YqGyqS2FKVRpTYavS%2FKDKfLOVn73QNOQ9TDphEHdo5kaUywFCSloXrEGCb2vawXg3WsqfZkzLOhvlWpmhTQ%3D%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64376&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj1UOyzCCRRBm%2F2SWAPrF2sQsCbYWO03umKEAkBQqf6OWr3GFyzJ7k93Pw%2F7WhomvWUgRAkjaOgwS4kHhQ4KIFzQjiX6%2Fc2PQP2hxLcPqz%2BBpz2889%2Bt%2FQ9vxfxgSamkrc7WB1Zcb%2BZfG7ieShkNGhZ0Jr5%2Bd84%2BCrT6YqGyqS2FKVRpTYavS%2FKDKfLOVn73QNOQ9TDphEHdo5kaUywFCSloXrEGCb2vawXg3WsqfZkzLOhvlWpmhTQ%3D%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=23001&ts=20040122055531&redir=http://www60.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWwP3CjwWFSkHil8j6RxgEsoCGLon%2B%2FxI4jylW1d2jb5lP1nCzYw9z8mLoOv7N3IlpZtDAQ8IbbhFRhpvwcIxzcmFOtzZu2HmxHNj53Oh368%2B%2F8rhdtbgvz6FWsqmqwkuKUlk%2BVslgzg5bZPRqIpLvB5jSxq5TTb0fcct7TDxB0WhdUKwJRmoWD036yuSENkfot%2BPlj%2FsZhxNwYMafurKVqg0Ui8xaYQVoOPnTMd2Olh3J4ywQoXiCCw%2F0refhWw%3D%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64340&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWg22LsXwtpDwpdI%2Bu8YyWSRDAUTDv8kHDj3FW1d2jb5m%2Ftv5mp%2BUW6Iff7%2FQw8AXnl0KBiJYjPCNwSLtwcIxLtX6KR6H26DTYDu729occtttvfr%2FvNuwfpGxWgwrAsqUP4oyeXOywFwBXZXRoxyWcHFONk7znnTghwpuPFNiC2WhdcqxJwWoDGy7DyuaMtLPp7%2FEzjzP1noyY8IjWqrpUlSw1CSu1LXSCBZlTdYL%2FcvWGJZ5ZrOnfLB8kRls%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64340&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj12POzCDhBDN%2F2iWg22LsXwtpDwpdI%2Bu8YyWSRDAUTDv8kHDj3FW1d2jb5m%2Ftv5mp%2BUW6Iff7%2FQw8AXnl0KBiJYjPCNwSLtwcIxLtX6KR6H26DTYDu729occtttvfr%2FvNuwfpGxWgwrAsqUP4oyeXOywFwBXZXRoxyWcHFONk7znnTghwpuPFNiC2WhdcqxJwWoDGy7DyuaMtLPp7%2FEzjzP1noyY8IjWqrpUlSw1CSu1LXSCBZlTdYL%2FcvWGJZ5ZrOnfLB8kRls%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=64376&ts=20040122055531&redir=http://www61.overture.com/d/sr/?xargs=02u3hs9yoaj1UOyzCCRRBm%2F2SWAPrF2sQsCbYWO03umKEAkBQqf6OWr3GFyzJ7k93Pw%2F7WhomvWUgRAkjaOgwS4kHhQ4KIFzQjiX6%2Fc2PQP2hxLcPqz%2BBpz2889%2Bt%2FQ9vxfxgSamkrc7WB1Zcb%2BZfG7ieShkNGhZ0Jr5%2Bd84%2BCrT6YqGyqS2FKVRpTYavS%2FKDKfLOVn73QNOQ9TDphEHdo5kaUywFCSloXrEGCb2vawXg3WsqfZkzLOhvlWpmhTQ%3D%3D
  • 7/28/2019 1DPGDCA6

    2/19

    Answer-Create a Windows Forms Application: -

    Close the project from Lesson 1, if it is still open in Visual Basic Express, and then on

    the File menu, click New and then selectNew Project.

    This will open up a new dialog called New Project in which you have the option to select

    the template that will be used to create the plug-in application.

    As you are working with Visual Basic Express, your installed templates are under the

    Visual Basic category. In the centre of the dialog, you can see various application

    templates that are available to you, the choice of which depends on the type of

    application you wish to create. As youre creating a simple, standalone EXE with a

    dialog-based user interface, select the Windows Forms Application template. You now

    need to select a name for your project. Enter My First Invent or Plug-in in the box near

    the bottom of the New Project dialog and select OK: The basic solution containing your

    project should now be created and loaded into the Visual Basic Express editor.

    Q. 3 What is CLR?

    Answer-The Common Language Runtime (CLR) is the environment where all programs

    in NET are run. It provides various services, like memory management and thread

    management. Programs that run in the CLR need not manage memory, as it is completely

  • 7/28/2019 1DPGDCA6

    3/19

    taken care of by the CLR. For example, when a program needs a block of memory, CLR

    provides the block and releases the block when program is done with the block. All

    programs targeted to .NET are converted to MSIL (Microsoft Intermediate Language).

    MSIL is the output of language compilers in NET . MSIL is then converted to native

    code by JIT (Just-in Time Compiler) of the CLR and then native code is run by CLR. As

    every program is ultimately converted to MSIL in .NET, the choice of language is pure

    personal. A program written inVB.NET and a program written in C# are both converted

    to MSIL. Then MSIL is converted to native code and run. So, whether you write

    program in C# or VB.NET at the end it is MSIL all that you get. It is believed VB6.0

    programmers will migrate to VB.NET and C++ and Java programmers switching

    to .NET will prefer to use C# as it more resembles those languages.

    For Java programmers, MSIL in .NET is same as Bytecode in concept. CLR is

    same as JVM (Java virtual machine). So the inevitable question is Is .NETplatform

    independent like Java? The answer is technically YES. A program written for .NET can

    run on any platform as long as .NET is made available on that platform. As ofnow, .NET runs only on Windows. So, .NET is technically platform independent but not

    real, at least not now. Efforts are on to make .NET run on Linux. The project is called as

    Mono and is currently being developed. Though some people doubt the seriousness of

    Microsoft, I will not be surprised if Microsoft comes out with NET on Linux in future. If

    that happens, all your VB NET, and C# programs can run as they are on both Windows

    and Linux.

    Q. 4 What do you understand by class view window? Explain the utility of

    immediate window.

    Answer-You can use the Class View window to view the class structure of your C++program. Classes are displayed in an outline format that allows you to navigate through

    the hierarchy to display the member functions for each class. A special branch of the

    hierarchy called Globaldisplays global functions.

    You can also use the Class View window to view function code and set

    breakpoints for a class.

    Displaying the Class View window

    Follow these steps to open the Class View window:

    Select View Classes from the C++ menu, or click on the View Classes button in

    the toolbar. A Class View window is displayed that shows the class hierarchy of your C+

    + program. Figure-1 shows an example of the Class View window.

    http://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=23269&ts=20040122055531&redir=http://www60.overture.com/d/sr/?xargs=02u3hs9yoaj1VuzTCDhBBm30Vvb2vgbAew1wduUvQ5AR4rdn4kMFJ5KBL7qVNq6qtOzNHrN7D%2F8nYr4W%2BMPbtTGa5kmhECyNZasIhJMT1ohEs9azHHvMzNdRZe4If6uxj22%2Fq8usPkvWfx9tCPkUgjk3Z9rPJwjKIHuEgIUgpUCQl62sac3m8Xmhp2TuTd4itIKbQirKCrn9%2BXlNSVWOpr%2BKo%2Buj4wIXOmbIaLZSqCF5hSpgWXy8S4nfyZlhOVy2YFrKcIzAB%2FaRzZUQ%3D%3Dhttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=23269&ts=20040122055531&redir=http://www60.overture.com/d/sr/?xargs=02u3hs9yoaj1VuzTCDhBBm30Vvb2vgbAew1wduUvQ5AR4rdn4kMFJ5KBL7qVNq6qtOzNHrN7D%2F8nYr4W%2BMPbtTGa5kmhECyNZasIhJMT1ohEs9azHHvMzNdRZe4If6uxj22%2Fq8usPkvWfx9tCPkUgjk3Z9rPJwjKIHuEgIUgpUCQl62sac3m8Xmhp2TuTd4itIKbQirKCrn9%2BXlNSVWOpr%2BKo%2Buj4wIXOmbIaLZSqCF5hSpgWXy8S4nfyZlhOVy2YFrKcIzAB%2FaRzZUQ%3D%3Dhttp://infocenter.arm.com/help/topic/com.arm.doc.dui0040d/CHDDBHJI.html#Beicehgihttp://itxt.vibrantmedia.com/al.asp?ipid=111&cc=us&ai=15598777&di=23269&ts=20040122055531&redir=http://www60.overture.com/d/sr/?xargs=02u3hs9yoaj1VuzTCDhBBm30Vvb2vgbAew1wduUvQ5AR4rdn4kMFJ5KBL7qVNq6qtOzNHrN7D%2F8nYr4W%2BMPbtTGa5kmhECyNZasIhJMT1ohEs9azHHvMzNdRZe4If6uxj22%2Fq8usPkvWfx9tCPkUgjk3Z9rPJwjKIHuEgIUgpUCQl62sac3m8Xmhp2TuTd4itIKbQirKCrn9%2BXlNSVWOpr%2BKo%2Buj4wIXOmbIaLZSqCF5hSpgWXy8S4nfyZlhOVy2YFrKcIzAB%2FaRzZUQ%3D%3Dhttp://infocenter.arm.com/help/topic/com.arm.doc.dui0040d/CHDDBHJI.html#Beicehgi
  • 7/28/2019 1DPGDCA6

    4/19

    Figure-1 the Class View window

    Viewing code from the Class View window

    Follow these steps to view the source code for a class:

    Display the Class View window.

    Click the right mouse button on a member function. A Class View window menu is

    displayed Figure-2

    Figure -2 The Class View window menu

  • 7/28/2019 1DPGDCA6

    5/19

    Select View Source from the Class View window menu to display the source code for the

    function.

    Note

    You can also double click the left mouse button on a member function to display the

    function source.

    Select Set or Edit Breakpoint... from the Execute menu if you want to add a breakpoint

    within the code you are viewing. Refer to the next section for information on how to set a

    breakpoint at function entry.

    The Immediate Window is your most valuable ally while debugging code. The

    Immediate Window is the equivalent of a debug window that allows you to watch the

    values of the variables in your code. (Bring up the Immediate Window by clicking on the

    View/Immediate Window Menu Option for Access v 2.0; View/Debug Window Menu

    Option for Access v 8.0)

    Immediate Window

    The Immediate Window can be used to return values, set values and test the validity of

    conditions.

    1. Putting a question mark before a variable or condition (e.g. ?MyVar) returns the value

    of MyVar. But remember that MyVar must be a variable local to the procedure you are

    stepping through or it must be a global variable. Asking for the value of a variable that is

    not declared in the current procedure will give you an error.

  • 7/28/2019 1DPGDCA6

    6/19

    2. To set the value of MyVar while stepping through your code, you can bring up the

    Immediate Window and set the value just like you would in the Access Basic code.

    MyVar = "The new value of this variable"

    3. You may terminate a program by typing

    End

    in the Immediate Window.

    4. If you are a teeny bit lazy about typing in the variable names in the Immediate

    Window each time you want to check their values, you could use this alternative instead:

    Debug.Print MyVar

    If you put this line at strategic points in your code you can see your code run through

    uninterrupted andsee the values in the Immediate Window!! Since there is always a

    downside to everything, there is one here too. This slows down the execution speed ofyour code tremendously. So if you do choose to use this then you either comment out

    these lines or delete them entirely from your modules.

    5. (Access v 2.0 only) One trick is to ensure that there are at least two line breaks

    between each variable whose value you wish to check. You may have noticed that if you

    write in two consecutive lines like:

    ?MyVar

    ?MyOtherVar

    and if you put the cursor on the first line and hit to check the value of MyVaragain, then the second line is overwritten by the value of MyVar, even if it is empty. So,

    if you skip a line, you can keep using these two lines over and over again without having

    to retype them in every time you want to check the values.

    (This does not happen in Access v 8.0)

    We use the Immediate Window extensively while testing our code. To check values, to

    run functions. YES! You can execute functions or subroutines by ....

    ?MyFunction(Arg1, Arg2)

    or

    ?MySub Arg1, Arg2

  • 7/28/2019 1DPGDCA6

    7/19

    Q. 5 Explain subroutine and control statement in VB.NET.

    Answer-Subroutine:-Subroutines can be thought of as miniature programs. A subroutine

    has a name attributed with it, much like a variable does. Unlike a variable, a subroutine

    doesn't hold data. Instead, it holds code. When the code in a subroutine is executed, the

    subroutine is said to be "called". Therefore, one subroutine can "call" another subroutine.Some subroutines are called automatically when certain actions are performed. For

    example, the Form_Load subroutine is automatically called when a form loads. This is

    where you have been inserting your test code before. What you were actually doing was

    adding code to this subroutine.

    The declaration of a subroutine has an optional keyword that can be used before "Sub".

    You may enter a keyword representing scope here. "Private" and "Public" are examples

    of scopes. You may have noticed that the Form_Load subroutine has the word "Private"

    before "Sub". This declares that this subroutine has a Private scope. If you don't specify

    the scope of a subroutine, Visual Basic will use a default scope. You should never rely ondefault scope, however. Get in the habit of always explicitly declaring the scope of your

    subroutines.

    Control statement:-

    Control Statements are used to control the flow of program's execution. Visual Basic

    supports control structures such as if... Then, if...Then ...Else, Select...Case, and Loop

    structures such as Do While...Loop, While...Wend, For...Next etc method. Then selection

    structure performs an indicated action only when the condition is true; otherwise the

    action is skipped.

    Syntax of the If...Then selection

    If Then

    statement

    End If

    e.g.: If average>75 Then

    txtGrade.Text = "A"

    End If

    If...Then...Else selection structure

    The If...Then...Else selection structure allows the programmer to specify that a different

    action is to be performed when the condition is True than when the condition is False.

    Syntax of the If...Then...Else selection

    If Then

    statements

    Else

    statements

    End If

    e.g.: If average>50 ThentxtGrade.Text = "Pass"

  • 7/28/2019 1DPGDCA6

    8/19

    Else

    txtGrade.Text = "Fail"

    End If

    Nested If...Then...Else selection structure

    Nested If...Then...Else selection structures test for multiple cases by

    placing If...Then...Else selection structures inside If...Then...Else structures.

    Syntax of the Nested If...Then...Else selection structure

    You can use Nested If either of the methods as shown above

    Method 1

    If < condition 1 > Then

    statements

    ElseIf < condition 2 > Then

    statements

    ElseIf < condition 3 > Then

    statements

    Else

    Statements

    End If

    Q. 7 Explain following properties of VB.NET:

    Answer-(i)Anchoring-Start a new windows projects. Add two textboxes to your form,

    and set the MultiLine properties of both to True. Change the height of the boxes.

    Click on Textbox1 and locate the Anchor property in the Properties box:

  • 7/28/2019 1DPGDCA6

    9/19

    The default is to anchor the control to the Top, Left edge of the form. Click the arrow to

    reveal a curious drop down box:

    The button in the middle represents your control. The big white areas are rather

    confusing - they don't actually do anything! To change the property, you click the smaller

    grey or white rectangles between the big white rectangle. Click again to deselect it. In the

    image below, the property has been changed so that the textbox is anchored to the Top,

    Left and Right sides of the form:

    The next image has the textbox anchored to the Right and Bottom edges of the Form:

    Notice where the cursor is in the images, and what has been changed. Click the arrow on

    the drop down box to confirm your choices.

    To see what effect this all has, do the following:

    Set the Anchor property of Textbox1 on the default ofTop, Left

    Change the Anchor property of Textbox2 to None (all the small rectangles should

    be white.)

    Run your programme and drag the edges of the Form outward. This will resize

    your form

    What you should notice is that Textbox1 stays where it is, and that the left edge of

    Textbox2 moves.

  • 7/28/2019 1DPGDCA6

    10/19

    Stop your programme from running. Change the Anchor properties of the two textboxes

    to anything you like. Run your form again and watch what happens. Try anchoring one

    textbox to the left and right of the form. Watch what happens.

    But anchoring a control to an edge of the form is a useful property to get used to, if you

    have a form that can be resized and want your controls to stay where they are.

    (ii)Docking

    Docking is similar to Anchoring, but this time the control fills a certain area of the form.

    To see how it works, click on one of your textboxes and locate the Dock property. Click

    the arrow to reveal a drop down box:

    This time, all the rectangles are like buttons. You can only dock to one side at a time, and

    the default is None. Click a button to see what it does to your textbox. Click the middle

    one, and the textbox will Fill the whole form.

    Docking is quite useful when used with the splitter control and panels, allowing you to

    create a Windows-style interface.

    (ii) Dock (iii) Causes Validation (iv) GetChildAtpoint

    Q. 10 Explain following:-

    (i) OleDbCommand class:-

    Command objects represent SQL commands or SQL stored procedures that you execute

    in a database. For example, to retrieve data from a database, you can create a connection

    object, open the connection with its Open method, and then assign the open connection

    object to the Connection property of a command object. You can then assign the

    command object to a command property of a data adapter, such as

    the SelectCommand property (which lets you retrieve rows of data from the database

    when you call the data adapter's Fill method). Besides the SelectCommand property, data

    adapters also support UpdateCommand, InsertCommand,

    and DeleteCommand properties, each of which takes connection objects that perform

    these various functions.

    As you can guess, you use OleDbCommand objects with OLE DB connections,

    and SqlCommand objects with SQL Server connections. How do you place the SQL you

    want to use in a command object? You can either assign that text to the command

  • 7/28/2019 1DPGDCA6

    11/19

    object's CommandTextproperty, or you can pass it to the command object's constructor,

    like this, where I'm selecting all the records in the pubs database's authors table:

    Dim Command1 As OleDbCommand = _

    New OleDbCommand("SELECT * FROM authors")

    Now I can set the type of the command, which, for SQL statements,

    is CommandType.Text (this is the default), and assign an open connection to the

    command's Connection property:

    Dim Command1 As OleDbCommand = _

    New OleDbCommand("SELECT * FROM authors")

    Command1.CommandType = CommandType.Text

    Connection1.Open()

    Command1.Connection = Connection1

    Now this command object is ready to go. In this case, I can assign it to a data

    adapter's SelectCommand property and execute its SQL with the data

    adapter's Fill method.

    You also can use the command object's built-in methods to execute commands in a

    database, no data adapter needed:

    ExecuteReader Executes SQL commands that return rows. Note that this

    method does notreturn a dataset, it creates a data reader, which is much more

    simplistic. See "Using a Data Reader" in the Immediate Solutions.

    ExecuteNonQuery Executes commands that do not return data rows (such

    as SQL INSERT, DELETE, UPDATE, and SET statements).

    ExecuteScalar Calculates and returns a single value, such as a sum, from a

    database.

    (ii) SqlCommand class:-

    The SqlCommand Class is used for T-SQL, SQL statements and stored procedures to

    execute against a SQL Server database. This class cannot be inherited because it is a

    sealed class. The SqlCommand is a sealed class that inherits DbCommand Class and the

    ICloneable interface.

    public sealed class SqlCommand : DbCommand, Cloneable

    The DbCommand class is a base class for database-specific classes that represent

    commands. The ICloneable interface creates a new instance of a class with the same

    value as an existing instance.

    Sealed classes are primarily used to prevent derivation. Because they can never be used

  • 7/28/2019 1DPGDCA6

    12/19

    as a base class, some run-time optimizations can make calling sealed class members

    slightly faster.

    The System.Data.SqlClient namespace is used for the SqlCommand class.This

    namespace is .NET Framework Data Provider for SQL Server.

    SqlCommand Class Constructor

    A Constructor is a method that is used for instantiating a class. It never returns a value.

    Its name is the same as the class name but it might not have a parameter or it might have

    parameters. The SqlCommand Class contains four types of constructors.

    A. SqlCommand( )

    B. SqlCommand(string)

    C. SqlCommand(string,SqlConnection)

    D. SqlCommand(string,SqlConnection,SqlTransaction)

    To explain all of them first we need to create a table tbl_employee.

    Fig 1. tbl_employee Definition

    This tbl_employee table contains some data that are used in the following methods.

    Here we define the SqlCommand class Constructors one by one.

    (A). SqlCommand()

    It is a SqlCommand class constructor. It has no parameters, which means it is a default

    constructor. We will use it to create an object of a SqlCommand class.

    //create connection to Sql server database

    SqlConnection con = new SqlConnection(@"Data Source=SANPEEP-

    PC\SANDEEP;Initial Catalog=Employee;User ID=sa; Password=123;Pooling=False");

    con.Open();

    SqlCommand cmd=new SqlCommand();//use SqlCommand() constructor

    cmd.CommandText="select Emp_id,Emp_name,Emp_exp,Emp_address from

    tbl_employee";

    cmd.Connection=con;

    SqlDataAdapter dap=new SqlDataAdapter(cmd);

    DataSet ds=new DataSet();

    dap.Fill(ds,"EmployeeTable");

  • 7/28/2019 1DPGDCA6

    13/19

    grdemployee.DataSource=ds;//grdemployee is a GridView Id

    grdemployee.DataBind();

    In the above code SqlCommand cmd=new SqlCommand() is used to create an object of

    the SqlCommand class. In this code CommandText is a property of the SqlCommand

    class object that is used to assign a string to the SqlCommand object. Inthe CommandText property we can assign a query or stored procedure for excution

    against a SQL Server database in string format. Another SqlCommand class object

    property is Connection which is used to assign a SqlConnection to the SqlCommand

    object. The remaining code excutes in a manner shown in Figure 2.

    Fig 2. Represention of DataSet connection to SqlDataAdapter

    (B). SqlCommand(string)

    It is an constructor of SqlCommand class that is used to create an object of the

    SqlCommand class. It has one parameter that is a string type which means we pass aparameter that has a SQL query or stored procedure in a string format. Like:

    //create connection to Sql server database

    SqlConnection con = new SqlConnection(@"Data Source=SANPEEP-

    PC\SANDEEP;Initial Catalog=Employee;User ID=sa; Password=123;Pooling=False");

    con.Open();

    SqlCommand cmd = new SqlCommand("select

    Emp_id,Emp_name,Emp_exp,Emp_address from tbl_employee");//use

    SqlCommand(string) constructor

    cmd.Connection=con;//con SqlConnection class object

    SqlDataAdapter dap=new SqlDataAdapter(cmd);DataSet ds=new DataSet();

  • 7/28/2019 1DPGDCA6

    14/19

    dap.Fill(ds,"EmployeeTable");

    grdemployee.DataSource=ds;//grdemployee is a GridView Id

    grdemployee.DataBind();

    (C). SqlCommand(string,SqlConnection)

    It is an another constructor of SqlCommand class. It has two parameters. The first

    parameter is a string type that is used to assign a SQL Query or Stored Procedure in

    string format. The second parameter is of SqlConnection type that is used to assign a

    connection for a database. It is the simplest way. We mostly use it comparision above.

    Like:

    //create connection to Sql server database

    SqlConnection con = new SqlConnection(@"Data Source=SANPEEP-

    PC\SANDEEP;Initial Catalog=Employee;User ID=sa; Password=123;Pooling=False");

    con.Open();

    SqlCommand cmd = new SqlCommand("selectEmp_id,Emp_name,Emp_exp,Emp_address from tbl_employee",con);//use

    SqlCommand(string,SqlConnection) constructor

    SqlDataAdapter dap=new SqlDataAdapter(cmd);

    DataSet ds=new DataSet();

    dap.Fill(ds,"EmployeeTable");

    grdemployee.DataSource=ds;//grdemployee is a GridView Id

    grdemployee.DataBind();

    (D). SqlCommand(string,SqlConnection,SqlTransaction)

    This is another constructor of SqlCommand Class that has three parameters. The first

    parameter is a string type that is used to assign a SQL query or stored procedure in string

    format. The second parameter is a SqlConnection type that is used to assign a

    SqlConnection to the SqlCommand. And the last parameter is a SqlTransaction type that

    is used to assign which transaction we are using for a particular SqlCommand. The

    SqlTransaction instance is used to define our transaction commit and rollback in our C#

    code.

    //create connection to Sql server database

    SqlConnection con = new SqlConnection(@"Data Source=SANPEEP-

    PC\SANDEEP;Initial Catalog=Employee;User ID=sa; Password=123;Pooling=False");

    con.Open();SqlTransaction mytransaction = con.BeginTransaction();//a transaction begins

    SqlCommand cmd = new SqlCommand("select

    Emp_id,Emp_name,Emp_exp,Emp_address from

    tbl_employee",con,mytransaction);//use SqlComman

    (string,SqlConnection,SqlTransaction) constructor

    mytransaction.Commit();//Method of SqlTransaction

    SqlDataAdapter dap=new SqlDataAdapter(cmd);

    DataSet ds=new DataSet();

    dap.Fill(ds,"EmployeeTable");

    grdemployee.DataSource=ds;//grdemployee is a GridView Id

    grdemployee.DataBind();

  • 7/28/2019 1DPGDCA6

    15/19

    SqlTransaction is not really used in the above metioned code; it is used when we use

    more than one DML Sql Statement.

    OUTPUT

    The above four samples of code gives us the same output no matter which we are used.

    Fig. 3 is the output of the above mentioned code.

    (iii) SqlDataAdapter class

    The SqlDataAdapter class is found in the System.Data.SqlClient namespace. It is a very

    important class in the .NET framework. It works as a bridge between DataSet and

    Database. It opens the database connection, executes the SQL statements and closes the

    connection at last. There is no need to open and close the connection. It has two more

    important methods Fill() and Update(). The Fill() method is used to fill the DataSet or

    DataTable and Update() is used for saving the changes to the Database in same order as

    the DataSet, which is made with DataSet.

    Now we will work with SqlDataAdapter in our application. I have a Database student and

    a Database table as "student_detail" which has one record. We take a window form

    application in Visual Studio 2010 > Take 2 button and 1 DataGridView control and writethe following code.

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Data.SqlClient;

    namespace adapter

    {

    publicpartialclassForm1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    SqlDataAdapteradapter;

    DataSet ds;

    DataTable dt;

  • 7/28/2019 1DPGDCA6

    16/19

    string connstring = "database=student;server=.;user=sa;password=wintellect";

    privatevoid Form1_Load(object sender, EventArgs e)

    {

    }

    privatevoid btndataset_Click(object sender, EventArgs e){

    // Using DataSet

    adapter = newSqlDataAdapter("select * from student_detail", connstring);

    ds = newDataSet();

    adapter.Fill(ds);

    dGVshowrecord.DataSource = ds.Tables[0];

    //********

    // Same operation can be performed by below code

    //********

    //adapter = new SqlDataAdapter();

    //SqlConnection con = new SqlConnection(connstring);

    //adapter.SelectCommand = new SqlCommand("select * from student_detail",

    con);

    //ds = new DataSet();

    //adapter.Fill(ds);

    //dGVshowrecord.DataSource = ds.Tables[0];

    }

    privatevoid btndatatable_Click(object sender, EventArgs e)

    {

    // Using DataTable

    adapter = newSqlDataAdapter("select * from student_detail", connstring);

    dt = newDataTable();

    adapter.Fill(dt);

    dGVshowrecord.DataSource = dt;

    //********

    // Same operation can be performed by below code

    //********

    //adapter = new SqlDataAdapter();

    //SqlConnection con = new SqlConnection(connstring);

    // adapter.SelectCommand = new SqlCommand("select * from student_detail",

    con);

    //dt = new DataTable();

    //adapter.Fill(dt);

    //dGVshowrecord.DataSource = dt;

    }

    }

    }

  • 7/28/2019 1DPGDCA6

    17/19

    Run the application.

    Output

    Click on both buttons to show records from "student_detail" table.

    Now we insert, delete and update records using the SqlDataAdapter class. We will use

    the update() method of SqlDataAdapter and also we have to use the SqlCommandBuilder

    class for performing such an operation. Add 2 buttons in your application and set

    their text property to "Clear" and "Update". Replace the preceding code with the

    following code.

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Data.SqlClient;

    namespace adapter

    {

    publicpartialclassForm1 : Form{

  • 7/28/2019 1DPGDCA6

    18/19

    public Form1()

    {

    InitializeComponent();

    }

    SqlDataAdapteradapter;

    DataSet ds; DataTable dt;

    string connstring = "database=student;server=.;user=sa;password=wintellect";

    privatevoid Form1_Load(object sender, EventArgs e)

    {

    }

    privatevoid btndataset_Click(object sender, EventArgs e)

    {

    // Using DataSet

    adapter = newSqlDataAdapter("select * from student_detail", connstring);

    ds = newDataSet();adapter.Fill(ds);// fill the DataSet

    dGVshowrecord.DataSource = ds.Tables[0];// Binding DataGridView with

    DataSet

    }

    privatevoid btnupdate_Click(object sender, EventArgs e)

    {

    SqlCommandBuildercmb = newSqlCommandBuilder(adapter);// Creating

    instance of SqlCommandBuilder

    adapter.Update(ds);// updating changes

    }

    privatevoid btnclear_Click(object sender, EventArgs e)

    {

    ds.Clear();// Clear the DataSet

    }

    }

    }

    Run the application

    Output

    Click the "Using Dataset" button. It will show record from Database.

  • 7/28/2019 1DPGDCA6

    19/19

    Make some changes in records of DataGridView. I have made some changes by adding

    some new row and updating course of first row. Click the "Update" button. It will save

    the current record of DataSet into Database.

    Now click the Clear button to clear the DataSet.

    Now click the "Using Dataset" button. It will show the record from the Database.