Saturday, May 29, 2021

How to Get Columns details from SQL Tables

Here is how you can get column names from specified table

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns 
WHERETABLE_NAME = 'MobilityOrders'

Here is how you can get column count from specified table

SELECT COUNT(COLUMN_NAME) as COUNT FROM INFORMATION_SCHEMA.Columns 
WHERE TABLE_NAME = 'MobilityOrders'

Here is how you can get column count from temp table

 SELECT COUNT(*) as Cnt FROM tempdb.sys.columns
 WHERE object_id = object_id('tempdb..#temp2')

Hope this helps 😀

Friday, May 21, 2021

How to: Looping through reader count dynamically C#

Here is how this can be done once you have the data call via Read methods
   
using (var reader = await sqlDbContext.ExecuteReaderAsync(command))
{
   
    while (await sqlDbContext.ReadAsync(reader))
    {
        // when count greaterthan 1
        if (reader.FieldCount > 1)
        {
            if (!string.IsNullOrEmpty(reader["ItemD"].ToString()))
            {
                mobilityChangeOrdersItem.ItemID= reader["ItemID"] == null ? 0 : Convert.ToInt32(reader["ItemID"].ToString());
            }
            if (!string.IsNullOrEmpty(reader["ID"].ToString()))
            {
                mobilityChangeOrdersItem.ID = reader["ID"] == null ? 0 : Convert.ToInt32(reader["ID"].ToString());
            }
        }
    }

    //Looping through complete list of return variables to find out requrired column
    string errorMessge = " SQL Message: -- *Start* ItemD -- :: " + mobilityChangeOrdersItem.ItemD;
    while (await reader.NextResultAsync())
    {
        var fieldvalues = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetName(i)).ToArray();

        while (await reader.ReadAsync())
        {
            if (fieldvalues.Contains("ErrorMessage"))
            { 
                errorMessge += Environment.NewLine + " ErrorMessage : " + reader["ErrorMessage"].ToString();
            }
            if (fieldvalues.Contains("ErrorProcedure"))
            {
                errorMessge += Environment.NewLine + " ErrorProcedure : " + reader["ErrorProcedure"].ToString();
            }
        }
        errorMessge += Environment.NewLine + " SQL Message: -- *End*";
        mobilityChangeOrdersItem.StatusMessage = errorMessge;
    } 
    mobilityChangeOrdersList.Add(mobilityChangeOrdersItem);
}

Hope this helps 😀

How to : Check if a column exists in a datareader in C#

 Here is how we can do in  C# supported Frameworks!!

var fieldvalues = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetName(i)).ToArray();

//get column name here
if (fieldvalues.Contains("ErrorMessage"))
{
    errorMessge += Environment.NewLine + " ErrorMessage : " + reader["ErrorMessage"].ToString();
}
//get column name here
if (fieldvalues.Contains("ErrorProcedure"))
{
    errorMessge += Environment.NewLine + " ErrorProcedure : " + reader["ErrorProcedure"].ToString();
}

BTW, ErrorMessage will return the value after checking these fields 

Hope this helps 😀

How to Enable or Disable Fast User Switching in Windows PC

Fast User Switching is an easy way for another person to log on to the computer without logging you off or closing your programs and files. Follow the steps below to for Fast Switch User.

1. To begin, make sure that you have logged in as Administrator or have required administrative rights to change local group policies.

2. Press the Windows +R button.

3. Run dialog box will pop-up,type gpedit.msc. This will open Local Group Policy Editor. (see pic 1 for reference)

4. Now, go to the following location and look for Local Computer Policy in the left pane.

5. Click on Local Computer Policy/Computer Configuration/Administrative Templates/System/Logon. (See Pic 2 for refrence)

6. Just double-click Hide Entry Points for Fast User Switching to bring up a dialog box to change Fast User Switching policy setting.

7. At the top, there are Not Configured, Enabled and Disabled options available. Selecting each setting will let you read its affect in Help section. To disable Hide Entry Points for Fast User Switching, just select Enabled from the list and click OK.

If you need to diable this option, you need to select Disabled from the list. Rest all process is same.

8. Once done, you will have to enforce this change made to Fast User Switching policy setting. To do so, close the Local Group Policy Editor and open the Run dialog box (Windows +R button). Enter the command “gpupdate /force“ and click OK. The policy will be updated and applied on all user accounts.

9. To re-enable Fast User Switching, just choose Not Configured in its policy setting dialog and apply the changes via gpupdate /force command.

Hope this helps setting up mutiple users to login on your PC.