Dot Net Learning Zone Blog

A blog about to learn Dot Net

Archive for the ‘C#’ Category

Entity Framework – SaveChanges() doesn’t get the generated Identity key

Posted by Rajesh on December 24, 2010

Step to get identity key
Suppose T_Emp is table placed on emp.edmx. Now open with emp.edmx in xml and find EntityType Name=”T_EMP”.
Finally , what you have to do add StoreGeneratedPattern=”Identity” in primary key . Now Save the edmx and execute the project.
Now you can get identity key after SaveChanges()

Entities e1 = new Entities();
T_Empl a = new T_Empl();
a.Name = “Rajesh”;
e1.AddToT_Empl(a);
e1.SaveChanges();
var id = a.ID;
——————————————————
<EntityType Name=”T_EMP”>
<Key>
<PropertyRef Name=”ID” />
</Key>
<Property Name=”ID” Type=”number” Nullable=”false” StoreGeneratedPattern=”Identity”/>
<Property Name=”Name” Type=”varchar2″ MaxLength=”100″ />
</EntityType>

Posted in Entity framework | Leave a Comment »

WCF link

Posted by Rajesh on April 29, 2010

http://msdn.microsoft.com/en-us/library/aa480210.aspx

http://msdn.microsoft.com/en-us/netframework/wcf-code-samples.aspx

http://msdn.microsoft.com/en-us/library/ms750530.aspx

Posted in C#, Uncategorized, WCF | Leave a Comment »

Configuration file for Gac ; Read confiuration file from GAC

Posted by Rajesh on April 25, 2010

Steps:

1) Create class library (Dal)

2) Generate strong name

3) Assign strong name with class library

4) Add config file(App.Config)

5) In App.Config File ,write

6) Compile and build class library

7) put assembly (Dal.dll) in Gac

8) go to run and Type c:\windows\assembly\gac_msil ,There is folder named as Dal will be appeared and within Dal folder there is folder naming like versionName_Publickey. Put Config file in this location c:\windows\assembly\gac_msil\Dal\ versionName_Publickey

How to read config file

public string GetConnectionString()

{

string strConfig;

String strDal = "";

string strAppPath = AppDomain.CurrentDomain.BaseDirectory;
string strAssPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

Configuration config2 = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

strDal = config2.AppSettings.Settings["SQLConnectionString"].Value;

if (ConfigurationManager.AppSettings["SQLConnectionString"] != null)

strConfig = ConfigurationManager.AppSettings["name"];

else

strConfig = "Unbale to read App.config File";

return strConfig;

}

Posted in C# | Leave a Comment »

Get Windows Service installed Path

Posted by Rajesh on April 25, 2010

using System;
using System.Management;
using System.ServiceProcess;
using System.IO;

namespace ConsoleApplication1
{
class Program
{

private static string _ServiceName = “TestService”;
static void Main(string[] args)
{

ServiceController[] srvControllers = ServiceController.GetServices();

foreach (ServiceController service in srvControllers)
{

if (service.ServiceName == _ServiceName)
{
Console.WriteLine(GetServicePath(_ServiceName));
Console.ReadLine();
}
}

//GoogleUpdaterService
}
public static string GetServicePath(string serviceName)
{
string strServiceName = “”;
using (ManagementObjectSearcher mos = new ManagementObjectSearcher(“SELECT PathName FROM Win32_Service WHERE Name = \”" + serviceName + “\”"))
{
foreach (ManagementObject mo in mos.Get())
{
strServiceName = Path.GetDirectoryName(mo["PathName"].ToString());
}
}

return strServiceName;
}
}

}

Posted in C# | Leave a Comment »

How to find assembly has assigned strong name or not?

Posted by Rajesh on May 10, 2008

The Assembly Name object contains information about an assembly, which you can use to bind to that assembly.

AssemblyName asName = null;

asName = AssemblyName.GetAssemblyName(“Path of Assembly”);

if (asName.GetPublicKey() != null)

{

Console.Write(“Assembly has assigned strong name”);

}

else

{

Console.Write(“Assembly has not assigned strong name”);

}

Posted in C# | Leave a Comment »

Use Tryparse to convert string value to integer

Posted by Rajesh on March 26, 2008

If you work on visual studio 2005. Don’t use int.prase because it throws an ArgumentNullException exception.
public static int IntTryParse(string values, int defaultValue)
{
int val = defaultValue;
if (int.TryParse(values, out val))
return val;
else
return defaultValue;

}

Posted in C# | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.