Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Author: ErcanOPAK

Asp.Net Core

How to get the current logged in user ID in ASP.NET Core

- 19.03.24 - ErcanOPAK comment on How to get the current logged in user ID in ASP.NET Core

using System.Security.Claims; var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); Note that this extension method only works for Users inside controllers, not view components, as the view component User is of IPrincipal.  

Read More
SQL

How to check seed value of tables in SQL

- 15.03.24 - ErcanOPAK comment on How to check seed value of tables in SQL

–View the current value: DBCC CHECKIDENT (“{table name}”, NORESEED) –Set it to the max value plus one: DBCC CHECKIDENT (“{table name}”, RESEED) –Set it to a spcefic value: DBCC CHECKIDENT (“{table name}”, RESEED, {New Seed Value})  

Read More
SQL

How to find a specific text string in a SQL Server Stored Procedure, Function, View or Trigger

- 05.12.23 - ErcanOPAK comment on How to find a specific text string in a SQL Server Stored Procedure, Function, View or Trigger

— Applicable for SQL 2005 and later versions USE [Your_DB]; SELECT [Scehma] = schema_name(o.schema_id), o.Name, o.type FROM sys.sql_modules m INNER JOIN sys.objects o ON o.object_id = m.object_id WHERE m.definition like ‘%your_text_to_search%’  

Read More
Windows

How to convert JPG or PNG to WebP in batch or single mode

- 12.11.23 - ErcanOPAK comment on How to convert JPG or PNG to WebP in batch or single mode

To convert JPG or other image formats to WebP, you can use the WebP command line converter tool provided by Google. You can download the WebP converter tool from https://developers.google.com/speed/webp/download (Click Download for Windows). Once downloaded, extract the folder to C:\ directory or wherever else you like (you will need the path to converter tool later). After extracting the folder, […]

Read More
SQL

How to get the first and the last day of previous month in SQL Server

- 06.11.23 - ErcanOPAK comment on How to get the first and the last day of previous month in SQL Server

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) –First day of previous month SELECT DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) –Last Day of previous month  

Read More
SQL

Not Null check on LEFT function with T-SQL

- 06.11.23 - ErcanOPAK comment on Not Null check on LEFT function with T-SQL

CONCAT function ignores NULLs: SELECT CONCAT(LEFT(LastName, 1), ‘,’ , LEFT(FirstName, 1), ‘ ‘ + LEFT(MiddleName, 1)) theNameWithInitials FROM myTable If you use CONCAT, you will not need to worry about whether LEFT(X) is null or not.

Read More
Türkiye

NICE 100 YILLARA TÜRKİYEM

- 28.10.23 | 03.02.24 - ErcanOPAK comment on NICE 100 YILLARA TÜRKİYEM
Read More
Git

How to delete all commit history in github

- 27.10.23 | 27.10.23 - ErcanOPAK comment on How to delete all commit history in github

Deleting the .git folder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following: Checkout git checkout –orphan latest_branch Add all the files git add -A Commit the changes git commit -am […]

Read More
CSS

How to display HTML components on the same line in CSS

- 27.10.23 - ErcanOPAK comment on How to display HTML components on the same line in CSS

Let’s say we have 3 buttons and we want to show them on the same line: #outer { width:100%; text-align: center; } .inner { display: inline-block; } <div id=”outer”> <div class=”inner”><button type=”submit” class=”msgBtn” onClick=”return false;” >Save</button></div> <div class=”inner”><button type=”submit” class=”msgBtn2″ onClick=”return false;”>Publish</button></div> <div class=”inner”><button class=”msgBtnBack”>Back</button></div> </div>  

Read More
SQL

How to insert results of a stored procedure into a temporary table

- 13.10.23 - ErcanOPAK comment on How to insert results of a stored procedure into a temporary table

CREATE TABLE #tmpTable ( COL1 int, COL2 int, COL3 nvarchar(max), COL4 nvarchar(max), COL5 bit ) INSERT INTO #tmpTable exec SpGetRecords ‘Params’ NOTE: The columns of #tmpTable must be the same as SpGetRecords. Otherwise, there will be a problem. If there are no parameters in the stored procedure, you don’t need to use ‘Params’.

Read More
C#

How to remove all non alphanumeric characters from a string in C#

- 28.09.23 - ErcanOPAK comment on How to remove all non alphanumeric characters from a string in C#

Replace [^a-zA-Z0-9] with an empty string. string str = “This-is-my+++***RegexPattern&Text”; Regex rgx = new Regex(“[^a-zA-Z0-9]”); str = rgx.Replace(str, “”); OUTPUT: ThisismyRegexPatternText If you want to add an exception then you should add this character like this to the regex pattern (let’s assume you wish to exclude the ampersand): [^a-zA-Z0-9 &] string str = “This-is-my+++***RegexPattern&Text”; Regex rgx = […]

Read More
C#

How to get the Xth Day of the Week of the Year in C#

- 25.09.23 - ErcanOPAK comment on How to get the Xth Day of the Week of the Year in C#

Let’s say we want to find the date of the first Friday of the 1st month. GetFirstFridayOfMonth(01.01.2023) =>06.01.2023 (The first Friday after 01.01.2023 is 06.01.2023) public DateTime GetFirstFridayOfMonth(DateTime date) { DateTime fridayDate = new DateTime(date.Year, date.Month, 1); //Let’s assume 1st Friday can start on the 1st of the month while (fridayDate.DayOfWeek != DayOfWeek.Friday) { fridayDate […]

Read More
C#

How to get formatted JSON in C#

- 23.09.23 | 23.09.23 - ErcanOPAK comment on How to get formatted JSON in C#

Like converting XML to Json or Json to XML, you can also use Newtonsoft to get formatted Json. private static string FormatJson(string json) { dynamic parsedJson = JsonConvert.DeserializeObject(json); return JsonConvert.SerializeObject(parsedJson, Formatting.Indented); } Note: You can use var instead of dynamic. Both will work.

Read More
C#

How to convert JSON to XML or XML to JSON in C#

- 23.09.23 | 23.09.23 - ErcanOPAK comment on How to convert JSON to XML or XML to JSON in C#

To do that easily, you can use Newtonsoft. // To convert an XML node contained in string xml into a JSON string XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string jsonText = JsonConvert.SerializeXmlNode(doc); // To convert JSON text contained in string json into an XML node XmlDocument doc = JsonConvert.DeserializeXmlNode(json); You can get more info from […]

Read More
SQL

How to use OUTPUT for Insert, Update and Delete in SQL

- 19.09.23 - ErcanOPAK comment on How to use OUTPUT for Insert, Update and Delete in SQL

The OUTPUT clause was introduced in SQL Server 2005. The OUTPUT clause returns the values of each row that was affected by an INSERT, UPDATE, or DELETE statement. It even supports a MERGE statement, which was introduced in SQL Server 2008 version. The OUTPUT clause has access to two temporary or in-memory SQL tables, INSERTED […]

Read More
SQL

How to get the first and last date of the current year in SQL

- 18.08.23 - ErcanOPAK comment on How to get the first and last date of the current year in SQL

SELECT DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfYear, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS LastDayOfYear, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0) AS FirstOfNextYear, DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)) AS LastTimeOfYear Thanks to Jamie F for the answer.

Read More
Reportviewer

How to solve extra blank page at end of Microsoft Reportviewer

- 31.07.23 - ErcanOPAK comment on How to solve extra blank page at end of Microsoft Reportviewer

To solve that problem you need to set the property ConsumeContainerWhitespace to True (in the properties dialog, F4).

Read More
Chrome / Tips and Tricks

How to Use Picture-in-Picture in Chrome Browser

- 26.06.23 | 26.06.23 - ErcanOPAK comment on How to Use Picture-in-Picture in Chrome Browser

Go to the Picture-in-Picture Extension (by Google). Click Add to Chrome. Click Add Extension. Find a video you want to watch. Click the picture-in-picture icon in the Chrome toolbar. The video will pop out and keep playing while you have different programs open. You must keep the original video tab open in Chrome to watch in picture-in-picture. Thanks to LifeWire.com […]

Read More
ASP.Net MVC / ASP.Net WebForms / Bootstrap / CSS

How to add some content to the right side of CardHeader on Bootstrap

- 21.06.23 | 21.06.23 - ErcanOPAK comment on How to add some content to the right side of CardHeader on Bootstrap

<div class=”card-header container-fluid”> <div class=”col-md-10″><h3>Your Header Title</h3></div> <div class=”col-md-2 float-right”>The content you want to add to the right side.</div> </div>  

Read More
ASP.Net MVC / ASP.Net WebForms / Bootstrap / JavaScript

How to change star rating color on mouseover/out, mouseenter/leave with Javascript

- 20.06.23 - ErcanOPAK comment on How to change star rating color on mouseover/out, mouseenter/leave with Javascript

function onMouseEnter(index){ var star = document.getElementsByClassName(“starClass”); var j; for(j = 0; j < fa.length; j++){ if(j < index){ star[j].style.color = “orange”; } else{ star[j].style.color = “#ccc”; } } } function onMouseOut(){ var star = document.getElementsByClassName(“starClass”); var k; for(k = 0; k < fa.length; k++){ star[k].style.color = “#ccc”; } }

Read More
ASP.Net MVC / C# / Entity Framework

How to get rows count in EntityFramework without loading contents

- 20.06.23 - ErcanOPAK comment on How to get rows count in EntityFramework without loading contents

Query syntax: var count = (from t in context.MyTable where t.Id == @Id select t).Count(); Method syntax: var count = context.MyTable .Where(t => t.Id == @Id) .Count() Both generate the same SQL query.

Read More
ASP.Net MVC

How to use if else block to decide style in Razor

- 20.06.23 - ErcanOPAK comment on How to use if else block to decide style in Razor

Declare a local variable at the beginning of the View: @{ var yourStyle = “”; if(Model.Condition) { yourStyle = “float:left;”; //specific conditions and logic } else { yourStyle = “float:right;”; //any other conditions and logic } } and then just use it in your div: <div style=”@yourStyle”>

Read More
SQL

How to get difference between 2 tables in MSSQL

- 17.06.23 - ErcanOPAK comment on How to get difference between 2 tables in MSSQL

If you have two tables A and B, both with column C, here are the records, which are present in the table A but not in B: SELECT A.* FROM A LEFT JOIN B ON (A.C = B.C) WHERE B.C IS NULL To get all the differences with a single query, a full join must be used, like this: […]

Read More
Wordpress

Solution for “utf8mb4 requires a newer client library” error in WordPress

- 20.05.23 - ErcanOPAK comment on Solution for “utf8mb4 requires a newer client library” error in WordPress

While checking the WordPress ‘Site Health’ status page, you may see ‘utf8mb4 requires a newer client library‘ highlighted by that tool: This is an issue that only applies to cPanel hosting servers when they use the CloudLinux PHP Selector, which all of our shared servers are using. The fix however is pretty easy. First, login to […]

Read More
C#

How to use TrimEnd to remove last character from string in C#

- 11.03.23 | 11.03.23 - ErcanOPAK comment on How to use TrimEnd to remove last character from string in C#

string useOfTrimEnd1= “Hello World !”.TrimEnd(‘!’); OUTPUT: Hello World string useOfTrimEnd2= “Hello World !!!!!!”.TrimEnd(‘!’); OUTPUT: Hello World Definition from Microsoft.com: TrimEnd(Char) Removes all the trailing occurrences of a character from the current string. TrimEnd() Removes all the trailing white-space characters from the current string. TrimEnd(Char[]) Removes all the trailing occurrences of a set of characters specified […]

Read More
C#

What is the difference between “int” and “uint” / “long” and “ulong”?

- 10.03.23 | 10.03.23 - ErcanOPAK comment on What is the difference between “int” and “uint” / “long” and “ulong”?

The primitive data types prefixed with “u” are unsigned versions with the same bit sizes. Effectively, this means they cannot store negative numbers, but on the other hand, they can store positive numbers twice as large as their signed counterparts. The signed counterparts do not have “u” prefixed.   The limits for int (32-bit) are: […]

Read More
SQL

How to solve the stucking on “Loading Packages” phase for SSMS installation

- 10.03.23 - ErcanOPAK comment on How to solve the stucking on “Loading Packages” phase for SSMS installation

to fix this problem: remove temp files from %temp% folder (C:\Users<user name>\AppData\Local\Temp) open the register and remove “HKLM\SOFTWARE\WOW6432Node\Microsoft\Microsoft SQL Server Management Studio” or execute via cmd (with admin rights): reg DELETE “HKLM\SOFTWARE\WOW6432Node\Microsoft\Microsoft SQL Server Management Studio” /reg:32 run install with admin rights NOTE: Just for those who don’t know register just open CMD as an […]

Read More
Türkiye

Geçmiş Olsun Türkiyem

- 06.02.23 | 10.03.23 - ErcanOPAK comment on Geçmiş Olsun Türkiyem

e-Devlet Kapısı üzerinden verilen bu hizmetlerin linkleri şöyle: En yakın kan bağışı merkezi için: https://www.turkiye.gov.tr/turk-kizilay-en-yakin-kan-bagis-merkezi AFAD Deprem Bağış Kampanyası için: https://www.turkiye.gov.tr/afet-ve-acil-durum-yonetimi-deprem-bagis-kampanyasi Türk Kızılay Deprem Bağış Kampanyası için: https://www.turkiye.gov.tr/turk-kizilay-deprem-bagis-kampanyasi  

Read More
C#

What is the difference between ‘ref’ and ‘out’ keywords in C#

- 14.01.23 - ErcanOPAK comment on What is the difference between ‘ref’ and ‘out’ keywords in C#

The best way to understand that difference is to show it in code. Let’s say we have an ‘Add’ function. And the only thing that method does is add 10 to the parameter value. static void Main (string[] args) { int myParameter = 20; Add(myParameter); Console.WriteLine(myParameter); } static void Add(int myParameter) { myParameter = myParameter […]

Read More
ASP.Net MVC / ASP.Net WebForms

How to check if javascript is enabled on the client’s browser

- 13.01.23 | 13.01.23 - ErcanOPAK comment on How to check if javascript is enabled on the client’s browser

The noscript element represents nothing if scripting is enabled, and represents its children if scripting is disabled. It is used to present different markup to user agents that support scripting and those that don’t support scripting, by affecting how the document is parsed. <script type=”text/javascript”> //If javascript enabled then this part will work. //Your javascript code here… </script> <noscript>JavaScript disabled on your […]

Read More
Page 61 of 69
« Previous 1 … 56 57 58 59 60 61 62 63 64 65 66 … 69 Next »

Posts navigation

Older posts
Newer posts
April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (448)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com