Welcome

Welcome to GauravDS

My name is Gaurav D Sawant. I’m a senior software engineer by profession. I’m an allrounder web application developer.

I have various hobbies one of which is to create DIY stuff.

DIY stuff consists of Electronics, Arduino, 3D Printing, Home automation using Raspberry Pi and many more…

In this website, you may encounter some of these hobbies along with some of my technical knowledge.

So let’s begin . . .

How to Set Up a Headless Raspberry Pi 3, Without attaching a Monitor?

https://www.youtube.com/watch?v=wwM6OBzf9xc
  1. Grab the Etcher software from https://etcher.io/
  2. Install Etcher and use it to write the Raspbian image to your SD card. You don’t need to extract the image or format the card prior to writing. Just choose the Raspbian .zip, pick your SD card and write (if you have trouble, verify the SHA256 checksum of the download).
  3. Remove and reinsert the SD card so that your Windows or Mac PC can see the small FAT32 partition on the SD card.
  4. On that small partition, create a file with the name ssh (or ssh.txt).
  5. To connect to a wireless network, create another file on the boot partition called wpa_supplicant.conf which has the following inside:

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    country=US

    network={
    ssid=”Your network SSID”
    psk=”Your WPA/WPA2 security key”
    key_mgmt=WPA-PSK
    }
  6. Safely eject the card from your PC and use it to boot the Pi.
  7. Find Pi IP address from the router.
  8. Grab the Putty software from https://www.putty.org/
  9. Connect putty using Pi’s IP address.
  10. Default Username is ‘pi’ and the default password is ‘raspberry’
  11. Open PI config using ‘sudo raspi-config
  12. Expand Filesystem (Recommended)
  13. sudo apt-get update‘ to update
  14. sudo apt-get upgrade‘ to upgrade
  15. Install remote desktop on Pi using ‘sudo apt-get install xrdp‘ .
  16. sudo reboot‘ to reboot.

Send Email using smtp C#

public void SendEmail(string smtpClient_add, int port_no, string username, string password, string fromEmail, string fromname, string Email_subject, string Email_body, string toEmail, string ccEmail, string pathToAttachment)
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient(smtpClient_add);

        mail.From = new MailAddress(fromEmail, fromname);
        mail.To.Add(toEmail);
		if (ccEmail != "")
        mail.CC.Add(ccEmail);
        mail.Subject = Email_subject;
        mail.Body = Email_body;
        mail.IsBodyHtml = true;
        if (pathToAttachment != "")
            mail.Attachments.Add(new Attachment(pathToAttachment));
        SmtpServer.Port = port_no;
        SmtpServer.Credentials = new System.Net.NetworkCredential(username, password);
        SmtpServer.EnableSsl = true;
        SmtpServer.Send(mail);
    }

Convert DataTable to HTML in C#

public static string ConvertDataTableToHTML(DataTable dt)
    {
        string html = "<table border='1' cellpadding='0' cellspacing='0'>";
        //add header row
        html += "<tr>";
        for (int i = 0; i < dt.Columns.Count; i++)
            html += "<th bgcolor='#507CD1' style='padding: 2px; color: #FFFFFF'>" + 
            dt.Columns[i].ColumnName + "</th>";
        html += "</tr>";

        //add rows
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            html += "<tr>";
            for (int j = 0; j < dt.Columns.Count; j++)           
            html += "<td style='padding: 2px;'>" + dt.Rows[i][j].ToString() + "</td>";                
        html += "</tr>";
        }
        html += "</table>";
        return html;
    }

Add Linked Servers using query

  • First, get all server name check if the server already linked or not

exec sp_linkedservers

  • Link server who has windows authentication

exec sp_addlinkedserver    @server=’SERVERNAME’

  • Link server who has login credentials

exec sp_addlinkedsrvlogin  ‘SERVERNAME’, ‘false’, null, ‘USERNAME’, ‘PASSWORD

Get All Database and Tables in SQL

DECLARE @DBName VARCHAR(256)
DECLARE @varSQL VARCHAR(512)
DECLARE @getDBName CURSOR

SET @getDBName = CURSOR FOR

SELECT name
FROM sys.databases where name NOT IN (‘master’, ‘tempdb’, ‘model’, ‘msdb’)

CREATE TABLE #TmpTable (DBName VARCHAR(256),
SchemaName VARCHAR(256),
TableName VARCHAR(256))

OPEN @getDBName
FETCH NEXT
FROM @getDBName INTO @DBName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @varSQL = ‘USE ‘ + @DBName + ‘;
INSERT INTO #TmpTable
SELECT ”’+ @DBName + ”’ AS DBName,
SCHEMA_NAME(schema_id) AS SchemaName,
name AS TableName
FROM sys.tables’

EXEC (@varSQL)
FETCH NEXT
FROM @getDBName INTO @DBName
END

CLOSE @getDBName
DEALLOCATE @getDBName
SELECT * FROM #TmpTable

Recover SQL statements

Recover MS SQL statements which get deleted/closed accidentally

SELECT
deqs.last_execution_time AS [Time],
dest.TEXT AS [Query]
FROM
sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE dest.dbid = DB_ID(‘YOURDATABASENAME’)
ORDER BY deqs.last_execution_time DESC

OR

SELECT
deqs.last_execution_time AS [Time],
dest.TEXT AS [Query]
FROM
sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY
deqs.last_execution_time DESC

Returns query with time.