Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, February 10, 2011

Assign Extension to application

There are two ways to assign extension to an application

1- on install time:
you can read about it on "The File Types Editor" part of below article:

How To Deploy .NET Windows Application

2- after running the application for a first time:

the below project is about this way

Code Project- Assign extension to a file


I prefer the first approach cause it's more standard

Sunday, February 7, 2010

3 Layer Programming, Data-Access Tutorial

Microsoft provide a very interesting tutorial about Data-Access.
you can find them in the below link:

Data Access Tutorials

Tuesday, November 17, 2009

How to: Enable/Disable Just-In-Time Debugging

To enable/disable Just-In-Time debugging

  1. On the Tools menu, click Options.

  2. In the Options dialog box, select the Debugging folder.

  3. In the Debugging folder, select the Just-In-Time page.

  4. In the Enable Just-In-Time debugging of these types of code box, select or clear the relevant program types: Managed, Native, or Script.

    To disable Just-In-Time debugging, once it has been enabled, you must be running with Administrator privileges. Enabling Just-In-Time debugging sets a registry key, and Administrator privileges are required to change that key.

  5. Click OK.

Reference:
Microsoft


Saturday, July 4, 2009

Send SMS

many time because of the special events (like: server crash,...);we want to send sms.
I found this article on the net about this subject!

How to send SMS messages from C# using an SQL database

Sunday, March 1, 2009

Vertical Label in ASP.NET

a simple code to change the label direction to vertival:

style="writing-mode:tb-rl;"

Monday, February 23, 2009

Polymorphism (C# Programming Guide)

From :

Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. Polymorphism is a Greek word that means "many-shaped" and it has two distinct aspects:
1. At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object's declared type is no longer identical to its run-time type.
2. Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed.
Virtual methods enable you to work with groups of related objects in a uniform way. For example, suppose you have a drawing application that enables a user to create various kinds of shapes on a drawing surface. You do not know at compile time which specific types of shapes the user will create. However, the application has to keep track of all the various types of shapes that are created, and it has to update them in response to user mouse actions. You can use polymorphism to solve this problem in two basic steps:
1. Create a class hierarchy in which each specific shape class derives from a common base class.
2. Use a virtual method to invoke the appropriate method on any derived class through a single call to the base class method.
First, create a base class called Shape, and derived classes such as Rectangle, Circle, and Triangle. Give the Shape class a virtual method called Draw, and override it in each derived class to draw the particular shape that the class represents. Create a List object and add a Circle, Triangle and Rectangle to it. To update the drawing surface, use a foreach loop to iterate through the list and call the Draw method on each Shape object in the list. Even though each object in the list has a declared type of Shape, it is the run-time type (the overridden version of the method in each derived class) that will be invoked.

Sunday, September 28, 2008

Multiline in tootip

you the below codes to add multiline tooltip for a button :

btnConvert.ToolTip = String.Format("This{0}is{0}a{0}multiline{0}tooltip", Environment.NewLine);

btnConvert.ToolTip = String.Format("YTD Excellence {0}\nYTD Overall {1}\nFeb Excellence {2}\nFeb Overall {3}",
"1",
"2",
"3",
"4"
);

Wednesday, August 27, 2008

Programmatically Creating Server Controls

// Create a new HtmlTable object.
HtmlTable table1 = new HtmlTable();
// Set the table's formatting-related properties.
table1.Border = 1;
table1.CellPadding = 3;
table1.CellSpacing = 3;
table1.BorderColor = "red";
// Start adding content to the table.
HtmlTableRow row;
HtmlTableCell cell;
for (int i = 1; i <= 5; i++)
{
// Create a new row and set its background color.
row = new HtmlTableRow();
row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
for (int j = 1; j <= 4; j++)
{
// Create a cell and set its text.
cell = new HtmlTableCell();
cell.InnerHtml = "Row: " + i.ToString() +
"
Cell: " + j.ToString();
// Add the cell to the current row.
row.Cells.Add(cell);
}
// Add the row to the table.
table1.Rows.Add(row);
}
// Add the table to the page.
this.Controls.Add(table1);

Monday, May 26, 2008

Send Mail

MailMessage msgMail = new MailMessage();
SmtpMail.SmtpServer = "192.xxx.xxx.xxx";
msgMail.To = y@p.com;
msgMail.Cc = s@p.com,j@p.com;
msgMail.Bcc = b@p.com;
msgMail.From = H@p.com;

msgMail.Subject = "ارسال توضيحات توسط پرسنل شماره " + lblPNo.Text;
//User.Identity.Name.Substring(15, User.Identity.Name.Length - 15);
msgMail.BodyFormat = System.Web.Mail.MailFormat.Html;
msgMail.BodyEncoding = System.Text.Encoding.UTF8;
string strBody =
"" +
" رزرو سالن جلسه " +
" " +
" " +
" " +
"
بسمه تعالي
به: اموراداري
به اطلاع مي رساند كارمند با شماره پرسنلي " +
"" + lblPNo.Text +
"
" + " توضيحاتي را براي شما ارسال نموده است. " +
"
" +
"
" +
"لطفا بررسي فرمائيد.
با تشكر " +
"
سيستم منابع انساني
";
msgMail.Body = strBody;
System.Web.Mail.SmtpMail.Send(msgMail);

Network User

You can find network user by this code:

User.Identity.Name.Substring(15, User.Identity.Name.Length - 15);

Tuesday, May 13, 2008

Solving out ASP.NET Enter key problem

Introduction
When I start programming with ASP.NET I used to face problems with enter key. Clicking enter key won't make a button click at all times. It will post back the form, but the relevant button you wanted to call won't be called. I read several articles which discuss this problem and tried some of the methods suggested by them. But nothing gave me a stable performance. So I created a library that can set default button for text box control when the enter key is pressed.

Identifying problem
Hitting enter key on ASP.NET pages will produce really different results. It varies on each browser. You can test it by placing one text box and one button on a webform. Write some code in button's onclick event. It could be some thing like

Response.write("You clicked this button");

Try debugging the program. Pressing enter key on the text box will make a postback. But whatever code you written inside button click won't be executed. This will work some time in firefox.

Then I found some ASP.NET developers was saying, "you can achieve this by putting one hidden field". So I placed something like this.



Start debugging again. Now pressing enter key will post form and execute the code for the button's onclick. Looks strange !

But that worked as temporary solution. This method can't be implemented when there is more than one button.

Assume you have two text boxes and two buttons. Hitting enter key from first text box should call first button and second text box should call second button. In this case hidden textbox method fails. It will call one button every time.

To solve this and make it work with all situations we can use javascript.

Using javascript for solution
txtBox1.Attributes.Add("onkeydown", "if(event.which event.keyCode){if ((event.which == 13) (event.keyCode == 13)) {document.getElementById('"+Button1.UniqueID+"').click();return false;}} else {return true}; ");

Adding the above attribute to the textbox will solve the problem. The keycode for enter key is 13. When a key is pressed in textbox it will check the keycode is 13. If it is 13 the relevant button will be clicked. event.keycode will not work with firefox, so we are using event.which.

Porting into DefaultButton class
I ported the same code into a class. Using this you can pass the textbox object and button object. It will set the necessary scripts for making enter key working. There are two static methods in DefaultButton class. One is for normal asp.net button and next is for image button.

To set default button for one text box follow these steps

Add reference to the library
Import System.Web.UI.DefaultButton namespace
Use DefaultButton.SetDefaultButton ( control , btButton )
This method will add keydown attribute for the textbox control and calls button when enter key is pressed.

Setting default buttons in ASP,NET 2.0
ASP.NET 2.0 comes with defaultbutton property. You can set default button for form and panels. This will be called when form is posting back using enter key.


source:http://www.codeproject.com/KB/aspnet/aspnet_Enter_key_problem.aspx









License

Sunday, May 11, 2008

Digit to String

This is procedure to convert no to string:


public static String ConvertDigitToString(string No, string Currency)
{
return ConvertDigitToString(Convert.ToInt64(No), Currency);
}
public static String ConvertDigitToString(Int64 No, string Currency)
{
string adad = "", bilion, miliard, million, thousand, digit, EnteredNo = No.ToString();
for (; EnteredNo.Length < 15; EnteredNo = "0" + EnteredNo) ;
bilion = EnteredNo.Substring(0, 3);
miliard = EnteredNo.Substring(3, 3);
million = EnteredNo.Substring(6, 3);
thousand = EnteredNo.Substring(9, 3);
digit = EnteredNo.Substring(12, 3);
if (bilion != "000")
{
adad = ReadDigit(bilion);
adad = adad + " ïéïيë ";
}
if (miliard != "000")
{
if (adad != "") adad = adad + "ي ";
adad = adad + ReadDigit(miliard);
adad = adad + " êïéïں©§ ";
}
if (million != "000")
{
if (adad != "") adad = adad + "ي ";
adad = adad + ReadDigit(million);
adad = adad + " êïéïيë ";
}
if (thousand != "000")
{
if (adad != "") adad = adad + "ي ";
adad = adad + ReadDigit(thousand);
adad = adad + " ىھں© ";
}
if (digit != "000")
{
if (adad != "") adad = adad + "ي ";
adad = adad + ReadDigit(digit);
}
return adad + " " + Currency;
}
private static String ReadDigit(string EnteredDigits)
{
string Sadgan, Dahgan, Yekan, adad = "";
Sadgan = EnteredDigits.Substring(0, 1);
Dahgan = EnteredDigits.Substring(1, 1);
Yekan = EnteredDigits.Substring(2, 1);
if (Convert.ToInt16(Sadgan) != 0)
{
switch (Convert.ToInt16(Sadgan))
{
case 1: adad = "ïè­§"; break;
case 2: adad = "§يï«¢"; break;
case 3: adad = "«ï­§"; break;
case 4: adad = "چىں© ­§"; break;
case 5: adad = "پںë­§"; break;
case 6: adad = "¬¬­§"; break;
case 7: adad = "ىه¢­§"; break;
case 8: adad = "ى¬¢­§"; break;
case 9: adad = "ëى­§"; break;
}
if (((Convert.ToInt16(Dahgan) != 0) (Convert.ToInt16(Yekan) != 0)) &&
(Convert.ToInt16(Sadgan) != 0)) adad = adad + " ي ";
switch (Convert.ToInt16(Dahgan))
{
case 1:
switch (Convert.ToInt16(Yekan))
{
case 0: adad = adad + "§ى"; break;
case 1: adad = adad + "ïںھ§ى"; break;
case 2: adad = adad + "§يںھ§ى"; break;
case 3: adad = adad + "«ïھ§ى"; break;
case 4: adad = adad + "چىں©§ى"; break;
case 5: adad = adad + "پںëھ§ى"; break;
case 6: adad = adad + "¬ںëھ§ى"; break;
case 7: adad = adad + "ىه§ى"; break;
case 8: adad = adad + "ى¤§ى"; break;
case 9: adad = adad + "ëيھ§ى"; break;
}
break;
case 2: adad = adad + " ï«¢"; break;
case 3: adad = adad + "«ï"; break;
case 4: adad = adad + "چىé"; break;
case 5: adad = adad + "پë¤ںى"; break;
case 6: adad = adad + "¬­¢"; break;
case 7: adad = adad + "ىه¢ں§"; break;
case 8: adad = adad + "ى¬¢ں§"; break;
case 9: adad = adad + "ëي§"; break;
}
if ((Convert.ToInt16(Yekan) != 0) &&
(Convert.ToInt16(Dahgan) != 0) &&
(Convert.ToInt16(Dahgan) != 1)
) adad = adad + " ي ";
switch (Convert.ToInt16(Yekan))
{
case 1: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "ïè"; break;
case 2: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "§ي"; break;
case 3: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "«ى"; break;
case 4: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "چىں©"; break;
case 5: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "پë¤"; break;
case 6: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "¬¬"; break;
case 7: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "ىه¢"; break;
case 8: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "ى¬¢"; break;
case 9: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "ëى"; break;
}
}
else
if (Convert.ToInt16(Dahgan) != 0)
{
switch (Convert.ToInt16(Dahgan))
{
case 1:
switch (Convert.ToInt16(Yekan))
{
case 0: adad = adad + "§ى"; break;
case 1: adad = adad + "ïںھ§ى"; break;
case 2: adad = adad + "§يںھ§ى"; break;
case 3: adad = adad + "«ïھ§ى"; break;
case 4: adad = adad + "چىں©§ى"; break;
case 5: adad = adad + "پںëھ§ى"; break;
case 6: adad = adad + "¬ںëھ§ى"; break;
case 7: adad = adad + "ىه§ى"; break;
case 8: adad = adad + "ى蠟ى"; break;
case 9: adad = adad + "ëيھ§ى"; break;
}
break;
case 2: adad = adad + " ï«¢"; break;
case 3: adad = adad + "«ï"; break;
case 4: adad = adad + "چىé"; break;
case 5: adad = adad + "پë¤ںى"; break;
case 6: adad = adad + "¬­¢"; break;
case 7: adad = adad + "ىه¢ں§"; break;
case 8: adad = adad + "ى¬¢ں§"; break;
case 9: adad = adad + "ëي§"; break;
}
if ((Convert.ToInt16(Yekan) != 0) && (Convert.ToInt16(Dahgan) != 1)) adad = adad + " ي ";
switch (Convert.ToInt16(Yekan))
{
case 1: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "ïè"; break;
case 2: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "§ي"; break;
case 3: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "«ى"; break;
case 4: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "چىں©"; break;
case 5: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "پë¤"; break;
case 6: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "¬¬"; break;
case 7: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "ىه¢"; break;
case 8: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "ى¬¢"; break;
case 9: if (Convert.ToInt16(Dahgan) != 1) adad = adad + "ëى"; break;
}
}
else
{//Convert.ToInt16(Dahgan)=0
switch (Convert.ToInt16(Yekan))
{
case 1: adad = adad + "ïè"; break;
case 2: adad = adad + "§ي"; break;
case 3: adad = adad + "«ى"; break;
case 4: adad = adad + "چىں©"; break;
case 5: adad = adad + "پë¤"; break;
case 6: adad = adad + "¬¬"; break;
case 7: adad = adad + "ىه¢"; break;
case 8: adad = adad + "ى¬¢"; break;
case 9: adad = adad + "ëى"; break;
}
}
return adad;
}