Monday, October 11, 2010

Console Programs//for exam


Console Programs//for exam

1. Write a console application in any .NET language that reads an array of integer numbers to count the number of odd and even numbers in the array and display the result

2. Write a C# Console Application that read a character array and count the number of vowels in that array. (For example: “portal” has 2 vowels)

3. Write a C# Console Application that generates the Factorial value of any given integer. (For example: 5! = 5*4*3*2*1)

Windows Programs

1. Write a complete Managed Windows application with a TextBox control containing a ContextMenu to change the font of text written on the TextBox

2. Write a complete program that has the following two forms with the controls as shown in figure://imp




The program should be able to perform the following tasks:
(i)                 When the user clicks Open button of form 1, form2 should open.
(ii)               When the user checks the checkbox in form 1 both of the checkboxes of form 2 should be checked and when the user uncheck the checkbox in form 1 both of the checkboxes of form 2 should be unchecked.
Similar Questions//imp
3. Write the OnPaint() Method to draw a string ".NET Programming" in Bold Italic.

4. Write a complete Windows Application that has a ComboBox control with the list of the fruits. Display the selected fruit in the another label on its change event.

5. Write a code snippet to display the OpenFile dialog box, set its filter to BMP, JPG and GIF images only, and if the user clicks ok, the selected image should be set as the background image of the form

6. Write a code snippet for an event handler to a button click so that when the button is clicked, a ColorDialog must be shown. When the user selects a color and clicks OK, the selected color should be the background color of the form

7. Write a GDI+ application that draws string “WELCOME” with red brush and fills a circle of radius 20 using the same brush in a PictureBox control when a user clicks “Draw” button

8. Write a complete Windows Application that uses Timer component to develop a digital clock in a Label control when a button is clicked//pardaina

9. Write a code snippet to register key pressing event and create event handler for the event to show which key was pressed in a Label control /pardaina

10. Write a complete Windows program that has a Label and a Button control. When the button is clicked, a ColorDialog should appear. The selected color should be the background color of the Label.//same as q.6

11. Write a windows application that has two TextBox controls taking input of numerator and denominator for division. While clicking “Divide” button, the result must be displayed on a Label. Give provisions for exceptions that might occur in the program/pardaina

12. Write a Windows application to add inputs of a textbox into a listbox and also delete the selected item in listbox with provided ‘Add’ and ‘Delete’ buttons. /pardaina

13. Write a windows program to catch DivideByZero Exception and FormatException Exception. /pardaina

14. Write a windows program with LinkLabel to open up Internet Explorer with the URL www.google.com/pardaina

15. Write a windows program with RichTextBox and the functionality to save into a file and open from the file

16. Write a windows program to change the background color of a PictureBox in a form using ColorDialog Object

17. Write a windows program to draw a filled Rectangle and Oval having SolidBrush in a PictureBox.

18. Write a program to display the position of the mouse clicked in a label in your form

19. Write simple individual windows programs illustrating the features of the controls: ComboBox, Tree, Tab.

           
Database Programs

1. Use ADO.NET classes to connect to a database and find the total number of records in a particular table

2. Write a program that connects to and disconnects from database and displays the connection state.
 
3. Write a program to select records from database and display them in ListBox.

4. Write a complete ASP.NET web Application with a code-behind page that uses appropriate ADO.NET classes to verify user's password and allow them to change it to a new password by typing the password two times. Use a validation control to ensure that the new password typed in the two textboxes are same. Make necessary assumption of your own for the database, table and its columns

5. Write a program that connects to and disconnects from database and displays the connection state.

6. Write a program to count records in table.

7. Write a program to connect to database using DataAdapter and fill the DataSet, display content in DataGrid.


Web Programs

1.      Write a Simple Web Form Page with Validators.

2. Write a Web Form with AdRotator

3. Create a Web Service that calculates Simple Interest (I) based on the Principal (P), Annual Rate (R) and Year (Y)

4. Write a web services that provides current temperature of your city

5. Write an ASP.NET web form with appropriate validation controls to ensure that an entry in a TextBox control cannot be blank and will be between 0 and 100.

6. Write a code snippet to utilize logic that executes only once when a page loads

7. Write a simple “Hello World” Web Service and steps to consume this web service

8. Write a complete ASP.NET program with code-behind page that has a CheckBox web server control and a TextBox web server control. Use validation control to assure that the TextBox is not empty. When the user clicks submit button, the typed name and the status of the CheckBox (Checked or Unchecked) should be shown in a Label control.

9. Write a complete ASP.NET web form with AdRotator control that asks for your name and displays the name in a label.

10. Write a complete ASP.NET web Application with a code-behind page that uses appropriate ADO.NET classes to verify user's password and allow them to change it to a new password by typing the password two times. Use a validation control to ensure that the new password typed in the two textboxes are same. Make necessary assumption of your own for the database, table and its columns

Write a program to select records from database and display them in ListBox.//All


Write a program to select records from database and display them in ListBox.//All
Try{
string constring=@"Data  Source=C:\anudb1.mdb;"+@"Provider=Microsoft.Jet.OLEDB.4.0";
string mySelectQuery = "Select * from Profile";                  
OleDbConnection myConnection = new OleDbConnection(constring);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);
myConnection.Open();
OleDbDataReader myReader=null;
myReader = myCommand.ExecuteReader();
while(myReader.Read())
      {
      listBox1.Items.Add(myReader["Name"]+" , "+myReader["Address"]);
      }
myReader.Close();
myConnection.Close();
}catch(){}
 
 
Write a program to connect to database using DataAdapter and fill the
DataSet, display content in DataGrid.
string strSql="Select * from Profile";
string constring=@"Data Source=C:\anudb1.mdb;"+@"Provider=Microsoft.Jet.OLEDB.4.0";
OleDbConnection con=new OleDbConnection(constring);
OleDbDataAdapter adap=new OleDbDataAdapter();              
adap.SelectCommand=new OleDbCommand(strSql,con);
DataSet ds=new DataSet();
adap.Fill(ds);
this.dataGrid1.DataSource=ds;

ASP.NET Validator Controls


ASP.NET Validator Controls  


This tutorial gives a brief overview of how to use the ASP.NET Input Validation Controls.

Back when we had only ASP, developers who had to write webpages for forms knew that the most tedious part is writing code to validate the user input. User input had to be validated so that malicious use of the pages couldn't be achieve. User input had to be validated so that an incorrect piece of information would not be entered. User input had to be validated so that the information stored was standardized. Yeah, some people had libraries of ASP functions to validate common things such as postal codes (zip codes for you Americans), e-mail addresses, phone numbers, etc. The developers of ASP.NET saw the tedium in always having to check user input. They decided that to simplify our life by including validation controls.
ASP.NET validation controls also provide two ways of validation: Server-side or Client-side. The nice thing about these Validation controls is that it will preform client-side validation when it detects the browser is able (unless client-side validation has been disabled). Thus reducing roundtrips. And it will preform server-side where necessary. This client-side/server-side detection and validation is done without extra work by the developer!

With ASP.NET, there are six(6) controls included. They are:
  • The RequiredFieldValidation Control

  • The CompareValidator Control

  • The RangeValidator Control

  • The RegularExpressionValidator Control

  • The CustomValidator Control


Validator Control Basics
All of the validation controls inherit from the base class BaseValidator so they all have a series of properties and methods that are common to all validation controls. They are:
  • ControlToValidate - This value is which control the validator is applied to.

  • ErrorMessage - This is the error message that will be displayed in the validation summary.

  • IsValid - Boolean value for whether or not the control is valid.

  • Validate - Method to validate the input control and update the IsValid property.

  • Display - This controls how the error message is shown. Here are the possible options:

    • None (The validation message is never displayed.)

    • Static (Space for the validation message is allocated in the page layout.)

    • Dynamic (Space for the validation message is dynamically added to the page if validation fails.)



The RequiredFieldValidation Control
The first control we have is the RequiredFieldValidation Control. As it's obvious, it make sure that a user inputs a value. Here is how it's used:


Required field: <asp:textbox id="textbox1" runat="server"/>
<asp:RequiredFieldValidator id="valRequired" runat="server" ControlToValidate="textbox1"
    ErrorMessage="* You must enter a value into textbox1" Display="dynamic">*
</asp:RequiredFieldValidator>
 
>

In this example, we have a textbox which will not be valid until the user types something in. Inside the validator tag, we have a single *. The text in the innerhtml will be shown in the controltovalidate if the control is not valid. It should be noted that the ErrorMessage attribute is not what is shown. The ErrorMessage tag is shown in the Validation Summary (see below).

The CompareValidator Control
Next we look at the CompareValidator Control. Usage of this CompareValidator is for confirming new passwords, checking if a departure date is before the arrival date, etc. We'll start of with a sample:


Textbox 1: <asp:textbox id="textbox1" runat="server"/><br />
Textbox 2: <asp:textbox id="textbox2" runat="server"/><br />
<asp:CompareValidator id="valCompare" runat="server"
    ControlToValidate="textbox1" ControlToCompare="textbox2"
    Operator="Equals"
    ErrorMessage="* You must enter the same values into textbox 1 and textbox 2"
    Display="dynamic">*
</asp:CompareValidator>
 
>

Here we have a sample where the two textboxes must be equal. The tags that are unique to this control is the ControlToCompare attribute which is the control that will be compared. The two controls are compared with the type of comparison specified in the Operator attribute. The Operator attribute can contain Equal, GreterThan, LessThanOrEqual, etc.
Another usage of the ComapareValidator is to have a control compare to a value. For example:


Field: <asp:textbox id="textbox1" runat="server"/>
<asp:CompareValidator id="valRequired" runat="server" ControlToValidate="textbox1"
    ValueToCompare="50"
    Type="Integer"
    Operator="GreaterThan"
    ErrorMessage="* You must enter the a number greater than 50" Display="dynamic">*
</asp:CompareValidator>
 
>

The data type can be one of: Currency, Double, Date, Integer or String. String being the default data type.

The RangeValidator Control
Range validator control is another validator control which checks to see if a control value is within a valid range. The attributes that are necessary to this control are: MaximumValue, MinimumValue, and Type.
Sample:


Enter a date from 1998:
<asp:textbox id="textbox1" runat="server"/>
<asp:RangeValidator id="valRange" runat="server"
    ControlToValidate="textbox1"
    MaximumValue="12/31/1998"
    MinimumValue="1/1/1998"
    Type="Date"
    ErrorMessage="* The date must be between 1/1/1998 and 12/13/1998" Display="static">*</asp:RangeValidator>
 
>


The RegularExpressionValidator Control
The regular expression validator is one of the more powerful features of ASP.NET. Everyone loves regular expressions. Especially when you write those really big nasty ones... and then a few days later, look at it and say to yourself. What does this do?
Again, the simple usage is:


E-mail: <asp:textbox id="textbox1" runat="server"/>
<asp:RegularExpressionValidator id="valRegEx" runat="server"
    ControlToValidate="textbox1"
    ValidationExpression=".*@.*\..*"
    ErrorMessage="* Your entry is not a valid e-mail address."
    display="dynamic">*
</asp:RegularExpressionValidator>
 
>

Here is a webpage I like to use to check my regular expressions.

The CustomValidator Control
The final control we have included in ASP.NET is one that adds great flexibility to our validation abilities. We have a custom validator where we get to write out own functions and pass the control value to this function.


Field: <asp:textbox id="textbox1" runat="server">
<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="textbox1"
    ClientValidationFunction="ClientValidate"
    OnServerValidate="ServerValidate"
    ErrorMessage="*This box is not valid" dispaly="dynamic">*
</asp:CustomValidator>
 
We notice that there are two new attributes ClientValidationFunction and OnServerValidate. These are the tell the validation control which functions to pass the controltovalidate value to. ClientValidationFunction is usually a javascript funtion included in the html to the user. OnServerValidate is the function that is server-side to check for validation if client does not support client-side validation.
Client Validation function:


<script language="Javascript">
<!--
    /* ... Code goes here ... */
-->
</script>
 
Server Validation function:


Sub ServerValidate (objSource As Object, objArgs As ServerValidateEventsArgs)
    ' Code goes here
End Sub

Validation Summary
ASP.NET has provided an additional control that complements the validator controls. This is the validation summary control which is used like:


<asp:ValidationSummary id="valSummary" runat="server"
    HeaderText="Errors:"
    ShowSummary="true" DisplayMode="List" />
 
The validation summary control will collect all the error messages of all the non-valid controls and put them in a tidy list. The list can be either shown on the web page (as shown in the example above) or with a popup box (by specifying ShowMessageBox="True")

Now you know how to use the Validator Controls in ASP.NET! Have fun!
I will also upload a sample of all the validator controls to the code sample section.

Acknoledgment: Professional ASP.NET (published by Wrox) was used a reference. It's a good book!

Tips to remember
  • If you are doing server-side validation, make sure the button onclick method has a Page.IsValid if statement or it will look like your validators aren't doing anything

  • Don't forget to wrap everything in the <form runat=server> tag.


Retrieving Schema Information from the Data Source


Retrieving Schema Information from the Data Source

Schema information can be retrieved from a data source using the FillSchema( ) method, which retrieves the schema information for the SQL statement in the SelectCommand. The method adds a DataTable to the DataSet and adds DataColumn objects to that table. Finally, it configures the AllowDBNull, AutoIncrement, MaxLength, ReadOnly, and Unique properties of the DataColumn, based on the data source. While it configures the AutoIncrement property, it doesn't set the AutoIncrementSeed and AutoIncrementStep properties. The FillSchema( ) method also configures the primary key and unique constraints for the DataTable. It doesn't configure the DefaultValue property.
In addition to an argument specifying the DataSet argument, the FillSchema( ) method takes an argument specifying whether the schema is transformed by the table mappings for the data adapter. Mapping tables and columns is discussed in more detail later in this chapter
If the FillSchema( ) method is used with a table that already has schema defined, the original schema isn't overwritten. Rather, new columns are added if they are part of the schema retrieved but don't exist in the table.
Finally, if a query returning multiple result sets is specified in the SelectCommand, only the schema from the first result set is used. To fill schemas based on queries with multiple result sets, use the Fill( ) method with the MissingSchemaAction set to AddWithKey.
The following example demonstrates the FillSchema method:
// connection and select command strings
String connString = "Data Source=(local);Integrated security=SSPI;" + 
    "Initial Catalog=Northwind;";
String selectSql = "SELECT * FROM Orders";
 
// create the data adapter
SqlDataAdapter da = new SqlDataAdapter(selectSql, connString);
 
// create a new DataSet to receive the table schema
DataSet ds = new DataSet();
// read the schema for the Orders table from the data source and
// create a table in the DataSet called "Orders" with the same schema
da.FillSchema(ds, SchemaType.Source, "Orders");
 
// create a new DataTable to receive the schema
DataTable dt = new DataTable("Orders");
da.FillSchema(dt, SchemaType.Source);
As with the Fill( ) method, the DataAdapter connection must be valid, but doesn't have to be open. If it is closed when FillSchema( ) is called, it is automatically opened
to retrieve the data and then closed. If it is open when FillSchema( ) is called, it is left open after the data is retrieved.

Updating the Data Source//imp

The Update( ) method can submit DataSet changes back to the data source. It uses the statements in the DeleteCommand, InsertCommand, and UpdateCommand objects to attempt to update the data source with records that have been deleted, inserted, or updated in the DataSet. Each row is updated individually and not as part of a batch process. Furthermore, the order in which the rows are processed is determined by the indexes on the DataTable and not by the update type. Figure below illustrates how the DataAdapter is used both to reconcile changed data in the DataSet with the data source using the Update() method and to retrieve data from the data source using the Fill() method.
Figure. Retrieving and updating data using the DataAdapter
The delete, insert, and update statements can be automatically generated using the CommandBuilder object, but this is probably not the best approach for production systems. Alternatively, custom update logic can be used where the DeleteCommand, InsertCommand, and UpdateCommand are each defined. Compared with using the CommandBuilder, custom logic can significantly improve performance and can implement solutions to complex updating and conflict-resolution scenarios.
The following example demonstrates the Update( ) method. For simplicity, a CommandBuilder generates the update logic.
// connection and select command strings
String connString = "Data Source=(local);Integrated security=SSPI;" + 
    "Initial Catalog=Northwind;";
String selectSql="SELECT * FROM Orders";
 
// create a new DataSet to receive the data
DataSet ds = new DataSet();
 
SqlDataAdapter da = new SqlDataAdapter(selectSql, connString);
 
// create the command builder
// this creates SQL statements for the DeleteCommand, InsertCommand,
// and UpdateCommand properties for the data adapter based on the
// select command that the data adapter was initialized with
SqlCommandBuilder cb = new SqlCommandBuilder(da);
 
// read all of the data from the orders table and load it into the 
// Orders table in the DataSet
da.Fill(ds, "Orders");
 
// ... code to modify the data in the DataSet
 
// update the data in the Orders table in the DataSet to the data source
da.Update(ds, "Orders");
As with the Fill( ) and FillSchema( ) methods, opening and closing the connection are performed by the data adapter, if necessary.