Sometimes you are not able to Capture Soap request using fiddler. In that case you have to make some changes in config file.
Add these lines to web.config files:
- <system.net>
- <defaultProxy>
- <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://127.0.0.1:8888" />
- </defaultProxy>
- </system.net>
AS we know that before C# 6 we use to include only namespaces as “using directive” But in C# 6 & C# 7 we can include static classes, enum, non-static classes & structs too as “using directive”.
In this code snippet I am going to show you a sample code for “using Struct as directive” so that you can aware about its syntax and usage. You may be interested in code snippet of,
1. 1. Using Static Classes as directive
2. 2. Using non-Static classes as directive
3. 3. Using Enum as directive
Below is the code snippet of “Using Struct as directive”,
- using static System.Drawing.Color;
- using static System.DateTime;
- using static System.Console;
-
- namespace UsingStructAsDirectiveinCSharp6n7
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
- var color =Red;
-
- var isLeapyear = IsLeapYear(2016);
-
- WriteLine($"Today is: " + Now.DayOfWeek);
-
- }
- }
- }
variable.ToString() and Convert.ToString(variable) both are used for converting data into string. But the main difference is that Convert.ToString() handles null, while ToString() doesn't handle null value.
Apart from that both works almost same. Let’s take an example to explain it:
- double amount = 10000.0;
- Console.WriteLine(amount.ToString());
- Console.WriteLine(Convert.ToString(amount));
In this case both method will convert “amount” to string and will return the same value but in case of null value variable.ToString() method will throw exception.
Now take another example:
- object myObject = null;
- string myString;
-
-
-
-
- myString = Convert.ToString(myObject);
As we know that we can get Enum and it value from an Enum type e.g. I want to create an enum for sorting of article like below:
- enum ArticleSortingOptions
- {
- MostPopularArticle =1,
- LatestArticles =2,
- FeaturedArtilces =3,
- MostLikedArtilces =4,
- MostCommentedArtilces=5
- }
And accessing it like
- WriteLine(MostPopularArticle.ToString());
- WriteLine((int)MostPopularArticle);
Output
MostPopularArticle
1
you may be thinking that I am aceesing enum item without enum name. Yes, it is possible in C# 6 & C# 7 that we can acess enum items without enum name for that we have to write enum name in using statement i.e.
- using static MultiValuedEnum.ArticleSortingOptions;
You will never prefer to display the value like “MostPopularArticle” but to be displayed like “Most Popular Article”. As we know that enum will not allow you to write an item with spaces. So we will require 3 types of values
1. Integer value of enum item.
2. 2. Name of enum Item (string Value of enum).
3. 3. Description of Enum (or some other value as per requirement).
We can achieve this by writing and extension method for enum.
- public static class ExtendEnum
- {
- public static string DisplayString<T>(this T enumVal)
- {
- FieldInfo fi = enumVal.GetType().GetField(enumVal.ToString());
- var attrs = fi?.GetCustomAttributes(typeof(DescriptionAttribute), true);
- if (attrs != null && attrs.Length > 0)
- {
- return ((DescriptionAttribute)attrs[0]).Description;
- }
- return "No Description found";
- }
- }
And access like:
- WriteLine(MostPopularArticle.ToString());
- WriteLine((int)MostPopularArticle);
- WriteLine(MostPopularArticle.DisplayString());
Complete Code:
- using System.ComponentModel;
- using System.Reflection;
- using static MultiValuedEnum.ArticleSortingOptions;
- using static System.Console;
-
- namespace MultiValuedEnum
- {
- enum ArticleSortingOptions
- {
- [Description("Most Popular Article")]
- MostPopularArticle = 1,
- LatestArticles = 2,
- FeaturedArtilces = 3,
- MostLikedArtilces = 4,
- MostCommentedArtilces = 5
- }
- class Program
- {
- static void Main(string[] args)
- {
- WriteLine(MostPopularArticle.ToString());
- WriteLine((int)MostPopularArticle);
- WriteLine(MostPopularArticle.DisplayString());
- }
- }
-
- public static class ExtendEnum
- {
- public static string DisplayString<T>(this T enumVal)
- {
- FieldInfo fi = enumVal.GetType().GetField(enumVal.ToString());
- var attrs = fi?.GetCustomAttributes(typeof(DescriptionAttribute), true);
- if (attrs != null && attrs.Length > 0)
- {
- return ((DescriptionAttribute)attrs[0]).Description;
- }
- return "No Description found";
- }
- }
- }
Output
MostPopularArticle
1
Most Popular Article
There are multiple ways to create an Angular app with Visual Studio and this article explains one of the easiest and the simplest ways to create Angular 5 app with Visual Studio 2017 using a template.
Let’s start creating an Angular 5 app with Visual Studio 2017 step by step.
Step 1
Download and install the latest version of TypeScript. Read More >>
In SQL Server sometimes we get some data for which we are not sure that from which table it is coming the we can use the below stored procedure to find it.
It has 3 input parameters:
- @DataToSearch: Data which you want to search.
- @DataToReplace: Data with which you want to replace.
- @IsExactMatch: Search for exact matching or search any mathing data. If you want to search any matching data you can leave it blank.
This stored procedure does not alter any value in database it only displays the list of table with column name which have the specified data. the @DataToReplace has value the it will show you an script to replace the value which you need to execute separately.
- CREATE PROCEDURE FindAnyStringFromTheDataBase
- @DataToSearch NVARCHAR(4000),
- @DataToReplace NVARCHAR(4000),
- @IsExactMatch BIT = 0
- AS
- SET NOCOUNT ON
-
- DECLARE @TempTable TABLE(RowId INT IDENTITY(1,1), SchemaName sysname, TableName sysname, ColumnName SysName, DataType VARCHAR(100), IsDataFound BIT)
-
- INSERT INTO @TempTable(TableName,SchemaName, ColumnName, DataType)
- SELECT Col.Table_Name,Col.TABLE_SCHEMA, Col.Column_Name, Col.Data_Type
- FROM Information_Schema.Columns AS Col
- INNER Join Information_Schema.Tables AS Tbl
- ON Col.Table_Name = Tbl.Table_Name
- AND Col.TABLE_SCHEMA = Tbl.TABLE_SCHEMA
- WHERE Table_Type = 'Base Table'
- And Data_Type In ('ntext','text','nvarchar','nchar','varchar','char')
-
-
- DECLARE @i INT
- DECLARE @MAXValue INT
- DECLARE @TableName sysname
- DECLARE @ColumnName sysname
- DECLARE @SchemaName sysname
- DECLARE @SQL NVARCHAR(4000)
- DECLARE @PARAMETERS NVARCHAR(4000)
- DECLARE @IsDataExists BIT
- DECLARE @SQLTemplate NVARCHAR(4000)
- DECLARE @dbName VARCHAR(100)
-
- SELECT @SQLTemplate = CASE WHEN @IsExactMatch = 1
- THEN 'If Exists(Select *
- From ReplaceTableName
- Where Convert(nVarChar(4000), [ReplaceColumnName])
- = ''' + @DataToSearch + '''
- )
- Set @IsDataExists = 1
- Else
- Set @IsDataExists = 0'
- ELSE 'If Exists(Select *
- From ReplaceTableName
- Where Convert(nVarChar(4000), [ReplaceColumnName])
- Like ''%' + @DataToSearch + '%''
- )
- Set @IsDataExists = 1
- Else
- Set @IsDataExists = 0'
- END,
- @PARAMETERS = '@IsDataExists Bit OUTPUT',
- @i = 1
-
- SELECT @i = 1, @MAXValue = MAX(RowId)
- FROM @TempTable
-
- WHILE @i <= @MAXValue
- BEGIN
- SELECT @SQL = REPLACE(REPLACE(@SQLTemplate, 'ReplaceTableName', QUOTENAME(SchemaName) + '.' + QUOTENAME(TableName)), 'ReplaceColumnName', ColumnName)
- FROM @TempTable
- WHERE RowId = @i
-
-
- PRINT @SQL
- EXEC SP_EXECUTESQL @SQL, @PARAMETERS, @IsDataExists = @IsDataExists OUTPUT
-
- IF @IsDataExists =1
- UPDATE @TempTable SET IsDataFound = 1 WHERE RowId = @i
-
- SET @i = @i + 1
- END
-
- SET @dbName = DB_NAME()
-
- SELECT SchemaName,@dbName as DB,TableName, ColumnName,
- 'Update '+ @dbName+'.'+SchemaName+'.'+TableName+ ' SET '+ColumnName+ ' = replace('+ColumnName+','''+ @DataToSearch+''','''+@DataToReplace+''')' as Script
- FROM @TempTable
- WHERE IsDataFound = 1
-
- GO