Constructor is a special method of the class that is automatically invoked when an instance of the class is created is called a constructor. The main use of constructors is to initialize the private fields of the class while creating an instance for the class. When you have not created a constructor in the class, the compiler […]
Category: C#
What does the word ‘new’ mean exactly in C#?
In C# (and many other languages) you will find that you use new often as it is fundamental to using types and classes. The new keyword does pretty much what it says: it will create a new instance of a class or type, assigning it a location in memory. Let me try and illustrate this. Company company; Here […]
Determine If string contains more than 1 value in C#
Let’s say you have a condition like that: if ((myString.Contains(“Value1”) && (myString.Contains(“Value2”)) && (myString.Contains(“Value3”))) { … } Basically, you want to check if all of your values are contained in the string . Fortunately (with the help of LINQ), this can by translated almost literally into C#: var values = new String[] {“Value1”, “Value2”, “Value3”}; if […]
How to calculate the sum of the datatable column in asp.net?
To calculate the sum of a column in a DataTable use the DataTable.Compute method. DataTable dt; … int sumValue; sumValue = dt.Compute(“Sum(TheSumColumn)”, string.Empty);
How to run code if not in Debug Mode in C#
Sometimes you want to run your code only when it is not in debug mode. You can use #IF DEBUG code block for this purpose #if DEBUG LogMail(“It is in debug mode, only add a log record.”) #else SendMail(“It is not in debug mode, so you can send e-mail.”) #endif
How to select distinct rows in a datatable in C#
The Scenario: You’ve loaded a massive DataTable from an Excel file, a CSV, or a legacy database. Now you realize it contains duplicate entries, and you need a unique list based on one or more columns—without writing complex foreach loops or heavy LINQ queries. The Pro Solution: The built-in DefaultView.ToTable() method is the most efficient […]
How to use Stopwatch in C#
The Stopwatch object is often used to measure how long things take. One quick thing to remember here is that it will take the time for everything you do between starting and stopping it, so make sure you only put the actual code you want to time between those.
How to access session variable in App_Code class file?
string MySessionValue = System.Web.HttpContext.Current.Session[“YourSessionVariable”].ToString();
ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn
You can use code below:
@helpers in Asp.NET MVC Razor
A @helper in Razor ultimately defines a function you can invoke from anywhere inside the view. The function can output literal text and can contain code blocks and code expressions. Don’t forget that heavy amounts of code in a view can lead to grief. You have @PluralPick(Model.Count, “child”, “children”). @helper PluralPick(int amount, string singular, string […]
How to set delivery format while using SMTP client in C#?
SmtpClient smtpClient = new SmtpClient(); smtpClient.DeliveryFormat = SmtpDeliveryFormat.International; You must note that it is only available with .NET Framework 4.5 or later.
How to trim whitespaces between characters in C#
trim() function does NOT remove “all whitespace characters” of your string. So instead of trim(), you can use String.Replace method: string str = “C Sharp”; str = str.Replace(” “, “”); OUTPUT: CSharp or if you want to remove all whitespace characters (space, tabs, line breaks…) string str = “C Sharp”; str = Regex.Replace(str, @”\s”, “”); […]
Using Code Blocks, Mixing Text & C# Code and Comments in Asp.NET MVC Razor
In Razor you can create blocks of code nearly anywhere using @{ }. A block of code doesn’t emit anything into the output, but you can use the block to manipulate a model, declare variables, and set local properties on a view. Be extremely careful about having too much code in a view, however, because […]
Using View Model to pass Multiple Models in Single View in MVC
In MVC we cannot pass multiple models from a controller to the single view. There are a few solution for that problem. But in this article we will use View Model. Here is the solution:
How to set Tooltip for a particular Devexpress Gridview Column
You can use code below:
Null-Conditional (?) and Null-Coalescing (??) Operators in C#
Null coalescing: The null coalescing operator “??” uses two question marks. With it you can use a custom value for a null reference variable. It simplifies null tests. C# program that uses null coalescing operator using System; class Program { static string _name; /// <summary> /// Property with custom value when null. /// </summary> static […]
Important Tips To Write Clean Code In C# from Debendra Dash
To develop reusable, reliable, readable, well-structured, and maintainable application, we must follow the coding standard. There are several coding practices in the software industry. None of them are good or bad but the main thing is that whatever approach we follow, we should make sure that everyone should follow the same standard.
Function with variable number of arguments in C#
By using the params keyword, you can specify a method parameter that takes a variable number of arguments. You can send a comma-separated list of arguments of the type specified in the parameter declaration or an array of arguments of the specified type. You also can send no arguments. If you send no arguments, the length of the params list is […]
Devexpress ASPxGridview Column Grouping in Code
The following code shows how to group by two columns (“Country” and “City”).
How to use the second argument of Response.Redirect: True or False
Response.Redirect(URL, false): The client is redirected to a new page and the current page on the server will keep processing ahead. Response.Redirect(“default.aspx”, false); Response.Redirect(URL, true): The client is redirected to a new page, but the processing of the current page is aborted. Response.Redirect(“default.aspx”, true);
How to solve “Response.Redirect cannot be called in a Page callback” for DevExpress Components
DevExpress.Web.ASPxClasses.ASPxWebControl.RedirectOnCallback(“the_redirect_page.aspx”);
How to use and get HiddenField Value in Gridview
You can add HiddenField to the Gridview like that:
Display HTPP Headers in C# & ASP.net
using System; using System.Web.UI; using System.Collections.Specialized; public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { NameValueCollection headers = base.Request.Headers; for (int i = 0; i < headers.Count; i++) { string key = headers.GetKey(i); string value = headers.Get(i); base.Response.Write(key + ” = ” + value + “<br/>”); } } }
Change format of the Day with Leading zero In DateTime
You can use both methods: DateTime.Now.Day.ToString(“00”); DateTime.Now.ToString(“dd”);
Add blank item at top of dropdownlist
drpList.Items.Insert(0, new ListItem(String.Empty, String.Empty)); drpList.SelectedIndex = 0;
Hide GridView Column on server-side
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Cells[index].Visible = false; }
Fire Combobox SelectedIndexChanged with button code-behind
myComboBox_SelectedIndexChanged(myComboBox, new EventArgs()); // or (null, null)






