In this blog I will show how to cast an int to enum, enum to int and string to enum. Let's make a sample enum: MyEnum.cs namespace Sample.Console { public enum MyEnum { Item1 = 1, Item2 = 2, Item3 = 3, Item4 = 4, Item5 = 5, } } 1. Int to Enum int integerValue = 2; MyEnum myenum = (MyEnum)integerValue; //Output: item2 2. Enum to Int MyEnum myenum = MyEnum.Item3; int integerValue = (int)my...
Yeterday I had a requirement where I needed to find the end of day in datetime. I decided to make extension on datetime to achieve it. Below is the sample code: DateTimeExtension.cs: #region Using Directives using System; #endregion namespace Sample.Console { public static class DateTimeExtension { public static DateTime GetEndOfDay(this DateTime datetime) { return datetime.AddDays(1).Ad...
This message irritates me when I encounter this while debugging my code. I am writing the solution below by which this can be avoided. Step 1: Go to Build >> Configuration Manager and select "Debug " as the option for Active solution configuration. Step 2 : Go to your web project properties >> Build >> General Uncheck the Optimized code checkbox. Step 3: Clean and Rebui...
In this blog I will show how to get selected values of checkboxlist in ASP.NET. The selected value can be get by applying foreach iterator on checkboxlist items and then we can find the selected items. I decided to make an extension to do the same. Below is the code for it: Control Extensions: using System.Collections.ObjectModel; using System.Linq; using System.Web.UI.WebControls; names...
Sometimes it is needed to add some text with linebreaks manually in textarea. It can be done by adding slash r and slash n (\r\n). Below is the sample code to do the same. <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form id="form1" runat="server"> <textarea id="myTextBox" style="width:500px;height:300px"&...
While working with GitHub extensions commit command in VisualStudio, I noticed it was not showing the new files which I added to my solution. This may be some bug of GitExtensions but I was able to resolve this by using the Git's "Git - Pending Changes" command. Instead of using git commit command I went for Git- Pending Changes option. To do the same right click the solution and click G...
Version control is a tool to manage the changes to documents, programs, and other information stored as computer files. It allow us to track what changes we did, revert the changes, do modifications and much more. Many people still have excuses for not using version control, I have listed some of them which I have encountered them saying: I'm the only developer in my company, I don't nee...
I find it handy using shortcut keys for various commands instead of command buttons while using VisualStudio. Today, while working with GIT in Visual Studio, I decided to apply shortcut keys to various commands (Commit, Pull etc.). Below are the steps which I used to do so: Step 1: Go to Tools >> Options Step 2: In Enviornment select keyboard option. Step 3: In Show commands contai...
In this blog I will show you how to extract the text between square brackets, without returning the brackets themselves. I will use REGEX class of in order to do so. Let's assume we have a string "Are you ready for regex [test text] magic?" and we want to extract "test text" from it ie. the text which lies in between the square brackets. Below is the sample code for it: using System.Text...
In this blog I will show how to detect the IIS version using C#. We are going to make use of RegistryKey class which is the encapsulation for registry. Below is the sample code to detect the IIS version: using System; using Microsoft.Win32; namespace Sample { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Version version = GetI...
I was dropping a column today from a table and I got the following error ALTER TABLE DROP COLUMN xxx failed because one or more objects access this column. The complete message which was coming in SQL Server Query Analyzer message pane was: Msg 5074, Level 16, State 1, Line 1 The object 'DF_TestTable_RankId' is dependent on column 'RankId'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE...
Few minutes back I was working with a table in my DB and I wanted to add a new column with a default value to my existing table. I decided to write a quick blog on the script I used for the same. Syntax: ALTER TABLE {TABLENAME} ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE} Example: Let's create an example to do the same. We will first create...
Recently while working with some JavaScript code, I encountered an error in IE7 "IE Microsoft JScript runtime error: 'JSON' is undefined" The error was coming at this line var data=JSON.stringify(......) ; I was getting this error only in IE7 and not in IE8 or IE9. Then I came to know that JSON.stringify was not the part of IE7. It is supported in IE versions greater than 7 i.e IE8, IE9 ...
I recently encountered one question on forums "Prototype is not overriding the function/method in JavaScript" by a person. He was having the below code: function Test() { this.foo = function() { console.log('base foo'); }; } Test.prototype.foo = function() { console.log('overridden foo'); }; var test = new Test(); test.foo(); And running the above code the output was coming "base foo" as...
Today I was working with a datetime field where I needed midnight datetime of that date. The solution was to subtract the time of day from the date. Here is the code for it: private static void Main(string[] args) { DateTime myDateTime = DateTime.Now; DateTime midnight = myDateTime.Subtract(myDateTime.TimeOfDay); Console.WriteLine("Original DateTime: " + myDateTime); Console.WriteLine("M...
Today I was working with some dates and I encountered an issue while converting date to string. I noticed that inspite of providing the exact format to .ToString(<format>) method the separator was not coming correct after the date was converted to string format. I was using the below code: string date= DateTime.Now.ToString("M/dd/yy"); and the output I was getting is: As you might ...
Today I was working with a code where I needed to check whether a physical directory exists or not and if not, then I had to create it. I decided to write a quick blog on this. Below is sample code to do the same: string path = "D:\\test"; string basePath = Server.MapPath(path); if (!System.IO.Directory.Exists(basePath)) { System.IO.Directory.CreateDirectory(basePath); } Above with the h...
Today I saw a person on forums asking this "How to convert a string having comma separated integers to an array of integers" . He was having a string in this format "1,4,6,10,15,20" and he wanted to convert it into an array of integers. I decided to wrie a blog on this for converting the string to arrayof integers. Below is the code: using System.Linq; namespace MyConsole { internal clas...
Today when I was working with one of my C# application I needed to use IsNullOrWhiteSpace method. You must be familiar with String.IsNullOrWhiteSpace which was introduced in .NET 4. But I was using .NET 3.5 for my application so I decided to write the IsNullOrWhiteSpace method as my own custom string extension method. I got the code for IsNullOrWhiteSpace by decompiling the code of Strin...
Stack is a memory kept as a separate space for a particular thread execution. Today a person asked me what is the limit of stack for a particular thread in IIS. The answer is 256 KB . I collected the following information from By default, the maximum stack size of a thread that is created in a native IIS process is 256 KB By default, the maximum stack size of a thread that is created by ...
Today I was working with my client side code where I needed to do a deep copy of JavaScript object. I found an elegant way to do the same by using $.extend function of jQuery. This function can accept the first argument as boolean type variable which denotes whether we want a deep or shallow copy of an object. We can use it like below: var myDeepCopyObject = $.extend(true, {}, myObject);...
A person asked me today that whether we can use tild(~) with HTML controls on ASP.NET pages without using runat server attribute. I replied him, yes, this is possible. We can write our controls like this: <a href='<%= Page.ResolveUrl("~/controls/mypage.aspx") %>'>XXX</a> Though I prefer using tild with runat server attribute but yes the above one can be other option too...
Today a person asked aquestion on the forums on how to select the first item of dropdownlist with jQuery. I decided to write a quick blog on the code which I used to set it. <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"><...
Yesterday a person asked me on the forums on how to rename a table in SQL Server. I told him the script sp_rename oldname,newname to do the same. I decided to write a short blog on the same to show with some scripts: Let's create a dummy table and insert some data in it: GO CREATE TABLE [MyTable] ([ID] INT IDENTITY,[Name] NVARCHAR(20)) GO INSERT INTO dbo.MyTable ( Name ) SELECT 'ABC' UNI...
You might have enountered the autcomplete which appears by default on textboxes on forms. I was working with some textboxes(Password e.t.c) where I needed to disable the autocomplete. I did the same by setting the "autocomplete" property of textbox control from server side code. But then I decided to make a Custom server control for the same so that everytime I don't have to write the sa...
It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.
enter your email address:
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18