27
ﻟﻠﻐﺔ اﻟﻤﺤﺠﻮزة اﻷﺳﺎﺳﯿﺔ اﻟﻜﻠﻤﺎتReserved Words abstract decimal float namespace return typeof as default for new sbyte uint base delegate foreach null sealed ulong bool do goto object short unchecked break double if operator sizeof unsafe byte else implicit out stackalloc ushort case enum in override static using catch event int params struct virtual char explicit interface private switch volatile checked extern internal protected this void class false is public throw while const finally lock readonly true continue fixed long ref try داﺧﻞ اﻟﺒﯿﺎﻧﺎت أﻧﻮاعC# Built-in Data Types Data Type Range brief byte 0 .. 255 byt sbyte -128 .. 127 sbt short -32,768 .. 32,767 srt ushort 0 .. 65,535 urt int -2,147,483,648 .. 2,147,483,647 int uint 0 .. 4,294,967,295 unt long -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807 lng ulong 0 .. 18,446,744,073,709,551,615 ung float -3.402823e38 .. 3.402823e38 flt double -1.79769313486232e308 .. 1.79769313486232e308 dbl decimal -79228162514264337593543950335.. 79228162514264337593543950335 dcm char A Unicode character. chr string A string of Unicode characters. str bool True or False. bln object An object. obj string strName; // warning The variable declared but never used double dblAge = 20.5; int intA, intB; int intC = 10, intD, intF = 20; const string conMsg = "Hello"; const bool bolA = true, bolB = false; string strA; const string conA = "Text"; اﻟﺘﻌﻠﯿﻘﺎتComments // this a single line comment. PDF created with pdfFactory trial version www.pdffactory.com

c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Embed Size (px)

Citation preview

Page 1: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Reserved Wordsالكلمات األساسیة المحجوزة للغة abstract decimal float namespace return typeof as default for new sbyte uint base delegate foreach null sealed ulong bool do goto object short unchecked break double if operator sizeof unsafe byte else implicit out stackalloc ushort case enum in override static using catch event int params struct virtual char explicit interface private switch volatile checked extern internal protected this void class false is public throw while const finally lock readonly true continue fixed long ref try

C# Built-in Data Typesأنواع البیانات داخل

Data Type Range brief byte 0 .. 255 byt

sbyte -128 .. 127 sbt short -32,768 .. 32,767 srt

ushort 0 .. 65,535 urt int -2,147,483,648 .. 2,147,483,647 int

uint 0 .. 4,294,967,295 unt long -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807 lng

ulong 0 .. 18,446,744,073,709,551,615 ung float -3.402823e38 .. 3.402823e38 flt

double -1.79769313486232e308 .. 1.79769313486232e308 dbl decimal -79228162514264337593543950335..

79228162514264337593543950335 dcm

char A Unicode character. chr string A string of Unicode characters. str

bool True or False. bln object An object. obj

string strName; // warning The variable declared but never used double dblAge = 20.5; int intA, intB; int intC = 10, intD, intF = 20; const string conMsg = "Hello"; const bool bolA = true, bolB = false; string strA; const string conA = "Text";

Commentsالتعلیقات // this a single line comment.

PDF created with pdfFactory trial version www.pdffactory.com

Page 2: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

/* this a Multiline comment That ends here. */

Expressionالتعبیرات

protected void Page_Load(object sender, EventArgs e) { const double conPi = 3.14159; double dblRadius, dblArea; dblRadius = 1; dblArea = conPi * dblRadius * dblRadius; Response.Write("Area = " + dblArea); }

Arithmetic Operatorsالعوامل الحسابیة : أوالOperator Description Usage

Example Value/Result

+ Addition 8 3 + 5 الجمع - Subtraction 2 3 - 5 الطرح

Unary Negation 2- 2- السالب * Multiplication الضرب 5 * 3 15 / Division 5 3 / 15 القسمة

% Modulus 2 3 % 17 باقي القسمة الصحیحة + +

Increment 1 and then return value ) prefix(ثم إرجاع قیمة 1الزیادة أوال بمقدار

int x=5 , y=3; y = ++ x;

y = 6 x = 6

Return value and then increment 1 )postfix( 1إرجاع القیمة أوال ثم زیادة القیمة

int x=5 , y=3; y = x ++;

y = 5 x = 6

- - Decrement 1 and then return value )prefix(ثم إرجاع القیمة 1إنقاص القیمة

int x=5 , y=3 ; y = --x ;

y=4 x=4

Return value and then decrement 1 )postfix( 1إرجاع القیمة أوال ثم إنقاص

int x=5 , y=3; y = x--;

y=5 x=4

PDF created with pdfFactory trial version www.pdffactory.com

Page 3: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Assignment Operatorsعوامل التخصیص : ثانیاOperator Description Example Result

int x= 5+3; x=8 األیمن المتغیر األیسر یأخذ ناتج التعبیر أو المتغیر =تزید قیمة المتغیر األیسر بمقدار قیمة المتغیر أو التعبیر األیمن =+

وتعمل أیضا مع النصوصint x=2; x += 3;

x=5

تنقص من قیمة المتغیر األیسر بمقدار قیمة المتغیر أو التعبیر =- األیمن

int x=2; x -= 5;

x=-3

;Int x=2 تضرب قیمة المتغیر األیسر في قیمة المتغیر أو التعبیر األیمن =*x *= 5;

x=10

;int x=10 یتم قسمة قیمة المتغیر األیسر علي قیمة المتغیر أو التعبیر األیمن =/x /=2 ;

x=5

قسمة قیمة المتغیر األیسر علي قیمة المتغیر أو التعبیر األیمن یتم =% .ثم یضع للمتغیر األیسر ما تبقي من القسمة الصحیحة

int x=11; x%=2;

x=1

Write it Don't write it variable *= number; variable = variable * number; variable /= number; variable = variable / number;

variable %= number; variable = variable % number; variable += number; variable = variable + number; variable -= number; variable = variable - number;

Concatenation String Operatorعامل دمج النصوص : ثالثاstring x ="Welcome to", y="C# programming!"; x= x+y; Response.Write(x);

!Welcome toC# programming

string x ="Welcome to", y="C# programming!"; x= x + " " + y; Response.Write(x);

string x = "Welcome to", y = "C# programming!"; x += " "; x += y; Response.Write(x);

!Welcome to C# programming

Conditional Expression Ternaryعامل التعبیر الشرطي الثالثي : رابعا

(Condition) ? value1 if true : value2 if false

protected void Page_Load(object sender, EventArgs e) { intFirst = 10, intSecond = 20, intLarge; intLarge = (intFirst > intSecond) ? intFirst : intSecond; Response.Write("<h2>Large Number Is:" + intLarge + "</h2>");

PDF created with pdfFactory trial version www.pdffactory.com

Page 4: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

}

Large Number Is:20

Comparison (Relational) Operatorsعوامل المقارنة : خامسا

Operator Description Usage Example Value/Result == Equal int x= 5, y= 6 ,z;

z = (x == y)? x : y;

z = 6

!= Not Equal int x= 5, y= 6 ,z; z = (x != y)? x : y;

z =5

< Less Than int x= 5, y= 6 ,z; z = (x < y)? x : y;

z =5

<= Less Than or Equal int x= 5, y= 6 ,z; z = (x <= y)? x : y;

z =5

> Greater Than int x= 5, y= 6 ,z; z = (x > y)? x : y;

z =6

>= Greater Than or Equal int x= 5, y= 5 ,z; z = (x >= y)? x : y;

z =5

Logical Operatorالعوامل المنطقیة : سادساOperato

r Description Usage Example Result

وتعني أن یكون ) And(ومعناھا && trueالمعمولین قیمتھما

int x = 5, y = 5, z ; z = ((x == y) && (x != 5)) ? x : 10 ;

z = 10

وتعني أن یكون إحدى ) Or(ومعناھا || علي األقل trueالمعمولین قیمتھ

int x = 5, y = 5, z ; z = ((x == y) || (x != 5)) ? x : 10 ;

z =5

وتأخذ معامل وحید ) Not(ومعناھا ! falseتجعلھ trueوتنفیھ إن كان

trueتجعلھ falseوإن كان

int x = 5, y = 5, z ; z = ( !(x == y) || (x != 5)) ? x : 10 ;

z =10

Bit Manipulationعوامل المعالجة علي مستوي وحدات البت : سابعاCategory Operator Description Bit Shift >> Shift right

<< Shift left Bitwise Logical & Integer bitwise AND, boolean logical AND

| Integer bitwise OR, boolean logical OR ^ Integer bitwise XOR, boolean logical XOR

Bitwise Assignment >>= , <<= , &= , |= , ^= Unary Bitwise ~ One's complement (unary NOT) – negative

Other Operatorsعوامل أخري :ثامنا

Operator Description Usage Example Result

PDF created with pdfFactory trial version www.pdffactory.com

Page 5: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

x is type یرجع العاملis القیمةtrue إن كانx من

نفس النوع المذكور type وترجعfalse في

.باقي الحاالت

int x=5; string y; y= (x is short) ? "yes" : "no";

y = no

typeof(x) ترجع نوع البیاناتdata type الخاص بــ

x

int x = 5; Type z = typeof(x)

type of variable z is int

new Type(...)

إنشاء الكائن عاملobject والنائب

delegate

Random intRnd = new Random(); intRnd.Next(1,7);

Print random number between 1:6

new Type[...]

عامل إنشاء المصفوفات Arrays

int[] intArr=new int[4]; intArr[0] = 10; intArr[1] = 20; Response.Write(intArr[1]);

20

x as Type ترجع كل قیمx منالمحدد وإن typeالنوع

لم تكن القیم من نفس nullالنوع یرجع القیمة

object[] objArr= new object[3]; objArr[0]=123; objArr[1]=false; objArr[2]="hello"; string s1 = objArr[1] as string; string s2 = objArr[2] as string;

s1=null s2="hello"

default(T)

یرجع القیمة االفتراضیة Typeللنوع المحدد

Response.Write(default(string)); Response.Write(default(int)); Response.Write(default(bool));

null 0 False

X ?? y إذا كانت قیمةx خالیةnull فیأخذ قیمةy بدال

عنھا وغیر ذلك یأخذ xقیمة

Response.Write("yes"?? "ok"); Response.Write(null ?? "ok");

Yes ok

Explicit Convert Operatorعامل التحویل الصریح :تاسعا

protected void Page_Load(object sender, EventArgs e) { double x = 250.6 ; int y; y = (int)x; Response.Write("<h2> Y = " + y + "</h2>"); // Printed Y = 250 }

Operators Precedenceتنفیذ العوامل أولویةCategory Operators Primary ( ) , x++, x--, new, typeof 1

Unary +, -, !, ++x, --x 2 Arithmetic — Multiplicative *, /, % 3

Arithmetic — Additive +, - 4 Shift <<, >> 5

Relational and type testing <, >, <=, >=, is 6 Equality ==, != 7

precedenceLogical, in order of &, ^, | 8 precedenceConditional, in order of &&, ||, ?: 9 Assignment =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= 10

PDF created with pdfFactory trial version www.pdffactory.com

Page 6: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Conversion Between Data Typesالتحویل بین أنواع البیانات Implicit Conversionالتحویل الضمني : أوال

int intNum =25 ; double dblNum; dblNum = intNum; //implicitly box the int value

int intNum =25 ; double dblNum=25.98537 ; intNum = dblNum ; // Error : Cannot implicitly convert

From To sbyte short, int, long, float, double, or decimal byte short, ushort, int, uint, long, ulong, float, double, or decimal

short int, long, float, double, or decimal ushort int, uint, long, ulong, float, double, or decimal

int long, float, double, or decimal uint long, ulong, float, double, or decimal long float, double, or decimal char ushort, int, uint, long, ulong, float, double, or decimal float double

ulong float, double, or decimal bool No possible implicit conversions to other simple types.

decimal No possible implicit conversions to other simple types. double No possible implicit conversions to other simple types.

Explicit Conversionالتحویل الصریح : ثانیا

int intNum; double dblNum=25.98537 ; intNum =(int) dblNum ;// explicitly box the double value

int intNum; double dblNum=25.98537 ; intNum = Convert.ToInt32 (dblNum); // explicitly box the double value

Convert.

( ToBoolean, ToByte, ToChar, ToDateTime, ToDecimal, ToDouble, ToInt16, ToInt32, ToInt64, ToSByte, ToSingle, ToString, ToUInt16, ToUInt32, ToUInt64 )

Statements Programmingالجمل البرمجیة

Category Description Declaration statements Used to declare local variables and constants. Expression statements Used to evaluate expression. Selection statements Used to select one of a number of possible statements for

execution based on the value of some expression. In this group are the (if and switch) statements.

Iteration statements Used to repeatedly execute an embedded statement. In this group are the (while, do, for, and foreach) statements.

PDF created with pdfFactory trial version www.pdffactory.com

Page 7: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Jump statements Used to transfer control. In this group are the break, continue, goto, throw, return, and yield statements.

Exception Handling statements

The try...catch statement is used to catch exceptions that occur during execution of a block, and the try...finally statement is used to specify finalization code that is always executed, whether an exception occurred or not.

checked and unchecked statements

Used to control the overflow checking context for integral-type arithmetic operations and conversions.

using statement Used to obtain a resource, execute a statement, and then dispose of that resource.

Declaration Statementجملة التعریف -1Syntax of Declaration Statement

var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3};

var x; // Error, no initializer to infer type from var y = {1, 2, 3}; // Error, array initializer not permitted var z = null; // Error, null does not have a type var u=5 , m=3 ; // Error, cannot have multiple declarators var v = v++; // Error, initializer cannot refer to variable itself

Expression Statement) تعبیرال(الجبریة اتجملة العبار -2

Syntax of Expression Statements

1-Local-variable-declaration: a) Local-variable-type Identifier b) Local-variable-type Identifier = local-variable-initializer c) var Identifier = local-variable-initializer 2- local-constant-declaration: const type identifier = constant-expression

expression-statement: invocation-expression

object-creation-expression assignment post-increment-expression post-decrement-expression pre-increment-expression pre-decrement-expression

PDF created with pdfFactory trial version www.pdffactory.com

Page 8: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

if Statementجملة -3Syntax of If Statement

protected void Page_Load(object sender, EventArgs e) { if (DateTime.Now. Hour< 12) Response.Write("<h2>Good morning</h2>"); Response.Write("<h2>" + DateTime.Now + "</h2>"); }

protected void Page_Load(object sender, EventArgs e) { if (DateTime.Now.Hour < 12) { Response.Write("<h2>Good morning</h2>"); Response.Write("<h2>" + DateTime.Now + "</h2>"); } }

if-statement:

1- if ( boolean-expression ) embedded-statement 2- if ( boolean-expression ) embedded-statement

else embedded-statement

PDF created with pdfFactory trial version www.pdffactory.com

Page 9: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

protected void Page_Load(object sender, EventArgs e) { if (DateTime.Now.Second < 12) { Response.Write("<h2>Good morning</h2>"); Response.Write("<h2>" + DateTime.Now + "</h2>"); } else Response.Write("<h2>Good evening</h2>"); }

protected void Page_Load(object sender, EventArgs e) { Random rnd = new Random(); string strLGrade = "" ; int intGrade = rnd.Next(100); //random number between 0 and 100 Response.Write("<h2>your grade is: " + intGrade + "</h2>"); if (intGrade >= 90) { strLGrade = "A"; Response.Write("<p>You got A! Congratulations.</p>"); } else if (intGrade >= 80) strLGrade = "B"; else if (intGrade >= 70) strLGrade = "C"; else if (intGrade >= 60) strLGrade = "D"; else if (intGrade < 60) { strLGrade = "F"; Response.Write("<p>You failed I'm sorry.</p>"); } Response.Write("Your Letter Grade: " + strLGrade); }

جملة انتقل إلي وجملة العنوان وجملة الھروب وجملة االختیار -4 goto, Labeled, break and switch statements

Syntax:

PDF created with pdfFactory trial version www.pdffactory.com

Page 10: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

protected void Page_Load(object sender, EventArgs e) { int x=1; next: Response.Write(x +"<br/>"); x++; if (x < 5) goto next; }

switch Statementجملة Syntax of switch Statement

protected void Page_Load(object sender, EventArgs e) { switch(DateTime.Now.Month) { case 1: Response.Write("January"); break; case 2: Response.Write("February"); break; case 3: Response.Write("March"); break; case 4: Response.Write("April"); break; case 5: Response.Write("May"); break; case 6: Response.Write("June"); break; case 7: Response.Write("July"); break; case 8: Response.Write("August"); break;

case 9: Response.Write("September"); break; case 10: Response.Write("October"); break;

1- labeled-statement: identifier : statement

2- goto-statement: a) goto identifier ; b) goto case constant-expression ; c) goto default ;

3- break-statement: break ;

switch-statement:

switch ( expression ) { case constant-expression : statement-list ; break; case … default : statement-list ; break;

PDF created with pdfFactory trial version www.pdffactory.com

Page 11: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

case 11: Response.Write("November"); break; default: Response.Write("December"); break; } // end of switch } //end page_load method

switch(strA) {case "ok": case "OK": x=2*3; //code execute if strA="ok" or strA="OK" break; }

while Statementجملة -5Syntax of while Statement

protected void Page_Load(object sender, EventArgs e) { int intCounter = 1; //declare and initialize control variable while (intCounter < 7) { Response.Write("<h2>Counter= "+ intCounter +"</h2>" ); intCounter ++; //increment control variable } //end while }

do Statementجملة -6Syntax of do Statement

while-statement: while ( boolean-expression ) embedded-statement

do-statement: do embedded-statement while ( boolean-expression ) ;

PDF created with pdfFactory trial version www.pdffactory.com

Page 12: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

protected void Page_Load(object sender, EventArgs e) { int intCounter = 1; //declare and initialize control variable do { Response.Write("<h2>Counter= "+ intCounter +"</h2>" ); intCounter ++; //increment control variable } while (intCounter < 7); }

for Statementجملة -7

Syntax of for Statement

protected void Page_Load(object sender, EventArgs e) {

for( int intCounter = 1; intCounter < 7; intCounter ++) Response.Write("<h2>Counter= "+ intCounter +"</h2>" ); }

foreach Statementجملة -8

Syntax of foreach Statement

protected void Page_Load(object sender, EventArgs e) { string[] strProducts = new string[5] { "CPU", "Monitor", "keyboard", "Speaker", "Printer" }; Response.Write("<ol>My Products :"); foreach (var strProduct in strProducts) Response.Write("<li>" + strProduct + "</li>"); Response.Write("</ol>"); }

for-statement: for ( for-initialize ; boolean-expression ; statement-expression-list

)

foreach-statement: foreach ( local-variable-type identifier in expression ) embedded-statement

PDF created with pdfFactory trial version www.pdffactory.com

Page 13: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

continueجملة -9

Syntax of continue Statement

protected void Page_Load(object sender, EventArgs e) { for (int x = 1; x < 10; x++) { if (x % 2 == 1) continue; Response.Write(x + "<br/>"); } }

lockجملة -10

Syntax of lock Statement

protected void Page_Load(object sender, EventArgs e) { decimal dcmBalance=3000; decimal dcmAmount=500; lock (this) { if (dcmAmount < dcmBalance) { dcmBalance -= dcmAmount; // safety to work with database Response.Write("Current Balance =" + dcmBalance); }//end if }//end lock }//end method

protected void Page_Load(object sender, EventArgs e) { using (System.IO.TextWriter w = System.IO.File.CreateText("c:\\FromWebSite.txt")) { w.WriteLine("Dr:"); w.WriteLine("Adel Sabour Ahmad"); } }

continue-statement: continue ;

lock-statement: lock ( expression ) embedded-statement

PDF created with pdfFactory trial version www.pdffactory.com

Page 14: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Namespaceسماء تفرعات األ

System.Data.OleDb.OleDbConnection

<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { using (TextWriter w = File.CreateText("c:\\FromWebSite.txt")) { w.WriteLine("Mr:"); w.WriteLine("Adel Sabour"); } using (TextReader r = new StreamReader("c:\\FromWebSite.txt")) { string strLine; while((strLine=r.ReadLine()) != null ) Response.Write("<h2>"+ strLine +"</h2>"); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <body></body></html>

Handling Exception Statementsجملة معالجة االستثناءات -11

Syntax of Handling Exception Statement

try-statement: 1- try block catch-clauses 2- try block finally-clause 3- try block catch-clauses finally-clause

try embedded-statement catch (class-type identifier) embedded-statement finally embedded-statement

PDF created with pdfFactory trial version www.pdffactory.com

Page 15: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

ý Exception o SystemException

§ ArithmeticException • DivideByZeroException • OverflowException • …

§ FormatException § ExternalException

• DbException o OleDbException o OracleException o SqlException o …

§ … o SmtpException o …

protected void Page_Load(object sender, EventArgs e) { int intX = 0, intY; try { intY = 215 / intX; Response.Write("intY = " + intY); } catch (DivideByZeroException expZero) { Response.Write("<h4 style='color:red'>Exception: Divide By Zero</h3>"); } catch (Exception exp) { Response.Write("<h4 style='color:red'>Exception: " + exp.Message + "</h3>"); } finally

PDF created with pdfFactory trial version www.pdffactory.com

Page 16: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

{ Response.Write("Execution of sensitive code is complete"); } }

check and uncheck Statementsجمل الفحص -12

Syntax of check and uncheck Statements

int intY = int.MaxValue; // then intY=2147483647 int intX = intY+5; Response.Write("intX =" + intX);

intX =-2147483644

int intY = int.MaxValue; try { int intX = checked(intY + 5); Response.Write("intX =" + intX); } catch (OverflowException expOver) { Response.Write("<h4 style='color:red'>Exception: Arthimetic Overflow!</h4>"); } catch(Exception exp) { Response.Write("<h4 style='color:red'>Exception:" + exp.Message + "</h4>"); }

checked-statement: checked block

unchecked-statement: unchecked block

PDF created with pdfFactory trial version www.pdffactory.com

Page 17: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

throw Statementجملة إنتاج استثناء -13

Syntax of throw Statement

protected void Page_Load(object sender, EventArgs e) { Random rndMonth = new Random(); switch(rndMonth.Next(1,14) ) { case 1: Response.Write("January"); break; case 2: Response.Write("February"); break; case 3: Response.Write("March"); break; case 4: Response.Write("April"); break; case 5: Response.Write("May"); break; case 6: Response.Write("June"); break; case 7: Response.Write("July"); break; case 8: Response.Write("August"); break; case 9: Response.Write("September"); break; case 10: Response.Write("October"); break; case 11: Response.Write("November"); break; case 12: Response.Write("December"); break; default: throw new ArgumentOutOfRangeException("Bad Month"); } }

returnجملة -14

Syntax of return Statement

returntype methodname( parameterlist ) { //method body statements }

throw-statement: throw expression(new System.Exception("Msg") ) ;

return-statement: return expression ;

PDF created with pdfFactory trial version www.pdffactory.com

Page 18: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

<script runat="server"> void randomNumbers() { Random rndNum = new Random(); Response.Write("<h3>Random Number :" + rndNum.Next() +"</h3>"); } protected void Page_Load(object sender, EventArgs e) { randomNumbers(); randomNumbers(); randomNumbers(); } </script>

<script runat="server"> int randomNumbers() { Random rndNum = new Random(); return rndNum.Next(); } protected void Page_Load(object sender, EventArgs e) { Response.Write("<h2>First Random Number:" + randomNumbers() + "</h2>"); Response.Write("Second Random Number IS" + randomNumbers()); Response.Write("<br>Third Random Number=" + randomNumbers()); } </script>

<script runat="server"> int randomNumbers(int Min,int Max) { Random rndNum = new Random(); return rndNum.Next(Min,Max+1);

PDF created with pdfFactory trial version www.pdffactory.com

Page 19: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

} protected void Page_Load(object sender, EventArgs e) { Response.Write("Random Number Between 5 and 8= " + randomNumbers(5,8) ); Response.Write("<br>Between 1000 and 1010=" + randomNumbers(1000,1010)); Response.Write("<br>Number Between 10 and 50=" + randomNumbers(10,50)); } </script>

Scope of declarationsالتعریفات مجال<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> int intScript = 1, intx=2; protected void Page_Load(object sender, EventArgs e) { int intMethod = 3; if(intMethod ==3) { int intx=4,intIf=5; Response.Write("<br/>From if Statement >> intx = " + intx); Response.Write("<br/>From if Statement >>intMethod=" + intMethod); Response.Write("<br/>From if Statement >>intScript=" + intScript); Response.Write("<br/>From if Statement >>intif=" + intif); } // do'nt use intx here Response.Write("<br/>From Method >>intMethod=" + intMethod); Response.Write("<br/>From Method >>intScript=" + intScript); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <% int intPage = 5; Response.Write("<br/>From Body >>intScript=" + intScript); Response.Write("<br/>From Body >> intx = " + intx); Response.Write("<br/>From Body >>intPage=" + intPage ); %> </body> </html>

PDF created with pdfFactory trial version www.pdffactory.com

Page 20: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Registration Service (2) <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Registration Form</title> <style type="text/css"> .style1 { width: 70%; border-style: none; border-width: 1px; } </style> </head> <body> <form id="form1" runat="server"> <div> <table align="center" cellspacing="3" class="style1" dir="ltr"> <tr> <td colspan="2" style="font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: large; text-decoration: underline overline; background-image: url('images/back2.GIF'); background-repeat: repeat-y"> Sign Up for Your New Account</td> </tr> <tr> <td > <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Full Name :" /> </td> <td> <asp:TextBox ID="txtName" runat="server" Font-Bold="True"></asp:TextBox> </td> </tr> <tr> <td>

PDF created with pdfFactory trial version www.pdffactory.com

Page 21: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

<asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Gender :" /> </td> <td> <asp:RadioButtonList ID="rblGender" runat="server" Font-Bold="True" RepeatDirection="Horizontal" Width="168px"> <asp:ListItem Selected="True" Value="M">Male</asp:ListItem> <asp:ListItem Value="F">Female</asp:ListItem> </asp:RadioButtonList> </td> </tr> <tr> <td> <asp:Label ID="Label3" runat="server" Font-Bold="True" Text="Country :" /> </td> <td> <asp:DropDownList ID="ddlCountry" runat="server" Width="168px"> <asp:ListItem>Egypt</asp:ListItem> <asp:ListItem>Oman</asp:ListItem> <asp:ListItem>Kuwait</asp:ListItem> <asp:ListItem>Sodan</asp:ListItem> <asp:ListItem>Lebya</asp:ListItem> <asp:ListItem>Bahreen</asp:ListItem> <asp:ListItem>Qatar</asp:ListItem> <asp:ListItem>Other</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td> <asp:Label ID="Label4" runat="server" Font-Bold="True" Text="E-Mail :" /> </td> <td> <asp:TextBox ID="txtEmail" runat="server" Font-Bold="True"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="Label5" runat="server" Font-Bold="True" Text="Security Question :"></asp:Label> </td> <td> <asp:TextBox ID="txtQuestion" runat="server" TextMode="MultiLine"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="Label6" runat="server" Font-Bold="True" Text="Security Answer :" /> </td> <td> <asp:TextBox ID="txtAnswer" runat="server" Font-Bold="True"></asp:TextBox>

PDF created with pdfFactory trial version www.pdffactory.com

Page 22: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

<br /> if you forget your password we will ask for the answer to your security Question </td> </tr> <tr> <td> <asp:Label ID="Label7" runat="server" Font-Bold="True" Text="User Name :" /> </td> <td> <asp:TextBox ID="txtUser" runat="server" Font-Bold="True"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="Label8" runat="server" Font-Bold="True" Text="Password :" /> </td> <td> <asp:TextBox ID="txtPass" runat="server" Font-Bold="True" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td colspan="2"> <asp:CheckBox ID="chbTerms" runat="server" Font-Bold="False" Text="I agree to Al Salam Company &lt;a href=&quot;terms.htm&quot;&gt;Terms of Service&lt;/a&gt;" /> </td> </tr> <tr> <td colspan="2"> <asp:Label ID="lblMsg" runat="server" Font-Bold="True"></asp:Label> </td> </tr> <tr> <td > &nbsp;</td> <td> <asp:Button ID="btnRegister" runat="server" Font-Bold="True" Text="Create Account" /> </td> </tr> </table> </div> </form> </body> </html>

PDF created with pdfFactory trial version www.pdffactory.com

Page 23: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Connection Objectكائن االتصال بقواعد البیانات : أوال

<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void btnRegister_Click(object sender, EventArgs e) { // Code Here } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <body> … <form id="form1" runat="server"> <asp:Button ID="btnRegister" runat="server" Font-Bold="True" Text="Create Account" onclick="btnRegister_Click" /> … </form> </body> </html>

1 <%@ Page Language="C#" %> 2 <%@ Import Namespace="System.Data.SqlClient"%> 3 <script runat="server"> 4 protected void btnRegister_Click(object sender, EventArgs e) 5 { 6 SqlConnection conn = new SqlConnection(); 7 conn.ConnectionString ="Data Source=.\\SQLEXPRESS;" 8 +"AttachDbFilename=|DataDirectory|\\AlSalam.mdf;" 9 +"Integrated Security=True;User Instance=True";

PDF created with pdfFactory trial version www.pdffactory.com

Page 24: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

10 conn.Open(); 11 lblMsg.Text = "Connection Successful"; 12 conn.Close(); 13 } 14 </script>

Command Objectقواعد البیانات علي وامراألتنفیذ كائن : ثانیا

SQLكیفیة تكوین جمل

PDF created with pdfFactory trial version www.pdffactory.com

Page 25: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

Registration

1 <%@ Page Language="C#" %> 2 <%@ Import Namespace="System.Data.SqlClient"%> 3 <script runat="server"> 4 protected void btnRegister_Click(object sender, EventArgs e) 5 { 6 string strInsert = 7 String.Format( 8 "Insert Into Member values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", 9 txtName.Text, rblGender.SelectedValue, ddlCountry.SelectedValue, 10 txtEmail.Text, txtQuestion.Text, txtAnswer.Text, txtUser.Text, txtPass.Text); 11 string strConn = "Data Source=.\\SQLEXPRESS;" 12 + "AttachDbFilename=|DataDirectory|\\AlSalam.mdf;" 13 + "Integrated Security=True;User Instance=True"; 14 SqlConnection conn = new SqlConnection(); 15 SqlCommand cmdInsert = new SqlCommand();

PDF created with pdfFactory trial version www.pdffactory.com

Page 26: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

16 conn.ConnectionString = strConn; 17 cmdInsert.Connection = conn; 18 cmdInsert.CommandText = strInsert; 19 if(chbTerms.Checked == false) 20 lblMsg.Text = "please check you agree to al salam company terms of services ."; 21 else 22 { conn.Open(); 23 cmdInsert.ExecuteNonQuery(); 24 conn.Close(); 25 lblMsg.Text = "your account has been successfully created."; } 26 } 27 </script> 28 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 29 <html>…</html>

1 <%@ Page Language="C#" %> 2 <%@ Import Namespace="System.Data.SqlClient"%> 3 <script runat="server"> 4 protected void btnRegister_Click(object sender, EventArgs e) 5 { 6 string strInsert = String.Format( 7 "Insert Into Member values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", 8 txtName.Text, rblGender.SelectedValue, ddlCountry.SelectedValue, 9 txtEmail.Text, txtQuestion.Text, txtAnswer.Text, txtUser.Text, txtPass.Text); 10 string strConn="Data Source=.\\SQLEXPRESS;" 11 + "AttachDbFilename=|DataDirectory|\\AlSalam.mdf;" 12 + "Integrated Security=True;User Instance=True"; 13 SqlConnection conn = new SqlConnection(); 14 SqlCommand cmdInsert = new SqlCommand(); 15 conn.ConnectionString = strConn; 16 cmdInsert.Connection = conn; 17 cmdInsert.CommandText = strInsert; 18 if(chbTerms.Checked==false) 19 lblMsg.Text = "please check you agree to al salam company terms of services ."; 20 else 21 { try 22 { 23 conn.Open(); 24 cmdInsert.ExecuteNonQuery(); 25 conn.Close();

PDF created with pdfFactory trial version www.pdffactory.com

Page 27: c#(3)-ملخص للغة c# من كتاب تطبيقات الويب المتقدمة بإستخدام asp.net  للمؤلف أ.د-عوض خليل و ا.عادل صبور_2

26 lblMsg.Text = "your account has been successfully created."; 27 } 28 catch (SqlException expSql) 29 { 30 if (expSql.Number == 2627) 31 lblMsg.Text = "username already exist Please enter a different user name."; 32 else 33 lblMsg.Text = "Sorry!! Database Error"; 34 } 35 catch 36 { 37 lblMsg.Text = "Your account was not created. Please try again."; 38 } 39 } 40 } 41 </script> 42 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 43 <html>…</html>

PDF created with pdfFactory trial version www.pdffactory.com