Skip to main content

Posts

asp.net - How to create text file and write text programmatically

Create a text file and write text programmatically The following asp.net c# example code demonstrates how we can create a text file and write text on it programmatically in the .net framework. We can get the application's physical path using HttpRequest.PhysicalApplicationPath property as Request.PhysicalApplicationPath. We also can get the file path by adding two strings, which are the physical application path and file name with extension. In this example, we created a text file name Text.txt and programmatically wrote two lines of text in this file at run time. StreamWriter Class allows us to implement a TextWriter for writing characters to a stream in a particular encoding. In this example code, we initialize a StreamWriter object. The File class’s CreateText(path) method creates or opens a file for writing UTF-8 encoded text. this method requires passing a parameter name 'path'. The path parameter value specifies the fi...

c# - How to get index of an element from an array

Array IndexOf() Method The following asp.net c# example code demonstrate us how can we get index of an array element programmatically at run timein an asp.net application. Array Class Array.IndexOf(Array, Object) overloaded method search for the specified object and returnthe index of its first occurrence in a one-dimensional array. Array.IndexOf(Array, Object) method has two required parameters named 'array' and 'value'. The 'array' parameter represent theone-dimensional array to search and 'value' parameter represent the object to locate in array. In this example code, we initializesa string array with elements and pass a string object as 'value' parameter value to IndexOf() method to get its index. Array.IndexOf() method return an integer value which represent the index of the first occurrence of specified object in array, if found;otherwise it return the lower bound of the array minus 1. The method thr...

asp.net - DataBind a RadioButtonList with an array

DataBind a RadioButtonList with array The following ASP.NET C# example code demonstrates to us how can we data bind a RadioButtonList with an Array data source. RadioButtonList is an ASP.NET list web server control. RadioButtonList control renders a radio group on the web page. RadioButtonList’s each ListItem object renders a single RadioButton with the same group name. We can populate RadioButtonList items from various data sources such as SqlDataSource, ObjectDataSource, Array, ArrayList, Generic List, Dictionary, etc. The Array is a popular data source object to data bind with a list web server control such as RadioButtonList, BulletedList, ListBox, etc. Each element from the Array object creates a new ListItem object when we data bind a list server control with the Array data source. To populate a RadioButtonList with an Array data source is very simple in the ASP.NET C# environment. First, we need to create an Array object. Next, w...

asp.net - How to DataBind a CheckBoxList with an array

DataBind a CheckBoxList with array The following ASP.NET C# example code demonstrates to us how can we data bind a CheckBoxList web server control programmatically at run time with an Array data source object. CheckBoxList is an ASP.NET list web server control. CheckBoxList allows web users to select/check one or more items at a time. CheckBoxList contains ListItem objects. Each ListItem object represents an item. We can populate a CheckBoxList from Array elements. To do this, first, we need to initialize an array object. Then we populate the Array with items (elements). Next, we need to specify the newly created Array as CheckBoxList control's DataSource. Finally, we can data bind CheckBoxList control with Array by calling the CheckBoxList DataBind() method. The populated CheckBoxlist creates the same number of items as the number of elements that exist in the Array. Each Array element creates an item in CheckBoxList. We populated CheckBoxL...

asp.net - How to data bind a DropDownList with an array

DataBind DropDownList with array The following asp.net c# example code demonstrates to us how can we populate a DropDownList server control with items from an Array data source. DropDownList is a list web server control. DropDownList can contain one or more ListItem objects. A ListItem object includes Text and an optional Value. The Array is the most preferable data source for .NET developers to populate a list server control. In this example code, we initialized a String data type Array object with items/elements. Then we set the DropDownList's DataSource property value to the newly initialized Array object. Next, we call the DropDownList DataBind() method to data bind DropDownList with the Array data source. Each element of the Array creates an item in DropDownList. This is a one-dimensional Array, so DropDownList's each ListItem object's 'Text' and 'Value' properties hold the same value; such as if Array'...

c# - How to compare two strings by ignoring case

String Compare() Method - case insensitive/ ignore case The .NET C# developers can compare two specified String objects using the String class’s Compare() method. The String Compare() method returns an integer that indicates their relative position in the sort order. This method is overloaded. String Compare(String, String, Boolean) method overload compares two specified String objects by ignoring or honoring their case. So we can use this overloaded method to compare two string objects by ignoring case (case insensitive String comparison). This overloaded method requires three parameters. First, two parameters are two String objects those we need to compare. And third parameter is a Boolean value. We must set the Boolean parameter value to True when we need to ignore the case during comparison. Or we must set the Boolean parameter value to False to respect the case during String comparison. If the method returned integer is less than zero, then the fir...

c# - How to compare two strings

String Compare() Method - case sensitive The .NET framework has many methods to properly manipulate String. String Compare() method allows us to compare two String objects. This method is overloaded for various support such as we can compare strings by ignoring or honoring cases, we also can use culture-specific information to influence the comparison. String class’s Compare(String, String, Boolean) overloaded method allows us to compare two string objects by honoring their case (case-sensitive string comparison). First, two parameters are two String objects that we want to compare and the third parameter is a Boolean value name ignoreCase. This Boolean parameter value False specifies the method to honor the case of the string during comparison. This method return value is a 32-bit signed integer that indicates the relationship between the two string objects. If the return value is less than zero then the first string is less than the second String. Ret...