Showing posts with label Technical. Show all posts
Showing posts with label Technical. Show all posts

Tuesday, August 10, 2010

Using a Stored Procedure in Entity Framework 4

If you have worked with LINQ and added stored procedures to it, it would seem obvious to be able to it the same way in Entity framework.

In LINQ all you had to do is to drag the stored procedure from your Server Explorer respective data connection on your model diagram.

In entity framework 4, there are few steps and they are different.

1. On your entity diagram in visual studio, right click and choose Update Model from database.

2. Add your stored procedure from the wizard. What it does, it adds the proc to your diagram. Its not visible in .Net code yet.
If you go to Model Browser > {EntityModel}.Store > Stored Procedures, it should be visible in there now

3. Right click, Add > Function Import. Complete this according to your requirement, name of the proc you want to see in .Net, which stored proc it maps to and what is the collection it returns etc.

After step 3 you can access access the proc using an instance of context.

For more visual representation of the steps, please visit WebLog of Ken Cox

Wednesday, August 4, 2010

Extracting tags from xhtml content

There are two ways of doing it. One is the dirty way, using Mid function in VB.Net or IndexOf string method but the more appropriate way would be to use regular expressions.

Following code will get you title using regular expression
Regex regex = new Regex("<title>(?<title>.*?)</title>", RegexOptions.IgnoreCase);
Match titleMatch = regex.Match(html);
string title = titleMatch.Groups["title"].Value;
Following code will get you meta tag My_Meta

Regex regex = new Regex("<META +NAME=\"(?<name>My_Meta*?)\" +CONTENT=\"(?<content>.*?)\" */?>", RegexOptions.IgnoreCase);
Match metaMatch = regex.Match(html);

title = metaMatch.Groups["content"].Value;

Thursday, July 15, 2010

Saving objects using Entity framework

TableClass tableObject = new TableClass();
tableObject.property1 = "Hello";
tableObject.proeprty2 = "World";

// NOTE: make sure to use the same instance of entityContextObject which was used to populate the tableObject (e.g. updating back to database)
entityContextObject.AddToTableClass(tableObject);
entityContextObject.SaveChanges();

Monday, June 28, 2010

Wednesday, March 31, 2010

outputting all attributes except one in xsl

In order to avoid outputting one attribute among all the attributes of a tag, following xpath can be used

Monday, October 5, 2009

Framework Design Guidelines - .Net

While looking for some guidelines I came across the webcast by Brad Adams and Krzysztof Cwalina. Very good video with some great tips. I'm definitely looking to get the book: Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries (2nd Edition)

Link: http://channel9.msdn.com/pdc2008/PC58/

Tuesday, September 29, 2009

Website vs Web Application VS 2008

Just recently I moved some code from one application to the other and ended up creating a website instead of web application. All the designer files started crashing on me and I ended up removing them since it wouldn't stop complaining about namespace issues.

Found this post which explains why it happens. Website model doesn't have designer files and are compiled on demand in single dlls. When you build a web application, it builds as one dll and maintain UI elements relation with code behind using designer files.

The link attached below explains how to do it, worked for me! So now I have decided that I'll use Web application instead of Website since its not suggested for non-smaller web applications.

Link: Detail Post by Guru Stop

Friday, September 25, 2009

How to get jQuery intellisense working VS2008

I followed steps from Scottgu blog post to setup jQuery in Visual Studio 2008 but it didn't work.

While searching for the solution I found this post from MWTech

Steps from the post are:

Getting jQuery Intellisense Up & Running

1. Ensure that VS2008 SP1 is installed. (Further info)
2. Ensure that Hotfix KB958502 is installed. (Further info)
3. Install the jQuery library into your project. It'll have a filename like "jquery-1.3.2.js".
4. Install the jQuery Intellisense file into your project. It may very well have a filename like "jquery-1.3.2-vsdoc2.js" but must be renamed to be identical to the jQuery library name, plus "-vsdoc". Thus in this example, it must be renamed to "jquery-1.3.2-vsdoc.js".
5. Provide a reference to the jQuery library. There are generally two ways to do this, both of which are described below.
6. In external Javascript files a direct reference to the jQuery Intellisense file must be made. More details are provided below.


Works perfectly! Have a look at the complete post if you are having same issues.

Wednesday, September 9, 2009

.NET IoC Container Comparisons

While looking for a comparison between so many Dependency Injection (IoC) frameworks available. Found this link which further points to a 2 article blog post. Good color coded comparison between some of the popular frameworks out there. Its missing MEF which I looked at today. Didn't get a chance to get more details on it but it seems pretty simple and easy to use.

Link: Part1
Link: Part2

Got the links from StackOverFlow post.

Wednesday, September 2, 2009

Is User member of AD group - VBA

Came across this small function which returns true or false based on the user belonging to a AD group. No need to add a reference to any dll. Very short and works great.

Quote:
Function IsMember(strDomain As String, strGroup _
As String, strMember As String) As Boolean
Dim grp As Object
Dim strPath As String

strPath = "WinNT://" & strDomain & "/"
Set grp = GetObject(strPath & strGroup & ",group")
IsMember = grp.IsMember(strPath & strMember)
End Function
Thanks to Greg Reddick
Link: http://my.advisor.com/doc/08691

Thursday, July 30, 2009

How do you convert a string into an enum?

Yesterday I was writing a method which would pass me a string which I needed to return as an Enum type. I did it using switch but didn't like doing it that way.

I found the solution on Tim Sneath's blog. I like one liners :), using it replaced bunch of code, in my case 10 cases (could be more or less depending on how many Enum values you have).
Quote:
enum Colour
{
Red,
Green,
Blue
}

// ...
Colour c = (Colour) Enum.Parse(typeof(Colour), "Red", true);
Please visit Tim's blog post if you would like to read the full post.

Monday, September 15, 2008

Response.End and download file problem using asp.net code inside sharepoint

Issue:

To download file from Sharepoint doesn’t work but works fine in plain asp.net environment.

Solution:

Solution is very simple, Add the following code for the button or link which initiates the download:

MyButton.OnClientClick = "document.getElementById(""" & Me.Page.Form.ClientID & """).onsubmit = function() {return true;}"

Link: http://social.msdn.microsoft.com/forums/en-US/sharepointdevelopment/thread/107b2c17-07fe-4a15-ad81-dcb31e1e9c84

Friday, September 12, 2008

Impersonating in Windows (WinForm)

Impersonating in asp.net is as simple as just adding a tag in web.config but in windows I found few links which were using kernel dll to impersonate. Article I found on theserverside.net shows new features in .net 2.0 which makes it very easy to do this.

Quote:

System.Diagnostics.ProcessStartInfo pInfo =   new System.Diagnostics.ProcessStartInfo();
            SecureString ss = new SecureString();
                          ... append password characters
            pInfo.Password = ss;
            pInfo.UserName = @"domain\alice";
            pInfo.FileName = @"c:\windows\notepad.exe";
            pInfo.UseShellExecute = false;
            Process.Start(pInfo);

Link: http://www.theserverside.net/tt/articles/showarticle.tss?id=NewSecurityFeatures

Wednesday, September 10, 2008

SPTraceView - Lightweight Tool for Monitoring the SharePoint Diagnostic Logging in Real-Time

Very small application, only 65kb zip, great tool for SharePoint diagnostic tracing

Quote:

SPTraceView moniors in real time all SharePoint diagnostic tracing (also called ULS tracing) and can notify you using a balloon-style messages in the tray bar when any information of particular interest to you is sent (traced) by any of the MOSS services and components.

Link: http://hristopavlov.wordpress.com/2008/06/19/sptraceview-lightweight-tool-for-monitoring-the-sharepoint-diagnostic-logging-in-real-time/

Saturday, September 6, 2008

OLE DB Connection string issue using Excel

Issue:
I came across two issues while trying to load a excel file in asp.net. I'll explain both of them separately.

1). Exception: The Microsoft Jet database engine could not find the object
This exception happens when the path is not fully qualified to the excel file used in the oledb connection string. Without path, its not going to through an exception while creating connection.

Before: Dim dsn As String = "provider=Microsoft.Jet.OLEDB.4.0;data source='MyFile.xls'; Extended Properties=""Excel 8.0;HDR=NO;"""

After: Dim dsn As String = "provider=Microsoft.Jet.OLEDB.4.0;data source='" &
Server.MapPath("MyFile.xls") & "'; Extended Properties=""Excel 8.0;HDR=NO;"""

Solution: use Server.MaptPath to get fully qualified path to a file to avoid this exception.

2). Exception: Could not find installable ISAM.
When using extended properties be careful with the syntax

Before: Dim dsn As String = "provider=Microsoft.Jet.OLEDB.4.0;data source='MyFile.xls'; Extended Properties=Excel 8.0;HDR=NO;"

After: Dim dsn As String = "provider=Microsoft.Jet.OLEDB.4.0;data source='" & Server.MapPath("MyFile.xls") & "'; Extended Properties=
""Excel 8.0;HDR=NO;"""

Solution: use quotes around the value of extended properties. If the syntax is wrong, it tries to look for another driver.

Thursday, August 28, 2008

Store user information on J2ME platform

Problem:

J2ME doesn’t support property files

Solution:

In J2SE properties file can be used to store information but that’s not available on J2ME platform, instead it provides a record store. A sample is available at Sun website.

Sample Link: http://developers.sun.com/mobility/midp/samples/#persist

Problem:

Export excel file from SQL Server 2005. In order to do SQL gave the attached error.

Solution:

Quote: (MSDN)

“By default, SQL Server does not allow ad hoc distributed queries using OPENROWSET and OPENDATASOURCE. When this option is set to 1, SQL Server allows ad hoc access. When this option is not set or is set to 0, SQL Server does not allow ad hoc access.”

MSDN Link: http://msdn.microsoft.com/en-us/library/ms187569.aspx

In order to turn this option on follow the link below

http://www.kodyaz.com/articles/enable-Ad-Hoc-Distributed-Queries.aspx

Tuesday, August 26, 2008

HTTP Post multipart file upload with J2ME

Task:

Upload images from a blackberry device to a website

Solution:

There are a lot of tutorials available to upload form data to a website but we spent sometime looking to post a file to a web server using Multi part. Found the article at Nokia which does the job exactly what we need to do. It’s a very to the point code sample with also a usage sample. Also attaching the code as a zip file incase the link is broken and because the site didn’t have a code download option. All credit goes to Nokia.

Link: http://wiki.forum.nokia.com/index.php/HTTP_Post_multipart_file_upload_with_J2ME

Wednesday, August 20, 2008

ShowDialog Memory leak in WinForm

Recently we had to use a common form to display all the reports for a windows application. For some reason all the reports would work only once. We were using a ReportForm.ShowDialog() to show the report form. Apparently ShowDialog doesn’t dispose the form when its closed as a normal form does when its shown using Form.Visible = True. After a lot of search I came across the article which mentioned that there is a memory leak issue if you don’t dispose the form properly after ShowDialog.

Best Practice according to the article is open the form using the following code and it works, solved our issue.

Quote:

using( SomeDialog sd = new SomeDialog() )
{
sd.ShowDialog();
}

Link:

http://kevin-berridge.blogspot.com/2007_08_01_archive.html

SharePoint Object Browser

Code project article for Sharepoint object browser. It comes very handy when doing programming with sharepoint.

Link:

http://www.codeproject.com/KB/sharepoint/sharepoint_object_browser.aspx