Wednesday, March 14, 2012
WCF is Case sensitive
Monday, September 20, 2010
SVN (Sub version) Export - remove svn files from code
I start my coding definitely with SVN, but later I moved to MS source safe(VSS). Since all my development I did with Visual studio, Source safe gave good support for me from the studio. But the poor performance of Source safe every time annoyed and it always brought that sweet thoughts of SVN. The big advantage here I've seen in Source safe was the easy isolation of Version control files and folders from the code files.
If you add your project to the SVN it'll add a "-svn" folder everywhere (inside every folder) in the project. So at the time of deployment everytime you have to go inside each and every folder and have to remove this svn folder(the reason for this svn removal is some time these files may create permission issues in the deployed server while manages with FTP). But in the case of Source safe everything manages with a single source file (project name.vss). So here things can do very easily.
When ever I do this file removal with SVN, I always would think about a simple solution probably hidden under SVN bunk. But truth is I never tried for that. The reason I again can tell most of my recent works I manages with VSS.
But recently, I was involving in big project deployment to a new server, the same concern came to me and I was really worried on how to remove these file from this big sized project which has thousands of folders under. I talked few of the senior people, but everyone has not much idea on SVN. Most of them worked only with VSS and they were well sounded in that(also I understood how much .Net people trust on their fully supported Source safe, even its poor performance ).
Instead of start my work I was sitting my seat, thinking. But the real truth once again got proved for me that "simplification will always happen whenever you approach things as simple". I simply discussed the context with one of my colleague who has comparatively less experienced there. He said he also doesn't have much knowledge in SVN, but in the past he heard some statements from his managers at the deployment time, they used the word "Export".
Yes, that was enough for me.. I jumped into my SVN context menu to know what is "export" or anything is there like that. Of course that was the answer... the perfect answer.. SVN Export will make your code isolated cleanly from the version control files.

Now situation is so calm. I went to cafeteria with my simple friend. :)
Wednesday, September 15, 2010
.Net MySql Connector
But I said, no there is something very straight forward like out "System.Data.SqlClient" or "System.Data.OracleClient" namespaces we have, those are available inbuilt with .Net Framework. So for this case I suppose this should be a matter of an assembly reference. And I tried, hardly searched, and atlast Google brought the result.
There is one connection "Mysql-Connector" assembly available in the "Mysql" site itself. From there you can download and as a reference to you project.
http://dev.mysql.com/downloads/connector/net/5.0.html
Now everything as usual, very straight forward . the code goes as like bellow.
//Add reference namespace
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//create connection object, command...
MySqlConnection Con = new MySqlConnection();
}
}
So we did it... once again I love .Net :)
Monday, August 30, 2010
Web service cross domain policy
This time workaround is not in the code its there in the Web service hosted server. In order to enable cross domain policy you have to add a "crossdomain.xml" file in the root of the application host. That is, either in your "wwwroot" or the folder where the root URL points (like if your application hosted in "http://myapplications.com/Webservice1", the cross domain policy xml file you have to locate is "http://myapplications.com/crossdomain.xml". Not inside of any the sub application folders ). Here is the plain simple crossdomain.xml content.
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"/>
</cross-domain-policy>
This MSDN reference makes it authenticated :)
Thats it!. Simple cool. So enjoy coding :)
Web service project host on remote server
Definitely I've to make sure that my Web service project is working perfectly, because everything depends on that. I hosted it and pulled the URL from my browser. It was nice to see that the web page lists all the WebMethods written to support my application. I clicked one of them to test from there itself that it is pulling data properly and outputs the exact XML. Page moved to the specific function test page to invoke and test the function(Thanks to .Net that we have very nice UI to test the Web service functions.)
But suddenly it shocked me that my "Invoke" button is missing instead it shows "The test form is only available for requests from the local machine". For a moment it made me mad because I've committed the entire application to the client before evening.
But nothing to worry I suppose, as usual google brought the quickest solution. It's just a matter of adding a protocol section in the web.config file. So see bellow it goes like this.
<configuration>
<system.web>
<webservices>
<protocols>
<add name="HttpGet">
<add name="HttpPost">
</add>
</add>
</protocols>
</webservices>
</system.web>
</configuration>
Thats it! now everything fine. Thanks for a cool week start :)
Friday, February 22, 2008
DataKeyNames
This is available in all of the data controls like GridView, DetailsView....etc. Most of the time we will assign the primary key of the data to this property . We can assign more that one data column value to this field, separated by a comma.This property need to be important at the time of updating a record from the data control.Now we can look one sample on how this datakeyNames were used in a DataGridView
DataKeyNames in GridView
Suppose I want to display an Employee details table data of a company. Here the user don't need to view the employee id in the table.But we were provided a checkbox on each rows corresponding to each employee, and a button provided bellow the Grid. On clicking this button, some action will take place for the checked rows data (here suppose we have to archive the checked row employee's data) . Then Our code will look like as bellow
Employee.aspx
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
DataKeyNames="emp_id">
<Columns>
<asp:BoundField DataField="fname" HeaderText="First name" SortExpression="fname" />
<asp:BoundField DataField="lname" HeaderText="Last name" SortExpression="lname" />
<asp:BoundField DataField="hire_date" HeaderText="Hire date" SortExpression="hire_date" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [emp_id], [fname], [lname], [hire_date] FROM [employee]">
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Archive" />
</div>
Employee.aspx.cs
public partial class Employee : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (((CheckBox)row.FindControl("CheckBox1")).Checked)
{
int EmployeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
//Archive the employee with this employee Id will goes from here
ArchiveEmployee(EmployeeID);
}
}
}
}
Multiple DataKeyNames
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
DataKeyNames="emp_id,dept_id">
<Columns>
<asp:BoundField DataField="fname" HeaderText="First name" SortExpression="fname" />
<asp:BoundField DataField="lname" HeaderText="Last name" SortExpression="lname" />
<asp:BoundField DataField="hire_date" HeaderText="Hire date" SortExpression="hire_date" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And in the Button1_Click event
Updating Data from DetailsView
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (((CheckBox)row.FindControl("CheckBox1")).Checked)
{
int EmployeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
int DepartementID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[1]);
}
}
}
It is important to set the dataKeyNames, while using ObjectDataSource to bind the details view.Here onclicking the Update button the ObjectDataSource will take Id value from the DataKeyNames of the DetailsView
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataSourceID="ObjectDataSource1" DefaultMode="Edit" DataKeyNames="Id">
<Fields>
<asp:TemplateField HeaderText="first name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("firstname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="DeleteCustomer"
SelectMethod="GetEmployeeById" TypeName="Employee
UpdateMethod="UpdateEmployee">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="firstname" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Sunday, October 14, 2007
Dynamically edit and encrypt the Web.Config sections in C# 2.0
Introduction
The web.config file concept is one, which made the asp.net application really on top in web developement technologies.Actually the coding styles in the asp.net program is that, like a teacher who preparing his student, feelings of a programmer who starts with asp.net.
At the bigining we were hardcoded the keywords(a common example is connection string) inside our code lines. Gradually we looked for a common place, and atlast dotnet brought that as web.config file.
Here I'm adding a small section (but really have big use in our application) of web.config management.
The article which give inspiration to write this was http://www.developerfusion.co.uk/show/6682/
To manipulate Web.Config contents in programatically we had to consider Web.Config file as a normal file or an xml file. .NET 2.0 provides many useful operations to be carried out on Web.Config file; like editing and encrypting sections of Web.Config file. This articles illustrates these functionalities .
Namespaces
The classes and methods to take control of the Web.Config are in 2 namespaces.
System.ConfigurationSystem.Web.Configuration
Each section in the Web.Config file has a corresponding class in either of the namespace. These classes allow modification of corresponding sections. The classes for sections within the "system.web" section are found in System.Web.Configuration. Classes for other sections that are not specific to Web.Config are found in System.Configuration.
Web.Config modification from the code
- Open Web.Config for editing using
WebConfigurationManagerclass. - Using respective
Configurationclass, bring about the necessary changes. - Save changes to the physical file using
Configurationclass.
private void UpdateConfig(string strKey, string strValue)
{
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
if (objAppsettings != null)
{
objAppsettings.Settings[strKey].Value = strValue;
objConfig.Save();
}
}
In the above piece of code, OpenWebConfiguration() method of WebConfigurationManager class opens Web.Config file in the root directory and returns it as a Configuration object. GetSection() method of Configuration class accepts path to a specific section as argument. The path is the relative path from the root node "configuration". You can refer to deeper nodes(sections in our context) by their names separated by '/'. For example, to get access to the "authentication" section, provide "system.web/authentication" as the parameter to GetSection() method. It returns a generic ConfigurationSecton object, which can be typecasted to proper configuration section class. In our example we get hold of the "appSettings" section with the help of AppSettingsSection class. AppSettingsSection class instance has a Settings collection property which contains application setting from the configuration section as key-value pairs. The Settings property can be indexed using key to get the corresponding value. You can also set the value property and call the Save() method of the Configuration object to write configurations in the Configuration instance to config file.
To delete an entry in the Web.config file: The Remove() method of Settings collection deletes an entry from the Configuration instance. Remove() method accepts key of the entry to be deleted.
Note: Please do not forget to call the Save() method of the Configuration instance to get the chanages reflected in the physical file.
objAppsettings.Settings.Remove("Location");
To iterate through all the key-value pairs in a configuration section, access the string array of keys via AllKeys property of Settings collection.foreach (string strKey in objAppsettings.Settings.AllKeys)
{
DataRow dr = dt.NewRow();
dr["Key"] = strKey;
dr["Value"] = objConfig.AppSettings.Settings[strKey].Value;
dt.Rows.Add(dr);
}
Encryption in Web.Config
Now comes the security issues. At times there comes the necessity for protecting sections of config file. In .NET 2.0 there are options available to encrypt sections of Web.config file programatically. The following method encrypts the "appSettings" section in Web.config file.
private void EncryptAppSettings()
{
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
if (!objAppsettings.SectionInformation.IsProtected)
{
objAppsettings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
objAppsettings.SectionInformation.ForceSave = true;
objConfig.Save(ConfigurationSaveMode.Modified);
}
}
The code above opens Web.Config file for modification. It then retrieves the "appSettings" section. The ProtectSection() method of SectionInformation class marks the configuration section for protection. It accepts the name of the protection provider to be used for the encryption. The ForceSave property indicates if the specified configuration section will be saved even if it has not been modified. Finally the Save() of the Configuration object writes the configuration settings to the Web.config file. The argument to the Save() method indicates the only properties modified need to be written to the physical file.
Decrypting sections of web.config file through code is very identical. The UnprotectSection() method of SectionInformation class removes the encryption from the configuration section.
private void DecryptAppSettings()This encrytion and decryption functionality can be applied to other sections of web.config file also. It comes in use mostly for "connectionStrings" section where usually the user name and password would be specified. This can done by creating a
{
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
if (objAppsettings.SectionInformation.IsProtected)
{
objAppsettings.SectionInformation.UnprotectSection();
objAppsettings.SectionInformation.ForceSave = true;
objConfig.Save(ConfigurationSaveMode.Modified);
}
}
ConfigurationSection object. An example for "connectionStrings" section is listed below. ConfigurationSection objConfigSection = objConfig.ConnectionStrings;
ConfigurationSection class represents a section within the configuration file. Configuration class has propertes for each configuration section. This property can be used to get respective ConfigurationSection objects. This is an alternative to the usage of GetSection() method of Configuration class.