Posts

Showing posts from 2019

AWS-Python: Fetching credentials stored in AWS Secret Manager with Python

AWS Secret Manager is a great service to keep all the credetial secretly. Anytime you need to access them to use inside your code, you can fetch those through the AWS SDK. AWS SDK need some information to be ready before start you send any request to AWS Secret Manager AWS Access Key AWS Secret Access Key Region Name Secret Name(The name you have given to store your credential in AWS Secret Manager) Here is code snippet that you need to write to retrive the information from AWS Secret Manager. import boto3 import base64 from botocore.exceptions import ClientError def get_secret(): secret_name= "redshift\db\dbname" region_name = "us-east-1" client = boto3.client( 'secretsmanager' , aws_access_key_id = '<aws_access_key_id>' , aws_secret_access_key = '<aws_secret_access_key>' , region_name = '<region_name>' ...

SQL: Find Column Name from MS SQL database

Use the following queries to find the column name from the tables from the database. SELECT table_name=sysobjects.name, column_name=syscolumns.name FROM sysobjects JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE sysobjects.xtype=’U’ AND syscolumns.name like ‘FirstName’ ORDER BY sysobjects.name,syscolumns.colid SELECT table_name=sysobjects.name, column_name=syscolumns.name, datatype=systypes.name, length=syscolumns.length FROM sysobjects JOIN syscolumns ON sysobjects.id = syscolumns.id JOIN systypes ON syscolumns.xtype=systypes.xtype WHERE sysobjects.xtype='U' and syscolumns.name='CARDNAME' ORDER BY sysobjects.name,syscolumns.colid

LINQ: Using int.TryParse() within a LINQ where clause

I pretty new to LINQ, and I’m keen to get more experience using it, so whenever an opportunity arises I like to try writing LINQ queries. I needed to write a method to extract a comma separated list of numbers from a config file, and return this as List. I was looking at ways to do this using LINQ, but hit a problem. I wanted my LINQ query to filter out any values in the CSV string that could not be parsed as an int, without causing an exception. Using int.TryParse() seemed like a possible solution, but I had problems because the TryParse() method has an out parameter to store the parsed result. I ended up with the code below, which seems to work, but looks very messy, because I think it is parsing the string twice. public static List<int> AuthorisedGroups { get       {             string[] authorisedGroupsStr = ConfigurationManager.AppSettings["AuthorisedGroups"].Split(new char[] { ...

LINQ: Implement NOT IN tables with LINQ to Entity

In SQL so many times we need to filter value from one table when the values are not exist on other table. As Below SELECT    GroupID ,    GroupName ,    GroupNumber      FROM  TableA     WHERE  GroupID NOT IN ( SELECT GroupID FROM TableB )   You can do it as below: return   from c in  context . TableA where !db. context . TableB .Any(p => p.GroupID== c.GroupID) select c;

LINQ: implement IN tables with LINQ to Entity

In SQL so many times we need to filter value from one table when the values are exist on other table. As Below SELECT    GroupID ,    GroupName ,    GroupNumber ,   FROM    TableA  WHERE    GroupID IN ( SELECT GroupID FROM TableB )   You can do it as below:  var temp = context . TableA           . Where ( x => context . TableB . Any ( y => y . GroupID != x . GroupID ))           . Select ( x => new { GroupID = x . GroupID , GroupName = x . GroupName , GroupNumber = x . GroupNumber }). ToList (); or return DomainContext.TableA.Where(a => DomainContext.TableB.Any(b => b.GroupId == a.GroupId))

LINQ: Left Outer Join With LINQ to Entity

In LINQ every join you made is by default Inner Join. To perform the Left Outer Join you can do like below: from mem in ctx.Member join dist in ctx.District on mem.district_id equals dist.district_id into jointDist from dist in jointDist.DefaultIfEmpty() select new StagingClass {  member_id = mem.member_id,  district_name = dist.district_name }

LINQ: Showing Single Record From Child Table with Parent Table Row.

In LINQ if you want to show the child table single record with the parent table, you can do like below: from mem in ctx.Member join ord in ctx.MemOrder on mem.member_id equals ord.member_id into memord select new StagingClass {  mem.member_id,  latestOrderNo = memord.FirstOrDefault().OrderNo }

Silverlight: Simple way to create call back method.

The Simple way to create a call back method in .Net. also you can pass on the value to the callback object inside the another call backmethod to your callbackmethod. Private Guid userId; public void SaveAndComplete(bool complete, Action onComplete) { userId= //One more callback method. _dc.SubmitChanges(SaveCompleted, onComplete); } private void SaveCompleted(SubmitOperation so) { if (!so.HasError) { //this is where you can pass on the value to the call back method. ((Action)so.UserState)(UserId); } else { //Error message } } Calling the callback method "SaveAndComplete" private void Save_Click(object sender, RoutedEventArgs e) { SaveAndComplete(false, SavedSuccessFully); } private void SavedSuccessFully(Guid generatedID) { //here you will get the value which you hve passed from the callback method. if (generatedAssessmentID == Guid.Empty) return; }

Silverlight: Finding Parent control

The problem the UserControl's parent is not the ChildWindow, its the Grid inside the child window. You need to get the parent of the parent of the UserControl to navigate to the ChildWindow:- ChildWindow cw = (ChildWindow)((FrameworkElement)this.Parent).Parent; However embedding this in your UserControl would bad practice, you would be stipulating to the consumer of your UserControl where the it can be sited. In the above case for the user control to work it would need to always be a direct child of the Layout root. A better approach would be to search up the visual tree looing for a ChildWindow. I would use this helper method (actually I'd place this in a helper extensions static class but I'll keep it simple here). private IEnumerable Ancestors() { DependencyObject current = VisualTreeHelper.GetParent(this); while (current != null) { yield return current; current = VisualTreeHelper.GetParent(current); } } Now you can use LINQ methods to get the...

Silverlight: Normal Properties vs. Dependency Properties

In the Silverlight development world there are two kinds of properties that you can have on a Silverlight control: normal C# properties and dependency properties. The normal C# properties are added to a Silverlight control the same way as they would be added to any C# class: public string MyProperty { get; set; } And then in XAML, you could set the value of the property like this: <MyControl MyProperty= "someValue" /> This works fine for setting specific values in templates that are unlikely to change or if they do change then the change is always manually made to the XAML file. But what happens if you want to have the value of the property bound to some other value that is retrieved from a database, web service, etc? You might try Binding to the property like so: <MyControl MyProperty= "{Binding DynamicPropertyValue}" /> Where DynamicPropertyValue was a property on the parent element that you wanted to to have your pro...

SQL: Getting rownumber

Suppose you have List of record from a table and you need the rownumber for each row you can use this below query: row_number() OVER (ORDER BY [Project2].[case_no] ASC) AS [row_number]

SQL: Conditional Where Clause

If you have some scenario where you need to add where clause based on some condition. for example: GradeID     MinMark   MaxMark    Grade 1           0          0          A 2           0          10         B 3           11         20         C 4           21         NULL       D As per above table we need Grade based on parameter @Mark to be checked as per the ...

IIS: Make Your site work with https on local IIS.

To make your site work on the https you need to follow some steps: Open the IIS Select the local Server on left side Tree(rootnode) Now on right hand side area Double Click on Server Certificates After that on right hand side panel select the Create Self Signed Certificate. Remeber this certificate is not a trusted certificate. That will open an dialog and give a friendly name and then click OK. Now on the left panel tree navigate to the Default Web Site or the website and select it,which you want to run on https. Now on the right hand side panel click on Binding that will open a dialog. Click the Add button will open another dialog in that dialog you need to slect the type "https". Then select the certificate you created ob the step 4 and 5 and click OK. Now you can brows your localhost with https as https://localhost/ Now enjoy using https in your local environment to test the application.

IIS: Created self signed certificates using makecert.exe for local IIS

Image
You have learned how to run the local IIS on https with self signed certificate with my previous blog http://iistipsandtricks.blogspot.in/2015/08/make-your-site-work-with-https-on-local.html. Now you want to get rid of the error you get when you first time get when you run your site with a untrusted self signed certificate. Also if you are working with SAS platform then you need to send authentication token which require trusted certificate.  These are steps below to make it work. 1. Open the Developer command prompt for visual Studio. 2. Run the below 2 commands on that window command prompt. makecert -r -n "CN=localhost" -b 01/01/2000 -e 01/01/2099 -eku 1.3.6.1.5.5.7.3.1 -sv localhost.pvk localhost.cer cert2spc localhost.cer localhost.spc pvk2pfx -pvk localhost.pvk -spc localhost.spc -pfx localhost.pfx You can do selecting all the 2 commands and paste to the command window and hit enter. 3. This above command will ask you 3-4 times for the password...

IIS: Publish Project to remote IIS with Web Deploy.

Image
To Push a build is too much hectic task. Like you need to publish to local machine and then copy to the remote IIS. Then can be eased by directly push the build to the remote IIS through web deploy. For this there is 2 things you need configure on in the remote IIS and other on your project file. Configuration on Remote IIS To make this whole thing work you need to install Microsoft Web Deploy and you need to choose all the feature to be install on the machine. you need to add the feature of IIS Management Service. for this you need to make sure you have added Management tool feature to the IIS. Configuration on the Project side In the Visual Studio right click on the solution and make a rebuild. Then right click on the web project and select the menu "Publish". On the Profile tab Drop down list select New Custom Profile. Give a name to the publish Profile and click OK. Then click next and on the next tab "Connection" sele...