Thursday, February 25, 2010

Hide a Base class property in Inherited class (Custom Control)

In case you are extending a control and you want your extended control not to display the Base control's (class) property in the designer view then you should be follwing this steps.

Example:
-----------
Suppose you are extending the TreeView Control and you want the TreeView Control's ShowCheckBoxes property to be hidden in the extended Control. Here are the steps you should follow to establish that.

1> Create a Class,i.e, TreeViewExtender, inherit it from TreeView Class.
2> Create a property with the same Name and ReturnType as it is in the Base Class.
3> Use the new operator whch is used to explicitly hide a member inherited from a base class. (To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.)
4> Use the BrowsableAttribute and set its value to false.

public class TreeViewExtender : TreeView
{
#region Properties

[Browsable(false)]
public new TreeNodeTypes ShowCheckBoxes
{
get
{
return base.ShowCheckBoxes;
}
set
{
base.ShowCheckBoxes = value;
}
}

#endregion
}

Note: The [Browsable(false)] is for whether it shows up in the designer property grid or not but it would be accessed from the editor or code behind.
In order to stop the assignment to this property make it a read-only property by removing the setter from the Property in the Derived Class.

Monday, January 4, 2010

Google Transliteration IME Softwares

One tool which will help us express better now ;)

http://www.google.com/ime/transliteration/
Read this page 'bout how to enable this in your Operating System: http://www.google.com/ime/transliteration/help.html

Alternative:
----------------
Another alternative by Google. By using Browser Bookmark ONLY. No installation required.

This works If you are having internet connectivity and can be turned on for ANY page. Read more here:
http://t13n.googlecode.com/svn/trunk/blet/docs/help_hi.html

Tuesday, December 15, 2009

Problem Importing Bulk Typed Objects from WCF or Problem getting huge list using WCF

If you are experiencing problem referencing a WCF Web Service from Silverlight which transmits bulk Data in terms of List then you have to tweak ur configuration a little in order to make our application run.

Suppose you have a silverlight application having a DropDownlist which would display 1/2 million names of a Type,say Person.

Your Wcf application is having a service contact that you pass 1/2 a million Types to the SilverLight application, eg: List GetAllPersons();

List personList=new List();
So you have a list containing 1/2 a million objects of Type and each having the details of TYpe.

The web.Config of the WCF Service Application should have BindingConfiguration and that configuration should possess these properties transferMode="Streamed",maxBufferSize="65536",maxReceivedMessageSize="204003200"'.
Here the Streamed transfer mode would help us to transit Bulk Typed Objects from WCF Service.

<'system.serviceModel'>
<'behaviors'>
<'serviceBehaviors'>
<'behavior name="ServiceBehavior1"'>
<'serviceMetadata httpGetEnabled="true"' />
<'serviceDebug includeExceptionDetailInFaults="false"' />
<'/behavior'>
<'/serviceBehaviors'>
<'/behaviors'>
<'services'>
<'service behaviorConfiguration="ServiceBehavior1" name="PersonInfo"'>
<'endpoint address="" binding="basicHttpBinding" bindingConfiguration="StreamBinding"
contract="IPersonInfo"'>
<'identity'>
<'dns value="localhost"' />
<'/identity'>
<'/endpoint'>
<'endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"' />
<'/service'>
<'/services'>

<'bindings'>
<'basicHttpBinding'>
<'binding name="StreamBinding"
transferMode="Streamed"
maxBufferSize="65536"
maxReceivedMessageSize="204003200"' />



Saturday, November 28, 2009

Method to check if Value exists in a Column of a Table, where all three (TableName,ColumnName and ColumnValue) are parameters

When working with LINQ to SQL you might get stuck with a problem when required to check if Value exists in a Column of a Table, where all three (TableName,ColumnName and ColumnValue) are parameters. If that Table is mapped/dropped in the .dbml file then with the help of this method would actually get the solution.

The code does the following:
----------------------------------------------------------------------------------
Instantiated an object of DataContext by passing the ConnectionString.
Checked if the Table exists, within all the Tables of the DataContext.
If its found, then checked if Column exists within the Columns of the Table.
If its found, then called a Generic Method of the Type Table and checked if the Value exists in that Column of that Table.

Solve:
-----------------------------------------------------------------------------------

-----------------------------------------------------------------------------------
using System.Web;
using System.Configuration;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Data.Entity;

///
/// Summary description for Utility Class.
///

public class Utility
{
DataClassesDataContext db = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["DbConnectionString"].ConnectionString);

public bool GetResult(string tableName, string columnName, string val)
{
var metaModel = db.Mapping;
//Checking if the TableName exists within the Tables of the .dbml file
MetaTable table = metaModel.GetTables().FirstOrDefault(t => t.TableName.ToLower().Contains(tableName.ToLower().Trim()));
if (table != null)
{
//Checking if the ColumnName exists within all the Columns of the MetaTable
MetaDataMember column = table.RowType.DataMembers.FirstOrDefault(clm => clm.MappedName.ToLower().Equals(columnName.ToLower().Trim()));
if (column != null)
{
//Get the Type of the Table from the TableName
Type type = Type.GetType(tableName);

//Invoking a generic Method
MethodInfo method = typeof(Utility).GetMethod("ValueExists");
MethodInfo genericMethod = method.MakeGenericMethod(type);
object[] parameters = new object[] { db, columnName, val };
return (bool)genericMethod.Invoke(this, parameters);
}
}
return false;
}

public static bool ValueExists(DataContext database, string columnName, string columnValue) where T : class
{
//check to see whether value existed in the column of the Table T
T obj = database.GetTable().Where(t =>
t.GetType().GetProperty(columnName).GetValue(t, null).ToString() == columnValue).FirstOrDefault();

return obj == null ? false : true;
}
}

Wednesday, November 18, 2009

Get data from StoredProc returning Table using Linq2Sql

I was using the Linq to Sql to execute a stored procedure, returning results by using a temporary table.
When I dropped the procedure on to the .dbml file, it generated a function with the return value typed as Integer.

The Stored Procedure looks like this:
--------------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE procedure [dbo].[test_proc]
as
begin

create table #temp_table
(
col1 varchar(255),
col2 varchar(255)
)

insert into #temp_table (col1, col2) values ('ram', 'shyam')
select * from #temp_table
end
go

My solution to this problem was:
---------------------------------
I created a partial class as under. You have to use the namespace System.Data.Linq.Mapping for the Table and Column tags.

[Table]
public partial class test_proc_Result
{
public test_proc_Result() { }

[Column]
public string col1;
[Column]
public string col2;
}

and then you have to modify the Function in the DBML's .designer.cs file and you r done.

[Function(Name="dbo.test_proc")]
public IEnumerable<'test_proc_Result'> test_proc()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return result.ReturnValue as IEnumerable<'test_proc_Result'>;
}

Calling function :
-------------------
List<'test_proc_Result'> result = db.test_proc().ToList();

You would find the values passed from the Store_proc in the result variable.
In case you are returning any scalar value such as BIT from the stored procedure.
Then the function should be changed like this:

[Function(Name="dbo.test_proc")]
public ISingleResult<'test_proc_Result'> test_proc()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return result.ReturnValue as ISingleResult<'test_proc_Result'>;
}


Note: Please remove all " ' " from the code

Monday, October 12, 2009

Get the Identity of the record using ADO.NET

Suppose you require to get the IDENTITY of the record just after inserting it in Database,using ADO.NET, using ExecuteNonQuery() command. You want ur sql_storedprocedure not to return the IDENTITY. IN that case you need to write the code as below:

//C#
string employeeName="developer";
string query = "Insert Into Employees(EmployeeName) Values (?)";
string query2 = "Select @@Identity";

int ID;
string connect = "Data Source=(local);Initial Catalog=TestDB;User ID=sa;Password=sa123";
using (SqlConnection conn = new SqlConnection(connect))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("", employeeName);
conn.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = query2;
ID = (int)cmd.ExecuteScalar();
}
}

Thursday, September 10, 2009

Create a Bing Maps account (Bing Silverlight Map Control)

Visit https://www.bingmapsportal.com.

Click the link "Create a Bing Maps account". You would land up opening the https://login.live.com, where you have to Login using the Windows Live ID.
Once you are done with this, you would be presented the Accout Details sceen on the bingmapsportal Site.Input all the fields as desired.

Click the link "Create or view Bing Maps keys" and input the ApplicationName and the URl.Note, the application running on you localhost and on the live server should have difference Credential/Key.

Once that is done copy the generated key and paste it as the CredentialsProvider of the Bing Silverlight Map COntrol. You can now view the Map on your Bing Silverlight Map Control.

CredentialsProvider="AmInmiEJcmmrHZ238dWFiT1Y3-nEtt2c2D0_jFvCdACSDzAyAsm3081KMVpIcefB" x:Name="map1"