Serving Information Simply

Wednesday 23 January 2013

Asp.Net | C#.Net | Sql Server Tutorial: Reading data from keyword and basic Input/output i...

Asp.Net | C#.Net | Sql Server Tutorial: Reading data from keyword and basic Input/output i...: In this post I will show how to apply command-based input/output actions in C#.Net by using the Console   class. Here you will see how to ...

Reading data from keyword and basic Input/output in C#.Net


In this post I will show how to apply command-based input/output actions in C#.Net by using the Console class. Here you will see how to display information by using the Write and WriteLine methods, and how to collect input information from the keyboard by using the Read and ReadLine methods…..

Console Class in C#.Net
The Console class provides a C# application with access to the standard input, standard output, and standard error streams. Standard input is normally associated with the keyboard—anything that the user types on the keyboard can be read from the standard input stream. Similarly, the standard output stream is basically directed to the screen, as is the standard error stream.
Note:-
These streams and the Console class are only having a meaning to console applications. These are applications that run in a Command window.
Write and WriteLine Methods
You can use the Console.Write and Console.WriteLine methods to display information on the console screen in C# console programming.
Main difference is that WriteLine appends a new line/carriage return pair to the end of the output, and Write does not dot that. These Both methods are overloaded. You can call them with variable numbers and types of parameters.
For example, you can use the following code to write “100″ to the screen:
Console.WriteLine(100);
You can use the below code to write the message “I am Don of, World” to the screen:
Console.WriteLine(“I am Don of, World”);
Read and ReadLine Methods
You can get user input from the keyboard by using the Console.Read and Console.ReadLine methods.
The Read Method
According to the method named Read it reads the next character from the keyboard. It returns the int value –1 if there is no more input available. Otherwise it returns an int representing the character read.
The ReadLine Method
ReadLine reads all characters up to the end of the input line (the carriage return character). The input is returned as a string of characters. You can use the following code to read a line of text from the keyboard and display it to the screen:
string input = Console.ReadLine( );
Console.WriteLine(“{0}”, input);
Example…..
A sample program to input name and age of a person and check it to be valid voter.
using x=System.Console;
class Program
{
static void Main(string[] args)
{
x.Write("Name : ");
string name=x.ReadLine();
x.Write("Age : ");
int age=int.Parse(x.ReadLine());
if(age>=18)
x.WriteLine("Dear {0} you can vote",name);
else
x.WriteLine("Dear {0} you cannot vote",name);
}
}

Here To convert string kind of data to numeric kind of data we are using Parse () method of the corresponding data type.

More about parse keyword you will get into my next article.

Thanks …………………keep visiting…….:)



Monday 21 January 2013

Asp.Net | C#.Net | Sql Server Tutorial: Literals in C#.Net

Asp.Net | C#.Net | Sql Server Tutorial: Literals in C#.Net: Literals Literals or constants are the values we write in a conventional form whose value is obvious. In contrast to variables, litera...

Literals in C#.Net


Literals

Literals or constants are the values we write in a conventional form whose value is obvious. In contrast to variables, literals (123, 4.3, “hi”) do not change in value. These are also called explicit constants or manifest constants. I have also seen these called pure constants, but I am not sure if that terminology is agreed on. At first glance one may think that all programming languages type their literals the same way.
  
Literals represent the possible choices in primitive types for that language. Some of the choices of types of literals are often integers, floating point, Boolean and character strings. Each of these will be discussed in this Topic.

Definition - The values that we use from our side for assignment or some expression is called as literal.

Types with Examples…

o   Integral Literals
§  Default is int
·         int num=67;
§  Use l or L with long and u or U with unsigned as suffix
·         long k=67L;
·         uint num=78U;
o   Floating
§  Default is double
·         double x=5.6;
§  Use f or F with floats and m or M with decimal as suffix
·         float y=5.7; //compile time code
·         float y=5.7f;
·         decimal k=6.7M;

o   Character Literals
§  Enclosed in Single Quotes
·         char ch=’A’;
·         char ch=65; // compiler time error
·         char ch=(char)65; //correct

o   String literals
§  Enclosed in double quotes
·         string name=”Abhinav”;
§  Strings are managed by String class
§  We can also use null to show no reference

o   Boolean literals
§  Can be true or false only
§  Default is false
·         bool married=true;

Note: true, false and null are the literal value and not the keywords

Thanks keep visiting.......


Friday 18 January 2013

Data Types in C#.Net


C# allows you to declare two kinds of variables: value types and reference types. The value types hold actual values, while reference types hold references to values stored somewhere in memory. 

Also value types are allocated on the stack and are available in most programming languages. Reference types are allocated on the heap and typically represent class instances. 
All the data types in .net framework are available within the namespace System. 

There are two types of data type in C#
1.   Primitive types or predefined

Ex:  byte, short, int, float, double, long, char, bool, DateTime, string, object etc...

 2.   Non-primitive types or User Defined

Ex: class, struct, enum, interface, delegate, array.
These are Special keywords used to define type of data and range of data.

Let’s discuss in brief……

Value Types

-          It Hold the real value. Internally are structure
-          Can of different types
o   Integrals
§  Number without decimal point
§  byte – 1 byte (unsigned)
§  short – 2 bytes
§  int – 4 bytes
§  long – 8 byte
§  sbyte – 1 (singed)
§  ushort (unsigned)
§  uint
§  ulong
o   Floatings
§  float – 4 bytes (accuracy 7 decimal points)
§  double –8 bytes (accuracy 15 decimal points)
§  decimal  - 16 bytes (accuracy 28 decimal points)
o   Characters
§  char – 2 bytes
o   Boolean
§  bool – 1 byte
Note:

Use sizeof() operator to view size of a data type

Output Style

  1. Java Style
    1. Use + to concatenate the results
    2. Example
Console.WriteLine("Size of decimal is " + sizeof(decimal));

  1. C – Style Output
    1. Use place holder like {0}, {1} etc. for different set of variables
    2. Example 1
Console.WriteLine("Size of decimal is {0}", sizeof(decimal));

Example 2

int a=5, b=7;
Console.WriteLine("sum of {0} and {1} is {2}", a, b, a + b);

Reference Types

-          Reference type Used to hold the address
-          These are …
o   string – To hold address of strings only
o   object – To hold address of any data type

Date and Time

Date time is one of the most frequently used data type in C#, here I am going to let you know some of properties about it also.

DateTime CurrentTime = DateTime.Now;//display’s current Date Time. 

int Days = DateTime.DaysInMonth(2011, 7);// it displays “31”.  

Examples

DateTime Now = DateTime.Now;
Console.WriteLine("Time:" + Now.TimeOfDay);//it display only Current Time of that day.

Console.WriteLine("Current month: "+Now.Month);//it display what is Current Month.
Console.WriteLine("To Day is: "+Now.DayOfWeek);// it gives current Day name.

Try these examples and watch the results....

DateTime myDateTime = DateTime.Parse("7/28/2011 10:17:30");
TimeSpan TimeSpan = new TimeSpan(3, 4, 3, 12);
DateTime newDateTime = myDateTime + TimeSpan;
DateTime subtracttime = myDateTime - TimeSpan;
Console.WriteLine("myDateTime + TimeSpan = " + newDateTime);
Console.WriteLine("myDateTime - TimeSpan = " + subtracttime);

DateTime now = DateTime.Now;
DateTime addtwodays = DateTime.Now.AddDays(2);//it adds two days to current date and time.
DateTime addminutes = DateTime.Now.AddMinutes(50);
Console.WriteLine("Current DateTime:" + now);
Console.WriteLine("Addition of two days, DateTime:" + addtwodays);
Console.WriteLine("adition of minutes, DateTime:" + addminutes);


using System;
class timespan
{
public static void Main()
{
DateTime Time = DateTime.Now;
TimeSpan TimeSpan = new TimeSpan(24, 00, 00);
DateTime Time24 = Time.Subtract(TimeSpan);
Console.WriteLine("myTimeSpan = " + TimeSpan);
Console.WriteLine("time before 24 hours = " + Time24);
Console.ReadLine();
}
}

Thanks .Keep visiting guys......

Thursday 17 January 2013

Textbox textchanged event in Asp.Net




 
TextChanged is an event. It take place when the text is modified in a TextBox. With it we make a program possessed on the value of a TextBox when the user types. When the text in a TextBox is changed, we look up something in a database and display immediately the results.
 
Tip: Use TextChanged with logical tests to make your program liable and reactive to your users' actions.

The following example shows how you can use this event to respond to changes in the TextBox control. The code displays the contents of the Text property of the control in other textbox when the Text property is changed. 

  
The code that you need to write on the designer page that is webpage-example.aspx.
-------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head" runat="server">
    <title>using OnTextChanged event in TextBox</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">TextBox Example: OnTextChanged</h2>
        <asp:Label ID="Label1" runat="server"Text="Email"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"AutoPostBack="true"OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <br /><br />
        <asp:Label ID="Label2" runat="server"Text="Confirm Email></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"BackColor="LightGoldenrodYellow"ForeColor="Crimson"></asp:TextBox>
    </div>
    </form>
</body>
</html>




 









Now We will fire the textchnaged event of textbox for showing the value of first textbox on the other text box...
For this right click on the selected textbox then into event tab there is textchanged event double click on it>>>
go through the snapshot.
   

webpage-example.aspx.cs
    protected void TextBox1_TextChanged(object sender, System.EventArgs e)
    {
        TextBox2.Text = TextBox1.Text;
    }

EXAMPLE-
---------- 
  

Thanks...