tag:blogger.com,1999:blog-85102908732861807372024-03-14T10:51:32.751+05:30.Net BasicThis blogs gives introduction to C#.NET programming for beginners. This blogs assumes that you have no programming experience whatsoever. It's a lot easier than you think, and can be a very rewarding hobby!after Refer this blogMohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.comBlogger39125tag:blogger.com,1999:blog-8510290873286180737.post-56155278766282782532012-04-04T16:29:00.000+05:302012-04-04T16:30:30.561+05:30Trigger Insert Update DeleteCreate TRIGGER trgTransactions on dbo.test1 AFTER INSERT, DELETE, UPDATE <br />AS <br />BEGIN <br /> DECLARE @DeletedId INT, @InsertId int <br /><br /> <br />SELECT @InsertId = Test from inserted <br />SELECT @DeletedId= Test from Deleted <br /> <br /> <br /><br /><br />IF (@InsertId > 0 AND isnull(@DeletedId,'') = '')<br /><br />BEGIN <br /> --DELETE FROM TomsWebDBNProdV2_clone_test.dbo.test1 where Test = @InsertId <br /> <br /> INSERT INTO TomsWebDBNProdV2_clone_test.dbo.test1 <br /> SELECT * FROM INSERTED <br /><br />END<br /><br /> -- insert<br /> IF (@InsertId > 0 AND @DeletedId > 0)<br />BEGIN <br /> DELETE FROM TomsWebDBNProdV2_clone_test.dbo.test1 where Test = @InsertId <br /> <br /> INSERT INTO TomsWebDBNProdV2_clone_test.dbo.test1 <br /> SELECT * FROM INSERTED <br /><br />END<br /><br /> -- update<br />IF( isnull(@InsertId,'') = '' AND @DeletedId > 0)<br /><br /> -- delete<br />DELETE FROM TomsWebDBNProdV2_clone_test.dbo.test1 where Test = @DeletedId<br /> <br /><br />ENDMohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-70736695202615329652011-05-20T17:54:00.001+05:302011-05-20T17:54:41.274+05:30Call C# Function in javascriptvar vCaller = '<%=txtSAP_TextChanged() %>';Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-68917182719745352222010-10-01T17:15:00.003+05:302010-10-01T17:20:09.343+05:30Get Maximum Numeric Value From Alpha Numeric Rowselect max(Convert(int,AlphaNumeric_Field))+1 from Table_Name where ISNUMERIC(AlphaNumeric_Field) = 1Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-27307195109682823052010-09-02T14:46:00.002+05:302010-09-02T14:49:47.489+05:30Search Text in All Columns in Sqlserver DatabaseCREATE PROC dbo.SearchAllTables<br />(<br />@SearchStr nvarchar(100)<br />)<br />AS<br />BEGIN<br />-- Copyright © 2010 Mohanraj Muthusamy. All rights reserved.<br />-- Purpose: To search all columns of all tables for a given search string<br />-- Written by: Mohanraj Muthusamy<br />-- Tested on: SQL Server 7.0 and SQL Server 2000 and SQL Server 2005<br />-- Date modified: 28th July 2010 22:50 GMT<br /><br />CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))<br />SET NOCOUNT ON<br />DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)<br />SET @TableName = ''<br />SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')<br />WHILE @TableName IS NOT NULL<br />BEGIN<br />SET @ColumnName = ''<br />SET @TableName =<br />(<br />SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))<br />FROM INFORMATION_SCHEMA.TABLES<br />WHERE TABLE_TYPE = 'BASE TABLE'<br />AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName<br />AND OBJECTPROPERTY(<br />OBJECT_ID(<br />QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)<br />), 'IsMSShipped'<br />) = 0<br />)<br />WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)<br />BEGIN<br />SET @ColumnName =<br />(<br />SELECT MIN(QUOTENAME(COLUMN_NAME))<br />FROM INFORMATION_SCHEMA.COLUMNS<br />WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)<br />AND TABLE_NAME = PARSENAME(@TableName, 1)<br />AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')<br />AND QUOTENAME(COLUMN_NAME) > @ColumnName<br />)<br />IF @ColumnName IS NOT NULL<br />BEGIN<br />INSERT INTO #Results<br />EXEC<br />(<br />'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)<br />FROM ' + @TableName + ' (NOLOCK) ' +<br />' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2<br />)<br />END<br />END<br />END<br />SELECT ColumnName, ColumnValue FROM #Results<br />ENDMohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-63331847031750751042010-05-12T14:55:00.000+05:302010-05-12T14:56:39.197+05:30Like operator for date in sqlservercreate PROCEDURE sp_Delete_RehabilitationDtls<br />(<br />@EmpId VARCHAR(50), @RehabDt datetime<br />)<br />AS<br />declare @Dtdate varchar(50)<br />select @Dtdate=replace((convert(varchar, @RehabDt,107) +'%'),',','')<br />delete<br />FROM Pro_Emp_Rehabilitation_Dts where Emp_Id=@EmpId and RehabilitationDate like @DtdateMohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-78395104613806035382010-05-07T19:30:00.001+05:302010-05-07T19:50:44.950+05:30Multiple rows in a single row with separator in SqlServerHere is a simple way to make a single row value from multiple rows with a separator in SqlServer. I have a table like the following…<br />Table Name: People<br />Column name :ID,Name<br /><br /><br />Now I need the ‘Name’ column values where matching the value of ‘aaaa’ in to a single row. For this I have tried to make a query. Just use the following query to get the solution. declare @res varchar(1000) select @res = coalesce(@res + ',', '') +Name from people WHERE Name = 'aaaa' select @resMohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-82767410870319074862010-05-07T17:15:00.001+05:302010-05-07T17:17:47.625+05:30How to join tables from different servers?EXEC sp_addlinkedserver SERVER_01<br />GO<br /><br />/* The following command links 'sa' login on SERVER_02 with the 'sa' login of SERVER_01*/<br /><br />EXEC sp_addlinkedsrvlogin @rmtsrvname = 'SERVER_01', @useself = 'false', @locallogin = 'sa', @rmtuser = 'sa', @rmtpassword = 'sa password of SERVER_01'<br /><br />GO<br /><br />SELECT a.title_idFROM SERVER_01.pubs.dbo.titles aINNER JOIN SERVER_02.pubs.dbo.titles bON a.title_id = b.title_idGOMohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-29813495373436940032009-06-26T01:27:00.002+05:302009-06-26T01:29:50.762+05:30DataGridView add Column and row Programatically in c#.netdataGridView1.Columns.Add(ColumnName , ColumnHeader Text);<br /><br /><br />DtView.Rows.Add(Number of Row);Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-37213251667999305992009-06-26T01:18:00.004+05:302009-06-26T01:23:47.454+05:30Call SELECT Stored Procedure in c#.net<span style="font-weight: bold;"> Main </span><br /><br /> String strStoreProcName = "Store Procedure Name";<br /> SqlCommand cmd = new SqlCommand(strStoreProcName);<br /> //Add number of Paramaters<br /> cmd.Parameters.AddWithValue("@Parameter Name in datebase", Parameter value);<br /> datatable = GetData(cmd);<br /><br />Function<br /><br /> public DataTable GetData(SqlCommand cmd)<br /> {<br /><br /> DataTable dt = new DataTable();<br /><br /> String strConnString= System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;<br /> <br /> <br /> SqlConnection con = new SqlConnection(strConnString); <br /> <br /> cmd.Connection = con ;<br /><br /> SqlDataAdapter sda = new SqlDataAdapter();<br /><br /> cmd.CommandType = CommandType.StoredProcedure;<br /><br /> try<br /> {<br /><br /> con.Open();<br /><br /> sda.SelectCommand = cmd;<br /><br /> sda.Fill(dt);<br /><br /> }<br /><br /> catch (Exception ex)<br /> {<br /><br /> return dt;<br /><br /> }<br /><br /> finally<br /> {<br /><br /> con.Close();<br /><br /> cmd.Dispose();<br /><br /> sda.Dispose();<br /><br /> }<br /><br /> return dt;<br /><br /> }Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-33326674974021814732009-06-26T01:13:00.004+05:302009-06-26T01:17:05.071+05:30Marquee in c#.net windows ApplicationNote:<br /> Make Use of timer control and write this below function in Timer control Ticker Events and also Set timer Enabled = true<br />private void TmrPPT_Tick(object sender, EventArgs e)<br /> {<br /><br /> string strScrollText= " Welcome to .Netbasic";<br /> if (!(intPosition <= strScrollText.Length))<br /> {<br /> intPosition = 0;<br /> }<br /> LblScrollingText.Text = strScrollText.Substring(intPosition);<br /> intPosition = intPosition + 1;<br /><br /> }Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com2tag:blogger.com,1999:blog-8510290873286180737.post-68594255768746446022009-06-26T00:50:00.002+05:302009-06-26T00:55:46.572+05:30Compare 2 different dates without seconds in sqlserver 2005select<br />(convert(varchar(10),DateTime1,103) + ' ' + convert(varchar(5),DateTime1,108))<br />,convert(varchar(10),DateTime2,103)+ ' '+ convert(varchar(5),DateTime2,108) from MIB_Time_Scheduling<br />where convert(varchar(10),DateTime1,103) = convert(varchar(10),DateTime1,103) and convert(varchar(5),DateTime2,108)=convert(varchar(5),DateTime2,108)Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-60304910037493730602009-06-23T01:39:00.003+05:302009-06-23T01:45:55.239+05:30Sending Email By using C#.netIn this coding we need to change "<a href="mailto:from@gmail.com">from@gmail.com</a>", "to@gmail.com" and "Password"..<br /><br />try<br />{<br />MailMessage mail = new MailMessage();<br />SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");<br />mail.From = new MailAddress("from@gmail.com");<br />mail.To.Add("to@gmail.com");<br />mail.Subject = "Test Mail";<br />mail.Body = "This is for testing SMTP mail from GMAIL";<br />SmtpServer.Port = 587;<br />SmtpServer.Credentials = new System.Net.NetworkCredential("From@gmail.com", "Password");<br />SmtpServer.EnableSsl = true;<br />// send mail to With Attachement<br />System.Net.Mail.Attachment attachment;<br />attachment = new System.Net.Mail.Attachment("c:\\DSC00081.JPG");<br />mail.Attachments.Add(attachment);<br />// Send mail to With HTML Page<br />mail.IsBodyHtml = true;<br />string htmlBody = "create html page";<br />mail.Body = htmlBody;<br />SmtpServer.Send(mail);<br />MessageBox.Show("mail Send");<br />}<br />catch (Exception ex)<br />{<br />MessageBox.Show(ex.ToString());<br />}Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-6847297177287839232009-06-18T23:54:00.002+05:302009-06-18T23:58:57.957+05:30Find and replace in Word DocumentNote: add a reference to Microsoft.Office.Interop.Word (version 12) and the Using statement below<br /><br />using Word = Microsoft.Office.Interop.Word;<br />public void FindAndReplaceTextInword(string Path, string FindText, string ReplaceText)<br /> {<br /> <br /> Word.Application word = new Word.Application();<br /> Word.Document doc = new Word.Document();<br /> // Define an object to pass to the API for missing parameters<br /> object missing = System.Type.Missing;<br /> try<br /> {<br /> // Everything that goes to the interop must be an object<br /> object fileName = Path;<br /><br /> // Open the Word document.<br /> // Pass the "missing" object defined above to all optional<br /> // parameters. All parameters must be of type object,<br /> // and passed by reference.<br /> doc = word.Documents.Open(ref fileName,<br /> ref missing, ref missing, ref missing, ref missing,<br /> ref missing, ref missing, ref missing, ref missing,<br /> ref missing, ref missing, ref missing, ref missing,<br /> ref missing, ref missing, ref missing);<br /><br /> // Activate the document<br /> doc.Activate();<br /><br /> // Loop through the StoryRanges (sections of the Word doc)<br /> foreach (Word.Range tmpRange in doc.StoryRanges)<br /> {<br /> // Set the text to find and replace<br /> tmpRange.Find.Text = FindText;<br /> tmpRange.Find.Replacement.Text = ReplaceText;<br /><br /><br /> // Set the Find.Wrap property to continue (so it doesn't<br /> // prompt the user or stop when it hits the end of<br /> // the section)<br /> tmpRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;<br /><br /> // Declare an object to pass as a parameter that sets<br /> // the Replace parameter to the "wdReplaceAll" enum<br /> object replaceAll = Word.WdReplace.wdReplaceAll;<br /><br /> // Execute the Find and Replace -- notice that the<br /> // 11th parameter is the "replaceAll" enum object<br /> tmpRange.Find.Execute(ref missing, ref missing, ref missing,<br /> ref missing, ref missing, ref missing, ref missing,<br /> ref missing, ref missing, ref missing, ref replaceAll,<br /> ref missing, ref missing, ref missing, ref missing);<br /> }<br /><br /> // Save the changes<br /> doc.Save();<br /><br /> // Close the doc and exit the app<br /> doc.Close(ref missing, ref missing, ref missing);<br /> word.Application.Quit(ref missing, ref missing, ref missing);<br /> }<br /> catch (Exception ex)<br /> {<br /> doc.Close(ref missing, ref missing, ref missing);<br /> word.Application.Quit(ref missing, ref missing, ref missing);<br /> }<br /> }Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-70370312091263942202008-07-08T13:33:00.000+05:302008-07-08T13:34:00.043+05:30events<div style="text-align: justify;"><span style="font-size:100%;"><b style="font-family: arial;">Event</b><span style="font-family: arial;"> - An action that an object does. When something happens, we say an event has happened. For example, when a button is clicked, we say it is the click( ) event. When a mouse hovers on an image, we say the mouseover( ) event has taken place.</span></span></div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-78923221980073808352008-07-08T13:28:00.001+05:302008-07-08T13:32:57.069+05:30property in C#.net<p style="font-family: arial; text-align: justify;" class="a"> <span style="font-size:100%;"><b>Property</b> - A property is a thing that describes the features of an object. A property is a piece of data contained within a class that has an exposed interface for reading/writing. Looking at this definition, we might think we could declare a public variable in a class and call it a property. While this assumption is somewhat valid, the real technical term for a public variable in a class is a field. The main difference between a field and a property is in the inclusion of an interface. <br /><br />We make use of Get and Set keywords while working with properties. We prefix the variables used within this code block with an underscore. Value is a keyword, that holds the value which is being retrieved or set.ss="a"> </span></p> <span style="font-family: arial;font-size:100%;" > Private _Color As String </span><span style="font-size:100%;"><br /></span><span style="font-family: arial;font-size:100%;" > Public Property Color() </span><span style="font-size:100%;"><br /></span><span style="font-family: arial;font-size:100%;" > Get </span><span style="font-size:100%;"><br /></span><span style="font-family: arial;font-size:100%;" > Return _Color</span><span style="font-size:100%;"><br /></span><span style="font-family: arial;font-size:100%;" > End Get </span><span style="font-size:100%;"><br /></span><span style="font-family: arial;font-size:100%;" > Set(ByVal Value) </span><span style="font-size:100%;"><br /></span><span style="font-family: arial;font-size:100%;" > _Color = Value </span><span style="font-size:100%;"><br /></span><span style="font-family: arial;font-size:100%;" > End Set </span><span style="font-size:100%;"><br /></span><span style="font-family: arial;font-size:100%;" > End Property </span>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-78883619848004999532008-07-08T13:27:00.001+05:302008-07-08T13:28:40.813+05:30Overriding and Shadowing<div style="text-align: justify; font-family: arial;"><span style="font-family: arial;font-size:100%;" >Both Overriding and Shadowing are ways to alter the behaviour of members of a base class. Shadowing is a VB.NET concept. In C#, this concept is called <b>Hiding</b>, though there is a difference between the two.</span><span style="font-size:100%;"><br /><br /></span><span style="font-family: arial;font-size:100%;" > When we do <b>shadowing</b>, we provide a new implementation to the base class member without overriding it. We may shadow a base class member in a derived class, by using the keyword <b>shadows</b>. The access level, return type, and the signature (means the datatypes of the arguments passed & the order of the types) of the derived class members which are shadowed, may differ from the base class. </span><span style="font-size:100%;"><br /><br /></span><span style="font-family: arial;font-size:100%;" > In C#, we may achieve shadowing using the keyword <b>new</b>. However, when Hiding in C#, the access level, the signature, return type of the derived class must be <b>same</b> as the base class. </span><span style="font-size:100%;"><br /><br /></span><span style="font-family: arial;font-size:100%;" ><b>Overriding</b> is the concept of providing a new implementation of derived class member as compared to its based class. In VB.NET, we do overriding using the <b>overrides</b> keyword, while in C#, overriding is achieved using the <b>override</b> keyword. For a class member to be overridable, we use the keyword <b>virtual</b> while defining it (in C#), and we use the keyword <b>overridable</b> (in VB.NET), though if we leave out specifying the overridable keyword, the member is overridable by default. </span> </div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-83043907747028399922008-07-08T13:23:00.000+05:302008-07-08T13:24:08.587+05:30ExecuteScalar and ExecuteNonQuery<div style="text-align: justify; font-family: arial;"><span style="font-size:100%;"><b>ExecuteScalar</b> - Returns only one value after execution of the query. It returns the first field in the first row. This is very light-weight and is perfect when all your query asks for is one item. This would be excellent for receiving a count of records (Select Count(*)) in an sql statement, or for any query where only one specific field in one column is required.<br /><br /><b>ExecuteNonQuery</b> - This method returns no data at all. It is used majorly with Inserts and Updates of tables. It is used for execution of DML commands.</span> </div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-56915216125571198472008-07-08T11:19:00.000+05:302008-07-08T12:46:08.587+05:30access specifiers<div style="text-align: justify; font-family: arial;"><span style="font-size:100%;">The main purpose of using access specifiers is to provide security to the applications. The availability (scope) of the member objects of a class may be controlled using access specifiers.<br /><br /></span><span style="font-size:100%;"><b>1. PUBLIC</b><br /></span><span style="font-size:100%;"> As the name specifies, it can be accessed from anywhere. If a member of a class is defined as public then it can be accessed anywhere in the class as well as outside the class. This means that objects can access and modify public fields, properties, methods.<br /><br /></span><span style="font-size:100%;"><b>2. PRIVATE</b><br /></span><span style="font-size:100%;"> As the name suggests, it can't be accessed outside the class. Its the private property of the class and can be accessed only by the members of the class. <br /><br /></span><span style="font-size:100%;"><b>3. FRIEND/INTERNAL </b><br /></span><span style="font-size:100%;"> Friend & Internal mean the same. Friend is used in VB.NET. Internal is used in C#. Friends can be accessed by all classes within an assembly but not from outside the assembly.<br /><br /></span><span style="font-size:100%;"><b>4. PROTECTED</b><br /></span><span style="font-size:100%;"> Protected variables can be used within the class as well as the classes that inherites this class. <br /><br /></span><span style="font-size:100%;"><b>5. PROTECTED FRIEND/PROTECTED INTERNAL</b><br /></span><span style="font-size:100%;"> The Protected Friend can be accessed by Members of the Assembly or the inheriting class, and ofcourse, within the class itself. <br /><br /></span><span style="font-size:100%;"><b>6. DEFAULT</b><br /></span><span style="font-size:100%;"> A Default property is a single property of a class that can be set as the default. This allows developers that use your class to work more easily with your default property because they do not need to make a direct reference to the property. Default properties cannot be initialized as Shared/Static or Private and all must be accepted at least on argument or parameter. Default properties do not promote good code readability, so use this option sparingly. </span> </div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-73287605271926457442008-07-08T11:17:00.000+05:302008-07-08T11:18:23.958+05:30DLL and EXE<div style="text-align: justify;"><span style="font-family: arial;font-size:100%;" >In .NET, an assembly may become a DLL or an EXE. Yet, there is a major underlying difference between the two.</span><br /><br /><span style="font-family: arial;font-size:100%;" >An EXE is an executable file, that may run on its own. Its independent. Where as a DLL is a Dynamic Link Library, that binds to an exe, or another DLL at runtime.</span><br /><br /><span style="font-family: arial;font-size:100%;" >A DLL has an exposed interface, through which members of the assembly may be accessed by those objects that require it.</span><br /><br /><span style="font-family: arial;font-size:100%;" >A DLL runs in tandem with the application space in memory, as the application references it. Whereas an EXE is independent, and runs as an independent process. </span></div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-48787032576529499722008-07-08T11:13:00.000+05:302008-07-08T11:15:17.569+05:30ADO and ADO.NET<div style="text-align: justify; font-family: arial;"><span style="font-size:100%;"> The old <b>ADO</b> (ActiveX Data Object) has evolved to ADO.NET in the .NET Framework. The ADO.NET object is a lightweight object. The ADO <b>Recordset</b> was a huge object in ADO. It provided the ability to support multiple types of cursors. It provided fast lightweight <b>"firehose"</b><br /><br /><b> ADO.NET</b> breaks the functionality of the ADO object to multiple classes, thereby allowing a focused approach to developing code. The ADO.NET <b>DataReader</b> is equivalent to the "firehose" cursor. The DataSet is a disconnected cache with tracking and control binding functionality. The <b>DataAdapter</b> provides the ability to completely customize how the central data store is updated with the changes to a DataSet. cursor and also supported a disconnected client-side cursor that supported tracking, optimistic locking, and automatic batch updates of a central database. However, all of this functionality was difficult to customize. </span></div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-32098212394857872612008-07-08T11:10:00.000+05:302008-07-08T11:13:07.997+05:30Acronyms in .NET<div style="text-align: justify; font-family: arial;"><span style="font-size:100%;"><b>ADO</b> - ActiveX Data Object - Microsoft ActiveX Data Objects (ADO) is a collection of Component Object Model objects for accessing different types of data sources.<br /><br /></span><span style="font-size:100%;"><b>AJAX</b> - Asynchronouse Javascript and XML - Ajax is a web development technology used for creating interactive web pages with fast data rendering by enabling partial postbacks on a web page (That means a section of the web page is rendered again, instead of the complete web page. This is achieved using Javascript, XML, JSON (Javascript Notation Language) and the XMLHttpRequest object in javascript.<br /><br /></span><span style="font-size:100%;"><b>ASP</b> - Active Server Pages - Microsoft's Server side script engine for creating dynamic web page.<br /><br /></span><span style="font-size:100%;"><b>C#</b> - C Sharp - Microsoft Visual C# is an object oriented programming language based on the .NET Framework. It includes features of powerful languages like C++, Java, Delphi and Visual Basic.<br /><br /></span><span style="font-size:100%;"><b>CAO</b> - Client Activated Object - Objects created on the server upon the client's request. This is used in Remoting.<br /><br /></span><span style="font-size:100%;"><b>CCW</b> - COM Callable Wrapper - This component is used when a .NET component needs to be used in COM.<br /><br /></span><span style="font-size:100%;"><b>CIL</b> - Common Intermediate Language - Its actually a low level human readable language implementation of CLI. All .NET-aware languages compile the source oode to an intermediate language called Common Intermediate Language using the language specific compiler.<br /><br /></span><span style="font-size:100%;"><b>CLI</b> - Common Language Infrastructure - This is a subset of CLR and base class libraries that Microsoft has submitted to ECMA so that a third-party vendor can build a .NET runtime on another platform.<br /><br /></span><span style="font-size:100%;"><b>CLR</b> - Common Language Runtime - It is the main runtime machine of the Microsoft .NET Framework. It includes the implementation of CLI. The CLR runs code in the form of bytes, called as bytecode and this is termed MSIL in .NET. <br /><br /></span><span style="font-size:100%;"><b>CLS</b> - Common Language Specification - A type that is CLS compliant, may be used across any .NET language. CLS is a set of language rules that defines language standards for a .NET language and types declared in it. While declaring a new type, if we make use of the [CLSCompliant] attribute, the type is forced to conform to the rules of CLS.<br /><br /></span><span style="font-size:100%;"><b>COFF</b> - Common Object File Format - It is a specification format for executables. <br /><br /></span><span style="font-size:100%;"><b>COM</b> - Component Object Model - reusable software components. The tribe of COM components includes COM+, Distributed COM (DCOM) and ActiveX® Controls.<br /><br /></span><span style="font-size:100%;"><b>CSC.exe</b> - C Sharp Compiler utility<br /><br /></span><span style="font-size:100%;"><b>CTS</b> - Common Type System - It is at the core of .NET Framework's cross-language integration, type safety, and high-performance code execution. It defines a common set of types that can be used with many different language syntaxes. Each language (C#, VB.NET, Managed C++, and so on) is free to define any syntax it wishes, but if that language is built on the CLR, it will use at least some of the types defined by the CTS.<br /><br /></span><span style="font-size:100%;"><b>DBMS</b> - Database Management System - a software application used for management of databases.<br /><br /></span><span style="font-size:100%;"><b>DISCO</b> - Discovery of Web Services. A Web Service has one or more. DISCO files that contain information on how to access its WSDL. <br /><br /></span><span style="font-size:100%;"><b>DLL</b> - Dynamic Link Library - a shared reusable library, that exposes an interface of usable methods within it.<br /><br /></span><span style="font-size:100%;"><b>DOM</b> - Document Object Model - is a language independent technology that permits scripts to dynamically updated contents of a document (a web page is also a document).<br /><br /></span><span style="font-size:100%;"><b>ECMA</b> - European Computer Manufacturer's Association - Is an internation organisation for computer standards.<br /><br /></span><span style="font-size:100%;"><b>GC</b> - Garbage Collector - an automatic memory management system through which objects that are not referenced are cleared up from the memory.<br /><br /></span><span style="font-size:100%;"><b>GDI</b> - Graphical Device Interface - is a component in Windows based systems, that performs the activity of representing graphical objects and outputting them to output devices.<br /><br /></span><span style="font-size:100%;"><b>GAC</b> - Global Assembly Cache - Is a central repository of reusable libraries in the .NET environment. <br /><br /></span><span style="font-size:100%;"><b>GUI</b> - Graphic User Interface - a type of computer interface through which user's may interact with the Computer using different types of input & output devices with a graphical interface.<br /><br /></span><span style="font-size:100%;"><b>GUID</b> - Globally Unique Identifier - is a unique reference number used in applications to refer an object.<br /><br /></span><span style="font-size:100%;"><b>HTTP</b> - Hyper Text Transfer Protocol - is a communication protocol used to transfer information in the internet. HTTP is a request-response protocol between servers and clients.<br /><br /></span><span style="font-size:100%;"><b>IDE</b> - Integrated Development Environment - is a development environment with source code editor with a compiler(or interpretor), debugging tools, designer, solution explorer, property window, object explorer etc.<br /><br /></span><span style="font-size:100%;"><b>IDL</b> - Interface Definition Language - is a language for defining software components interface.<br /><br /></span><span style="font-size:100%;"><b>ILDASM</b> - Intermediate Language Disassembler - The contents of an assembly may be viewed using the ILDASM utility, that comes with the .NET SDK or the Visual Studio.NET. The ildasm.exe tool may also be used in the command line compiler.<br /><br /></span><span style="font-size:100%;"><b>IIS</b> - Internet Information Server - Is a server that provides services to websites and even hosts websites. <br /><br /></span><span style="font-size:100%;"><b>IL</b> - Intermediate Language - is the compiled form of the .NET language source code. When .NET source code is compiled by the language specific compiler (say we compile C# code using csc.exe), it is compiled to a .NET binary, which is platform independent, and is called Intermediate Language code. The .NET binary also comprises of metadata.<br /><br /></span><span style="font-size:100%;"><b>JIT</b> - Just in Time (Jitter) - is a technology for boosting the runtime performance of a system. It converts during runtime, code from one format into another, just like IL into native machine code. Note that JIT compilation is processor specific. Say a processor is X86 based, then the JIT compilation will be for this type of processor.<br /><br /></span><span style="font-size:100%;"><b>MBR</b> - MarshallByReference - The caller recieves a proxy to the remote object.<br /><br /></span><span style="font-size:100%;"><b>MBV</b> - MarshallByValue - The caller recieves a copy of the object in its own application domain.<br /><br /></span><span style="font-size:100%;"><b>MDI</b> - Multiple Document Interface - A window that resides under a single parent window. <br /><br /></span><span style="font-size:100%;"><b>MSIL</b> - Microsoft Intermediate Language - now called CIL.<br /><br /></span><span style="font-size:100%;"><b>Orcas</b> - Codename for Visual Studio 2008<br /><br /></span><span style="font-size:100%;"><b>PE</b> - Portable Executable - an exe format file that is portable.<br /><br /></span><span style="font-size:100%;"><b>RAD</b> - Rapid Application Development <br /><br /></span><span style="font-size:100%;"><b>RCW</b> - Runtime Callable Wrapper - This component is used when a .NET needs to use a COM component.<br /><br /></span><span style="font-size:100%;"><b>SAX</b> - Simple API for XML - It is a serial access parser API for XML. The parser is event driven and the event gets triggered when an XML feature is encountered.<br /><br /></span><span style="font-size:100%;"><b>SDK</b> - Software Development Kit<br /><br /></span><span style="font-size:100%;"><b>SMTP</b> - Simple Mail Transfer Protocol - a text based protocol for sending mails.<br /><br /></span><span style="font-size:100%;"><b>SN.exe</b> - Strong Name Utility - a tool to make strong named assemblies.<br /><br /></span><span style="font-size:100%;"><b>SQL</b> - Structured Query Language - a language for management of data in a relational structure.<br /><br /></span><span style="font-size:100%;"><b>SOAP</b> - Simple Object Access Protocol - a protocol used for exchange of xml based messages across networks.<br /><br /></span><span style="font-size:100%;"><b>TCP</b> - Transmission Control Protocol - data exchange protocol across networks using streamed sockets.<br /><br /></span><span style="font-size:100%;"><b>UI</b> - User Interface <br /><br /></span><span style="font-size:100%;"><b>URI</b> - Uniform Resource Identifier<br /><br /></span><span style="font-size:100%;"><b>URL</b> - Uniform Resource Locator <br /><br /></span><span style="font-size:100%;"><b>UDDI</b> - Universal Description, Discovery and Integration - it is a platform independent business registration across the internet.<br /><br /></span><span style="font-size:100%;"><b>WAP</b> - Wireless Access Protocol - a protocol that enables access to the internet from mobile phones and PDAs.<br /><br /></span><span style="font-size:100%;"><b>WC</b> - Windows Cardspace - Part of .NET 3.0 framework, that enables users to secure and store digital identities of a person, and a provision to a unified interface for choosing the identity for a particular transaction, like logging in to a website.<br /><br /></span><span style="font-size:100%;"><b>WCF</b> - Windows Communication Foundation - Part of .NET 3.0 framework, that enables communication between applications across machines.<br /><br /></span><span style="font-size:100%;"><b>WF</b> - Windows Workflow Foundation - Part of .NET 3.0 framework, used for defining, execution and management of reusable workflows.<br /><br /></span><span style="font-size:100%;"><b>WKO</b> - Well Known Object - These are MBR types whose lifetime is controlled by the server's application domain.<br /><br /></span><span style="font-size:100%;"><b>WPF</b> - Windows Presentation Foundation - Part of .NET 3.0 framework, is the graphical subsystem of the .NET 3.0 framework.<br /><br /></span><span style="font-size:100%;"><b>WSDL</b> - Web Services Description Language - is an XML based language for describing web services.<br /><br /></span><span style="font-size:100%;"><b>WML</b> - Wireless Markup Language - is a content format for those devices that use Wireless Application Protocol.<br /><br /></span><span style="font-size:100%;"><b>VB.NET</b> - Visual Basic .NET - .NET based language. Its the .NET implementation of VB6, the most widely used language in the world.<br /><br /></span><span style="font-size:100%;"><b>VBC.exe</b> - VB.NET Compiler<br /><br /></span><span style="font-size:100%;"><b>VES</b> - Virtual Execution System - It provides the environment for execution of managed code. It provides direct support for a set of built in data types, defines a hypothetical machine with an associated machine model and state, a set of control flow constructs, and an exception handling model. To a large extent, the purpose of the VES is to provide the support required to execute the Common Intermediate Language instruction set. <br /><br /></span><span style="font-size:100%;"><b>VS</b> - Visual Studio<br /><br /></span><span style="font-size:100%;"><b>VSS</b> - Visual Source Safe - An IDE by Microsoft, to maintain source code versions and security.<br /><br /></span><span style="font-size:100%;"><b>VSTS</b> - Visual Studio Team Suite - Visual Studio Team System - it is an extended version of Visual Studio .NET. It has a set of collaboration and development tools for software development process. <br /><br /></span><span style="font-size:100%;"><b>XML</b> - Extensible Markup Language - is a general purpose well formed markup language. </span> </div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-14858997376945585802008-07-04T11:49:00.002+05:302008-07-04T12:00:28.282+05:30Passing Value-Type Parameters<div style="text-align: justify; font-family: arial;"><span style="font-size:100%;">A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Therefore, passing a value-type variable to a method means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the variable. If you want the called method to change the value of the parameter, you have to pass it by reference, using the ref or out keyword. For simplicity, the following examples use ref.<br /><br /></span><div style="text-align: justify;"><span style="font-size:100%;"><span style="font-weight: bold;">Example </span><a name="exampleToggle"><!----></a></span> </div><p style="text-align: justify;"><span style="font-size:100%;">The following example demonstrates passing value-type parameters by value. The variable <span class="code">n</span> is passed by value to the method <span class="code">SquareIt</span>. Any changes that take place inside the method have no affect on the original value of the variable.</span></p><pre class="libCScode" id="ctl00_rs1_mainContentContainer_ctl12CSharp" space="preserve"><span style="color: blue;">class</span> PassingValByVal<br />{<br /> <span style="color: blue;">static</span> <span style="color: blue;">void</span> SquareIt(<span style="color: blue;">int</span> x)<br /> <span style="color: green;">// The parameter x is passed by value.</span><br /> <span style="color: green;">// Changes to x will not affect the original value of x.</span><br /> {<br /> x *= x;<br /> System.Console.WriteLine(<span style="color: maroon;"><span style="color: maroon;"></span></span>x);<br /> }<br /> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main()<br /> {<br /> <span style="color: blue;">int</span> n = 5;<br /> System.Console.WriteLine(<span style="color: maroon;"><span style="color: maroon;"></span></span>n);<br /><br /> SquareIt(n); // Passing the variable by value.<br /> System.Console.WriteLine(<span style="color: maroon;"><span style="color: maroon;"></span></span>n);<br /><br /> <span style="color: green;">// Keep the console window open in debug mode.</span><br /> System.Console.WriteLine(<span style="color: maroon;"><span style="color: maroon;">"Press any key to exit."</span></span>);<br /> System.Console.ReadKey();<br /> }<br />}<br /><span style="color: green;">/* Output:<br /> The value before calling the method: 5<br /> The value inside the method: 25<br /> The value after calling the method: 5<br />*/</span><br /><br /><br /></pre><p style="text-align: justify;"><span style="font-size:100%;">The variable <span class="code">n</span>, being a value type, contains its data, the value <span class="code">5</span>. When <span class="code">SquareIt</span> is invoked, the contents of <span class="code">n</span> are copied into the parameter <span class="code">x</span>, which is squared inside the method. In <span class="code">Main</span>, however, the value of <span class="code">n</span> is the same, before and after calling the <span class="code">SquareIt</span> method. In fact, the change that takes place inside the method only affects the local variable <span class="code">x</span>.</span></p><div> </div><p style="text-align: justify;"><span style="font-size:100%;">The following example is the same as the previous example, except for passing the parameter using the <span class="keyword">ref</span> keyword. The value of the parameter is changed after calling the method.</span></p><div style="text-align: justify;"><span style="font-size:100%;"><span style="color: blue;">class</span> PassingValByRef</span><br /><span style="font-size:100%;">{</span><br /><span style="font-size:100%;"> <span style="color: blue;">static</span> <span style="color: blue;">void</span> SquareIt(ref <span style="color: blue;">int</span> x)</span><br /><span style="font-size:100%;"> <span style="color: green;">// The parameter x is passed by reference.</span></span><br /><span style="font-size:100%;"> <span style="color: green;">// Changes to x will affect the original value of x.</span></span><br /><span style="font-size:100%;"> {</span><br /><span style="font-size:100%;"> x *= x;</span><br /><span style="font-size:100%;"> System.Console.WriteLine(<span style="color: maroon;"><span style="color: maroon;"></span></span>x);</span><br /><span style="font-size:100%;"> }</span><br /><span style="font-size:100%;"> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main()</span><br /><span style="font-size:100%;"> {</span><br /><span style="font-size:100%;"> <span style="color: blue;">int</span> n = 5;</span><br /><span style="font-size:100%;"> System.Console.WriteLine(n);</span><br /><br /><span style="font-size:100%;"> SquareIt(ref n); // Passing the variable by reference.</span><br /><span style="font-size:100%;"> System.Console.WriteLine(<span style="color: maroon;"><span style="color: maroon;"></span></span>n);</span><br /><br /><span style="font-size:100%;"> <span style="color: green;">// Keep the console window open in debug mode.</span></span><br /><span style="font-size:100%;"> System.Console.WriteLine(<span style="color: maroon;"><span style="color: maroon;">"Press any key to exit."</span></span>);</span><br /><span style="font-size:100%;"> System.Console.ReadKey();</span><br /><span style="font-size:100%;"> }</span><br /><span style="font-size:100%;">}</span><br /><span style="font-size:100%;"><span style="color: green;">/* Output:</span></span><br /><span style="font-size:100%;"><span style="color: green;"> The value before calling the method: 5</span></span><br /><span style="font-size:100%;"><span style="color: green;"> The value inside the method: 25</span></span><br /><span style="font-size:100%;"><span style="color: green;"> The value after calling the method: 25</span></span><br /><span style="font-size:100%;"><span style="color: green;">*/</span></span><br /></div><div style="text-align: justify;"><span style="font-size:100%;">In this example, it is not the value of <span class="code">n</span> that is passed; rather, a reference to <span class="code">n</span> is passed. The parameter <span class="code">x</span> is not an int; it is a reference to an <span class="keyword">int</span>, in this case, a reference to <span class="code">n</span>. Therefore, when <span class="code">x</span><span class="code">x</span> refers to: <span class="code">n</span>. is squared inside the method, what actually gets squared is what </span></div><pre class="libCScode" id="ctl00_rs1_mainContentContainer_ctl12CSharp" space="preserve"><br /></pre><br /></div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-61836064079758786092008-06-25T17:14:00.002+05:302008-06-25T17:57:13.884+05:30Strings in .NET and C#<h1 style="text-align: justify;font-family:arial;"><span style="font-size:100%;">Strings in .NET and C#</span></h1><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> The <code>System.String</code> type (shorthand <code>string</code> in C#) is one of the most important types in .NET, and unfortunately it's much misunderstood. This article attempts to deal with some of the basics of the type. </span></p><div style="text-align: justify; font-family: arial;"> </div><h2 style="text-align: justify;font-family:arial;"><span style="font-size:100%;">What is a string?</span></h2><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> A string is basically a sequence of characters. Each character is a <a href="http://www.unicode.org/">Unicode</a> character in the range U+0000 to U+FFFF (more on that <a href="http://www.yoda.arachsys.com/csharp/strings.html#encoding">later</a>). The string type (I'll use the C# shorthand rather than putting <code>System.String</code> each time) has the following characteristics: </span></p><div style="text-align: justify; font-family: arial;"> </div><dl style="text-align: justify;font-family:arial;"><dt><span style="font-size:100%;">It is a reference type</span></dt><dd><span style="font-size:100%;"> It's a common misconception that string is a value type. That's because its immutability (see next point) makes it act sort of like a value type. It actually acts like a normal reference type. See my articles on <a href="http://www.yoda.arachsys.com/csharp/parameters.html">parameter passing</a> and <a href="http://www.yoda.arachsys.com/csharp/memory.html">memory</a> for more details of the differences between value types and reference types. </span></dd><dt><span style="font-size:100%;">It's immutable</span></dt><dd><span style="font-size:100%;"> You can never actually change the contents of a string, at least with safe code which doesn't use reflection. Because of this, you often end up changing the value of a string <i>variable</i>. For instance, the code <i>s = s.Replace ("foo", "bar");</i> doesn't change the contents of the string that <code>s</code> originally referred to - it just sets the value of <code>s</code> to a new string, which is a copy of the old string but with "foo" replaced by "bar". </span></dd><dt><span style="font-size:100%;">It can contain nulls</span></dt><dd><span style="font-size:100%;"> C programmers are used to strings being sequences of characters ending in '\0', the nul or null character. (I'll use "null" because that's what the Unicode code chart calls it in the detail; don't get it confused with the <code>null</code> keyword in C# - <code>char</code> is a value type, so can't be a null reference!) In .NET, strings can contain null characters with no problems at all as far as the string methods themselves are concerned. However, other classes (for instance many of the Windows Forms ones) may well think that the string finishes at the first null character - if your string ever appears to be truncated oddly, that could be the problem. </span></dd><dt><span style="font-size:100%;">It overloads the <code>==</code> operator</span></dt><dd><span style="font-size:100%;"> When the <code>==</code> operator is used to compare two strings, the <code>Equals</code> method is called, which checks for the equality of the contents of the strings rather than the references themselves. For instance, <code>"hello".Substring(0, 4)=="hell"</code> is true, even though the references on the two sides of the operator are different (they refer to two different string objects, which both contain the same character sequence). Note that operator overloading only works here if both sides of the operator are string expressions at compile time - operators aren't applied polymorphically. If either side of the operator is of type <code>object</code> as far as the compiler is concerned, the normal <code>==</code> operator will be applied, and simple reference equality will be tested. </span></dd></dl><div style="text-align: justify; font-family: arial;"> </div><h2 style="text-align: justify;font-family:arial;"><span style="font-size:100%;"><a name="interning">Interning</a></span></h2><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> .NET has the concept of an "intern pool". It's basically just a set of strings, but it makes sure that every time you reference the same string <a href="http://www.yoda.arachsys.com/csharp/strings.html#literals">literal</a>, you get a reference to the same string. This is probably language-dependent, but it's certainly true in C# and VB.NET, and I'd be very surprised to see a language it didn't hold for, as IL makes it very easy to do (probably easier than failing to intern literals). As well as literals being automatically interned, you can intern strings manually with the <code>Intern</code> method, and check whether or not there is already an interned string with the same character sequence in the pool using the <code>IsInterned</code> method. This somewhat unintuitively returns a string rather than a boolean - if an equal string is in the pool, a reference to that string is returned. Otherwise, <code>null</code> is returned. Likewise, the <code>Intern</code> method returns a reference to an interned string - either the string you passed in if was already in the pool, or a newly created interned string, or an equal string which was already in the pool. </span></p><div style="text-align: justify; font-family: arial;"> </div><h2 style="text-align: justify;font-family:arial;"><span style="font-size:100%;"><a name="literals">Literals</a></span></h2><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> Literals are how you hard-code strings into C# programs. There are two types of string literals in C# - regular string literals and verbatim string literals. Regular string literals are similar to those in many other languages such as Java and C - they start and end with <code>"</code>, and various characters (in particular, <code>"</code> itself, <code>\</code>, and carriage return (CR) and line feed (LF)) need to be "escaped" to be represented in the string. Verbatim string literals allow pretty much anything within them, and end at the first <code>"</code> which isn't doubled. Even carriage returns and line feeds can appear in the literal! To obtain a <code>"</code> within the string itself, you need to write <code>""</code>. Verbatim string literals are distinguished by having an <code>@</code> before the opening quote. Here are some examples of the two types of literal, and what they amount to: </span></p><div style="text-align: justify; font-family: arial;"> </div><table style="text-align: left; margin-left: 0px; margin-right: 0px;font-family:arial;" border="1"> <tbody><tr> <th><span style="font-size:100%;">Regular literal</span></th> <th><span style="font-size:100%;">Verbatim literal</span></th> <th><span style="font-size:100%;">Resulting string</span></th> </tr> <tr> <td><span style="font-size:100%;"><code>"Hello"</code></span></td> <td><span style="font-size:100%;"><code>@"Hello"</code></span></td> <td><span style="font-size:100%;"><code>Hello</code></span></td> </tr> <tr> <td><span style="font-size:100%;"><code>"Backslash: \\"</code></span></td> <td><span style="font-size:100%;"><code>@"Backslash: \"</code></span></td> <td><span style="font-size:100%;"><code>Backslash: \</code></span></td> </tr> <tr> <td><span style="font-size:100%;"><code>"Quote: \""</code></span></td> <td><span style="font-size:100%;"><code>@"Quote: """</code></span></td> <td><span style="font-size:100%;"><code>Quote: "</code></span></td> </tr> <tr> <td><span style="font-size:100%;"><code>"CRLF:\r\nPost CRLF"</code></span></td> <td><span style="font-size:100%;"><code>@"CRLF:</code><br /><code>Post CRLF"</code></span></td> <td><span style="font-size:100%;"><code>CRLF:</code><br /><code>Post CRLF</code></span></td> </tr> </tbody></table><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> For other escape sequences, please see the <a href="http://www.yoda.arachsys.com/csharp/faq/#escapes">relevant FAQ entry</a>. Note that the difference is only for the compiler's sake. Once the string is in the compiled code, there's no such thing as a verbatim string literal vs a regular string literal. </span></p><div style="text-align: justify; font-family: arial;"> </div><h2 style="text-align: justify;font-family:arial;"><span style="font-size:100%;"><a name="debugger">Strings and the debugger</a></span></h2><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> Numerous people run into problems when inspecting strings in the debugger, both with VS.NET 2002 and VS.NET 2003. Ironically, the problems are often generated by the debugger trying to be helpful, and either displaying the string as a regular string literal with backslash-escaped characters in, or displaying it as a verbatim string literal complete with leading <code>@</code>. This leads to many questions asking how the <code>@</code> can be removed, despite the fact that it's not really there in the first place - it's only how the debugger's showing it. Also, some versions of VS.NET will stop displaying the contents of the string at the first null character, <i>and</i> evaluate its Length property incorrectly, calculating the value itself instead of asking the managed code. Again, it then considers the string to finish at the first null character. </span></p><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> Given the confusion this has caused, I believe it's best to examine strings in a different way when debugging, at least if you think something odd is going on. I suggest using a method like the one below, which will print the contents of a string to the console in a safe way. Depending on what kind of application you're developing, you may want to write this information to a log file, to the debug or trace listeners, or pop it up in a message box. </span></p><div style="text-align: justify; font-family: arial;"> </div><table style="text-align: left; margin-left: 0px; margin-right: 0px;font-family:arial;" class="code"><tbody><tr><td><pre><span style="font-size:100%;"><span class="Modifier">static</span> <span class="Modifier">readonly</span> <span class="ReferenceType">string</span>[] LowNames =<br />{<br /><span class="String">"NUL"</span>, <span class="String">"SOH"</span>, <span class="String">"STX"</span>, <span class="String">"ETX"</span>, <span class="String">"EOT"</span>, <span class="String">"ENQ"</span>, <span class="String">"ACK"</span>, <span class="String">"BEL"</span>,<br /><span class="String">"BS"</span>, <span class="String">"HT"</span>, <span class="String">"LF"</span>, <span class="String">"VT"</span>, <span class="String">"FF"</span>, <span class="String">"CR"</span>, <span class="String">"SO"</span>, <span class="String">"SI"</span>,<br /><span class="String">"DLE"</span>, <span class="String">"DC1"</span>, <span class="String">"DC2"</span>, <span class="String">"DC3"</span>, <span class="String">"DC4"</span>, <span class="String">"NAK"</span>, <span class="String">"SYN"</span>, <span class="String">"ETB"</span>,<br /><span class="String">"CAN"</span>, <span class="String">"EM"</span>, <span class="String">"SUB"</span>, <span class="String">"ESC"</span>, <span class="String">"FS"</span>, <span class="String">"GS"</span>, <span class="String">"RS"</span>, <span class="String">"US"</span><br />};<br /><span class="Modifier">public</span> <span class="Modifier">static</span> <span class="ValueType">void</span> DisplayString (<span class="ReferenceType">string</span> text)<br />{<br />Console.WriteLine (<span class="String">"String length: {0}"</span>, text.Length);<br /><span class="Statement">foreach</span> (<span class="ValueType">char</span> c <span class="Statement">in</span> text)<br />{<br /> <span class="Statement">if</span> (c < class="String">"<{0}> U+{1:x4}"</span>, LowNames[c],<br /> (<span class="ValueType">int</span>)c);<br /> }<br /> <span class="Statement">else</span> <span class="Statement">if</span> (c > 127)<br /> {<br /> Console.WriteLine (<span class="String">"(Possibly non-printable) U+{0:x4}"</span>,<br /> (<span class="ValueType">int</span>)c);<br /> }<br /> <span class="Statement">else</span><br /> {<br /> Console.WriteLine (<span class="String">"{0} U+{1:x4}"</span>, c,<br /> (<span class="ValueType">int</span>)c);<br /> }<br />}<br />}<br /></pre></td></tr></tbody></table><div style="text-align: justify; font-family: arial;"> </div><h2 style="text-align: justify;font-family:arial;"><span style="font-size:100%;"><a name="memory">Memory usage</a></span></h2><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> In the current implementation at least, strings take up 20+(n/2)*4 bytes (rounding the value of n/2 down), where n is the number of characters in the string. The string type is unusual in that the size of the object itself varies. The only other classes which do this (as far as I know) are arrays. Essentially, a string <i>is</i> a character array in memory, plus the length of the array and the length of the string (in characters). The length of the array isn't always the same as the length in characters, as strings can be "over-allocated" within mscorlib.dll, to make building them up easier. (<code>StringBuilder</code> does this, for instance.) While strings are immutable to the outside world, code within mscorlib can change the contents, so <code>StringBuilder</code> creates a string with a larger internal character array than the current contents requires, then appends to that string until the character array is no longer big enough to cope, at which point it creates a new string with a larger array. The string length member also contains a flag in its top bit to say whether or not the string contains any non-ASCII characters. This allows for extra optimisation in some cases. </span></p><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> Although strings aren't null-terminated as far as the API is concerned, the character array is null-terminated, as this means it can be passed directly to unmanaged functions without any copying being involved, assuming the inter-op specifies that the string should be marshalled as Unicode. </span></p><div style="text-align: justify; font-family: arial;"> </div><h2 style="text-align: justify;font-family:arial;"><span style="font-size:100%;"><a name="encoding">Encoding</a></span></h2><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> (If you don't know about character encodings and Unicode, please read my <a href="http://www.yoda.arachsys.com/csharp/unicode.html">article on the subject</a></span> first.) </p><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> As stated at the start of the article, strings are always in Unicode encoding. The idea of "a Big-5 string" or "a string in UTF-8 encoding" is a mistake (as far as .NET is concerned) and usually indicates a lack of understanding of either encodings or the way .NET handles strings. It's very important to understand this - treating a string as if it represented some valid text in a non-Unicode encoding is almost always a mistake. </span></p><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> Now, the Unicode coded character set (one of the flaws of Unicode is that the one term is used for various things, including a coded character set and a character encoding scheme) contains more than 65536 characters. This means that a single <code>char</code> (<code>System.Char</code>) cannot cover every character. This leads to the use of <i>surrogates</i> where characters above U+FFFF are represented in strings as two characters. Essentially, <code>string</code> uses the UTF-16 character encoding form. Most developers may well not need to know much about this, but it's worth at least being aware of it. </span></p><div style="text-align: justify; font-family: arial;"> </div><h2 style="text-align: justify;font-family:arial;"><span style="font-size:100%;"><a name="culture">Culture and internationalization oddities</a></span></h2><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> Some of the oddities of Unicode lead to oddities in string and character handling. Many of the string methods are <i>culture-sensitive</i> - in other words, what they do depends on the culture of the current thread. For example, what would you expect <code>"i".toUpper()</code> to return? Most people would say <code>"I"</code>, but in Turkish the correct answer is <code>"İ"</code> (Unicode U+0130, "Latin capital I with dot above"). To perform a culture-insensitive case change, you can use <code>CultureInfo.InvariantCulture</code>, and pass that to the overload of <code>String.ToUpper</code> which takes a <code>CultureInfo</code>. </span></p><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> There are further oddities when it comes to comparing, sorting, and finding the index of a substring. Some of these are culture-specific, and some aren't. For instance, in all cultures (as far as I can see), <code>"lassen"</code> and <code>"la\u00dfen"</code> (a "sharp S" or eszett being the Unicode-escaped character in there) are considered equal when <code>CompareTo</code> or <code>Compare</code> are used, but not when <code>Equals</code> is used. <code>IndexOf</code> will treat the eszett as the same as <code>"ss"</code>, unless you use a <code>CompareInfo.IndexOf</code> and specify <code>CompareOptions.Ordinal</code> as the options to use. </span></p><div style="text-align: justify; font-family: arial;"> </div><p style="text-align: justify;font-family:arial;"><span style="font-size:100%;"> Some other unicode character appear to be completely invisible to the normal <code>IndexOf</code>. Someone asked in the C# newsgroup why a search/replace method was going into an infinite loop. It was repeatedly using <code>Replace</code> to replace all double spaces with a single space, and checking whether or not it had finished by using <code>IndexOf</code>, so that multiple spaces would collapse to a single space. Unfortunately, this was failing due to a "strange" character in the original string between two spaces. <code>IndexOf</code> matched the double space, ignoring the extra character, but <code>Replace</code> didn't. I don't know which exact character was in the real data, but it can be easily reproduced using U+200C which is a zero-width non-joiner character (whatever that means, exactly!). Put one of those in the middle of the text you're searching in, and <code>IndexOf</code> will ignore it, but <code>Replace</code> won't. Again, to make the two methods behave the same, you can use <code>CompareInfo.IndexOf</code> and pass in <code>CompareOptions.Ordinal</code>. My guess is that there's a lot of code which would fail on "awkward" data like this. (I wouldn't for a moment claim that all my code is immune, either.) </span></p>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-68746355489402379992008-06-23T15:46:00.002+05:302008-06-23T15:51:35.471+05:30MySQL - SQL INSERT Stored Procedure<div style="text-align: justify; font-family: arial;"><span style="font-size:100%;"><span style="font-weight: bold;">MySQL Stored Procedure - INSERT - Example</span><br /><br />Source code to create and add "sql insert stored procedure" to catalog<br /><br />The following example is for creating a simple insert stored procedure. You can run it through an explicit call from a host language program or directly from a DBMS query execution shell like dbOrchestra.<br /><br />DROP PROCEDURE IF EXISTS `sp_students_INSERT_byPK`<br />GO<br /><br />CREATE PROCEDURE sp_students_INSERT_byPK<br /> (<br /> IN p_student_id INT(11) ,<br /> IN p_password VARCHAR(15) ,<br /> IN p_active_flg TINYINT(4) ,<br /> IN p_lastname VARCHAR(30) ,<br /> IN p_firstname VARCHAR(20) ,<br /> IN p_gender_code VARCHAR(1) ,<br /> IN p_is_on_staff TINYINT(4) ,<br /> IN p_birth_dttm DATETIME <br /> )<br />BEGIN<br /><br /> INSERT INTO students<br /> (<br /> student_id ,<br /> password ,<br /> active_flg ,<br /> lastname ,<br /> firstname ,<br /> gender_code ,<br /> is_on_staff ,<br /> birth_dttm <br /> )<br /> VALUES<br /> (<br /> p_student_id ,<br /> p_password ,<br /> p_active_flg ,<br /> p_lastname ,<br /> p_firstname ,<br /> p_gender_code ,<br /> p_is_on_staff ,<br /> p_birth_dttm <br /> ) ;<br />END<br /><br />GO<br /><br />You will note that just like in a INSERT statement you do not have to use all of the columns available when creating a stored procedure. You must however populate all columnar data associated with the PK (primary key), and columns associated with unique indexes (note: there are exceptions to this, but they will not addressed here), and columns defined in the ddl as "NOT NULL".<br /><br /><span style="font-weight: bold;">Executing the sql insert stored procedure</span><br /><span style="font-weight: bold;">Execute insert stored procedure</span><br /><br />To run the insert stored procedure you need to supply a value to the student_id variable as well as populate all required data. In this example I have included a cross section of columns for your reference, as well as the datatype associated with the columns in our SQL INSERT.<br /><br />/***<br /> IN p_student_id INT(11)<br /> IN p_password VARCHAR(15)<br /> IN p_active_flg TINYINT(4)<br /> IN p_lastname VARCHAR(30)<br /> IN p_firstname VARCHAR(20)<br /> IN p_gender_code VARCHAR(1)<br /> IN p_is_on_staff TINYINT(4)<br /> IN p_birth_dttm DATETIME<br />***/<br /><br />CALL sp_students_INSERT_byPK<br /> (<br /> 25 ,<br /> 'mydogSpot1' ,<br /> 1 ,<br /> 'Bag' ,<br /> 'James' ,<br /> 'M' ,<br /> 1 ,<br /> '1942-10-11'<br /> )<br />GO<br /><br />A few thing to point out regarding the code above. Datetime and character data is entered using single quoted string. Integer data is entered without quotes. I also included all columns that were defined as NOT NULL. I also included the password column which is part of a unique index. You will note that I did not include columns for some of the non-unique indexes (soc_sec_num, other_id_num, and driver_license_num). I did this intentionally to demonstrate that they are not required columns. Having said this, in the "real world" one would only put indexes on columns where the intent was to actually collect the data for that column. I just wanted to make a technical point here. The pragmatic point is that you would want to expose columns that are part of indexes.<br /><br /><span style="font-weight: bold;">What stored row looks like after the SQL INSERT</span><br /><br />I want to call you attention to the fact that all columns that are not referenced in the query get set to null. Also, if the schema had default values defined these would also get stored for that column when the rows gets inserted.<br /><br /> p_password = 'mydogSpot1'<br /> p_active_flg = '1'<br /> p_lastname = 'Bag'<br /> p_firstname = 'James'<br /> p_gender_code = 'M'<br /> p_birth_dttm = '1942-10-11 00:00:00'<br /></span></div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0tag:blogger.com,1999:blog-8510290873286180737.post-77826532308263019372008-06-23T11:01:00.006+05:302008-06-23T11:20:07.511+05:30C# - TextBox AppendText<div style="text-align: justify; font-family: arial;font-family:arial;"><span style="font-size:100%;"><span style="font-weight: bold;">C# - TextBox AppendText</span><br /><br /></span><div style="text-align: justify;"><span style="font-size:100%;">This article demonstrates some reusable code that you can use to solve a problem related to newlines and TextBox control in its multiline mode. It can help really enhance the appearance and exactness of your Windows Forms program written in C#. The code is below and free for anyone to take and use.<br /></span></div><span style="font-size:100%;"><br /><span style="font-weight: bold;">Problem</span><br /><br /></span><div style="text-align: justify;"><span style="font-size:100%;">In .NET, you are able to append text to a multiline TextBox with AppendText. This is great and it makes it easy to keep a log going of what's happening or events or turns or whatever. However, that will not append a newline or line feed to the end of the text, so when you call textBox1.AppendText again, the text will be on the same line.<br /></span></div><span style="font-size:100%;"><br /></span><div style="text-align: justify;"><span style="font-size:100%;"><i>/// <summary></summary></i><br /></span><span style="font-size:100%;"><i><summary>/// Just a test method that shows the code being used.</summary></i><br /></span><span style="font-size:100%;"><i><summary>/// </summary></i><br /></span><span style="font-size:100%;">private void Test()<br /></span><span style="font-size:100%;">{<br /></span><span style="font-size:100%;"> for (int i = 0; i <><br /><span style="font-size:100%;"> { <b><br /></b>textBox1.AppendText("Some text");</span><br /><span style="font-size:100%;"> }</span><br /><br /><span style="font-size:100%;">}</span><br /></span></div><pre><span style="font-size:100%;"><br /><br /><span style="font-weight: bold;">Solution</span><br /></span></pre></div><div style="text-align: justify; font-family: arial;font-family:arial;"><div style="text-align: justify;"><span style="font-size:100%;">Let's explore some alternatives, and then I will show you the very best way that I have found, that solves the entire newline problem with TextBox controls in Windows Forms. First, let me show you what happens when you always append a newline to the TextBox. My first reaction was to change the code to this:<br /></span></div><span style="font-size:100%;"><br /></span><div style="text-align: justify;"><span style="font-size:100%;"><i>/// <summary></summary></i><br /></span><span style="font-size:100%;"><i><summary>/// Another test method.</summary></i><br /></span><span style="font-size:100%;"><i><summary>/// </summary></i><br /></span><span style="font-size:100%;">private void Test()<br /></span><span style="font-size:100%;">{<br /></span><span style="font-size:100%;"> for (int i = 0; i <><br /><span style="font-size:100%;"> {<br />textBox1.AppendText("Some text" + Environment.NewLine);</span><br /><span style="font-size:100%;"> }</span><br /><span style="font-size:100%;">}</span><br /></span></div><pre><span style="font-size:100%;"><br /></span></pre><div style="text-align: justify;"><span style="font-size:100%;">That works fine, but it isn't as elegant in the code and it also has a small problem:<br /></span><span style="font-size:100%;">when the TextBox is appended to, there will be a <b>blank line at the end</b>. For<br /></span><span style="font-size:100%;">a scrolling log-style scenario, I don't want that blank line. This is what the two<br /></span><span style="font-size:100%;">calls would produce:<br /></span></div><pre><span style="font-size:100%;"><br /></span></pre><div style="text-align: justify;"><span style="font-size:100%;">In one of my programs, that blank line caused a bit of a blemish on my UI. So I tried the following code. What I show next is the code and then what it will look like in your TextBox. As I show, it will put a blank line at the very start, which is very undesirable.<br /></span></div><span style="font-size:100%;">/// <summary><br />/// Another test.<br />/// </summary><br />private void Test()<br />{<br /> for (int i = 0; i < style="font-weight: bold;">Best Solution<br /><br /></span><div style="text-align: justify;"><span style="font-size:100%;">Nevermind--I whipped up a better solution and put it in the Windows form. The following code uses some conditional logic to test whether we need a newline on the TextBox append. This is better than AppendLine or any version of AppendText. What I show next is the new function, how you can call it, and then its output on the TextBox.<br /></span></div><span style="font-size:100%;"><br /><br />/// Append a line to the TextBox, and make sure the first and last<br />/// appends don't show extra space.<br /><br /></span><div style="text-align: justify;"><span style="font-size:100%;">The string you want to show in the TextBox.<br />private void AppendTextBoxLine(string myStr)<br />{<br />if (textBox1.Text.Length > 0)<br />{<br />textBox1.AppendText(Environment.NewLine);<br />}<br />textBox1.AppendText(myStr);<br />}<br /></span></div><span style="font-size:100%;"><br /></span><div style="text-align: justify;"><span style="font-size:100%;">///<br />/// Just a test method that shows the above code being used.<br />///<br />private void TestMethod()<br />{<br />for (int i = 0; i <></div><span style="font-size:100%;"><br /></span></div><div style="text-align: justify; font-family: arial;font-family:arial;"><span style="font-size:100%;"><br /><br /></span></div>Mohanhttp://www.blogger.com/profile/08306795953345279206noreply@blogger.com0