Monday, November 2, 2009

Editable ListBox User Control

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

using System.Data;

namespace ExByTejas

{

public class EditableListBox : ListBox

{

TextBox editBox = new TextBox();

public EditableListBox()

{

editBox.Location = new System.Drawing.Point(0, 0);

editBox.Size = new System.Drawing.Size(0, 0);

editBox.Hide();

this.Controls.AddRange(new Control[] {this.editBox});

editBox.Text = "";

editBox.BackColor = Color.Beige;

editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);

editBox.LostFocus += new System.EventHandler(this.FocusOver);

}

protected override void OnKeyPress(KeyPressEventArgs e)

{

if (e.KeyChar == 13)

CreateEditBox();

base.OnKeyPress(e);

}

protected override void OnDoubleClick(EventArgs e)

{

CreateEditBox();

base.OnDoubleClick(e);

}

protected override void OnKeyDown(KeyEventArgs e)

{

if (e.KeyData == Keys.F2)

CreateEditBox();

base.OnKeyDown(e);

}

protected void CreateEditBox()

{

Rectangle r = this.GetItemRectangle(this.SelectedIndex);

string itemText = string.Empty;

if(this.DataSource == null)

itemText = (string)this.Items[SelectedIndex];

else

itemText = this.Text;

editBox.Location = new System.Drawing.Point(r.X, r.Y);

editBox.Size = new System.Drawing.Size(r.Width - 10, r.Height);

editBox.Show();

this.Controls.AddRange(new System.Windows.Forms.Control[] {this.editBox});

editBox.Text = itemText;

editBox.Focus();

editBox.SelectAll();

editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);

editBox.LostFocus += new System.EventHandler(this.FocusOver);

}

protected void FocusOver(object sender, System.EventArgs e)

{

UpdateEditedValue();

}

protected void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

if (e.KeyChar == 13)

UpdateEditedValue();

}

private void UpdateEditedValue()

{

if (this.DataSource == null)

{

this.Items[SelectedIndex] = editBox.Text;

editBox.Hide();

}

else

{

DataRowView rowView = (DataRowView)this.SelectedItem;

rowView.Row[this.DisplayMember] = editBox.Text;

editBox.Hide();

}

}

}

}

Monday, September 7, 2009

Put HTML page break for printing

you can use any of the following style for HTML page break (specially for printing)
1) style="page-break-after:always"
2) style="page-break-before:always"

Friday, September 4, 2009

Single border around HTML Table cell

style="border-collapse:collapse"

Borders for the cells are generally displayed independently from each other. so each cell has its own border and that's what border looks like double for the whole table. we can collapsed this double border into single border with the border-collapse style property, check the above example.

Select Distinct Column Values From DataTable

DataView dv= new DataView(SourceTable);
DataTable dt = dv.ToTable(true, "Col1", "Col2","Col3" ...);

Wednesday, September 2, 2009

get mouse position in c#

I might be wrong but you can try following two

Point p = Cursor.Position;
Point p2 = Control.MousePosition;

Tuesday, August 25, 2009

Add Custom Property into DataGridViewTextBoxColumn

public class CustomDataGridViewTextBoxColumn : DataGridViewTextBoxColumn
{
private bool _displayContextMenu = false;
public bool DisplayContextMenu
{
set { _displayContextMenu = value; }
get { return _displayContextMenu ; }
}

public override object Clone()
{
CustomDataGridViewTextBoxColumn myCol = (CustomDataGridViewTextBoxColumn)base.Clone();
myCol.DisplayContextMenu = _displayContextMenu ;
return myCol;
}
}

Note: When you derive from DataGridViewTextBoxColumn and add new properties to the derived class, be sure to override the clone method to copy the new properties during cloning operations. You should also call the base class's clone method so that the properties of the base class are copied to the new cell.

Thursday, July 2, 2009

Create fixed size string for CSV file in C#

public static string GetFixedLengthString(string input, int length)
{
string result = string.Empty;
if (string.IsNullOrEmpty(input))
{
result = new string(' ', length);
}
else if (input.Length > length)
{
result = input.Substring(0, length);
}
else
{
result = input.PadRight(length);
}
return result;
}

Wednesday, June 24, 2009

bitvector32 vs bool in C#

In application sometime you need more boolean fields.Also that memory usage and performance is main concern.

For example you require 50 boolean fields. If you create 50 boolean variable like bool b1,b2,b3....b50; or create array of 50 than bool[] b = new bool[50]; then also each bool veriable require 1 byte. so total space usage 50 * 1 = 50bytes.

If you use bitvector32 instead of creating array or 50 variable then it just require 4byte. Each bitvector32 can hold upto 32 different flags. so for 50 we just need two bitvector32,and space usage reduce to 8 byte instead of 50 byte.

BitArray is also provide functionality like bitvector32 but it is class so it overhead with instance of class, but bitvector32 is simple structure so it reduce that over head too.
Example:
BitVector32 bv = new BitVector32(0); This will assign all bits to 0, as 0 binary representation is 00000000000000000000000000000000.
BitVector32 bv = new BitVector32(-1); This will assign all bits set to 1. As –1 binary representation is 11111111111111111111111111111111. (Two’s complement).

Friday, May 29, 2009

Concurrency Handling

The DataAdapter and optimistic concurrency

Before we get into some of the methods for handling concurrency issues in updating a database with ADO.NET, let's talk a little about the DataAdapter.

When you are in Visual Studio .NET, you can use the Data Adapter Configuration Wizard to generate all the Command objects in the DataAdapter. This simplifies operations, although you are free to generate these commands yourself. In addition, you can also use the Commands generated by the wizards as the basis for customized Stored Procedures, which is hopefully the direction you would want to take things in the first place.

What is often not obvious to developers is that, by default, the Data Adapter Configuration Wizard generates SQL statements that check for optimistic concurrency violations. Optimistic concurrency means that you assume no one will be making edits to a record while you are making your edits. Because you are "optimistic" that two people will not edit the same record simultaneously, you do not apply a lock to the record as soon as a user starts to edit it. Instead, you apply the lock only when the actual update is attempted. This results in a much shorter lock, and in database terms, this is good. Locks are smart; but long locks are not smart.

You can see this behavior by starting a new project in VS.NET and dragging a SqlDataAdapter or OleDbDataAdapter onto a designer surface. If you step through the wizard and choose to use SQL statements, the next page of the wizard will allow you to type in the Select statement. This page has a button labeled "Advanced Options". Clicking this button opens the Advanced SQL Generation Options dialog box, and by default, the option for using optimistic concurrency is turned on.

To check for optimistic concurrency violations, the Data Adapter Configuration Wizard writes SQL statements that verify the record you are about to update or delete has not changed since you originally loaded it into your DataSet. The Wizard does this by adding a rather large WHERE clause to the SQL statement to verify that it is updating or deleting an exact match of what was downloaded and placed in the DataSet. However, this also relies on another aspect of the DataSet: DataTables can store multiple versions of each row. Each DataTable in a DataSet is made up of DataRow objects. Each record in the table can actually exist multiple times, where each record has a different DataRowVersion. The ones that come into play with optimistic concurrency are Current and Original.

When the DataAdapter first populates your DataSet with records, each DataRow is given a DataRowVersion of Original. If you then edit a record, the changes are stored in a new version of the record with a DataRowVersion of Current. Now, you have both the Original version (from the database) and the Current version (the changes you want to make). It is called the Current version because it is the current version of the data in your DataSet, but, of course, it is not yet the current version in the underlying database.

The Four Basic Concurrency Handling Options

At this point, you have four basic optimistic concurrency options in SQL update queries:

1) Include Only the Primary Key Columns

You can include only the primary columns in the SQL UPDATE and DELETE queries. This creates a "last in wins" updating scenario. Both update attempts will succeed. Obviously, the database is not going to maintain both sets of changes. The changes made by the last update will override the previous changes.

The "last in wins" scenario is not appropriate if you want to prevent users from unwittingly overwriting other users' changes.

The CommandBuilder object does not offer this optimistic concurrency option; the Visual Studio.Net Data Adapter Configuration Wizard does. On the Advanced Options tab, you would deselect the "Use Optimistic Concurrency" check box.

2) Include All Columns in the WHERE Clause

As mentioned above, the default behavior of both the CommandBuilder and the Data Adapter Configuration Wizard is to include all the columns in the WHERE clause. This prevents your code from overwriting changes made by other users between the time your code retrieves the row and the time your code attempts to submit the pending change in the row.

Because the value of another user's updated column for a row of data has changed in the database, no row in the table satisfies all the criteria in the query's WHERE clause. Thus, the database does not modify the row. The DataAdapter queries the database to determine how many rows the query modified, discovers that the query did not successfully update the desired row, and marks the DataRow accordingly.

This is the concurrency option that the CommandBuilder object uses, and as explained, the Data Adapter Configuration Wizard uses this concurrency option by default.

You control how the DataAdapter responds to a failed update attempt by using the ContinueUpdateOnError property. By default, this property is set to False, which means that the DataAdapter will throw a DBConcurrencyException when it encounters a failed update attempt. If you want the DataAdapter to attempt to submit the remaining changes, you would set its ContinueUpdateOnError property to True.

If you set this property to True and one or more of the update attempts fail, the DataAdapter will not throw an exception. When the DataAdapter encounters a failed update attempt, it will set the HasErrors property of the corresponding DataRow object to True and set the RowError property of the DataRow to the concurrency error message. You can then check the HasErrors property of your DataSet or DataTable after calling DataAdapter.Update to determine whether any of the update attempts failed, and decide how your application should handle this business logic for the user.

Some developers will want to submit changes in a transaction and commit the changes only if all update attempts succeed. In such scenarios, you'll probably want to leave the ContinueUpdateOnError property set to its default value of False and roll back the transaction if the Update method throws an exception.

3) Include the Primary Key and Timestamp Columns

You can simplify the WHERE clause of your query-based updates by relying on timestamp columns. The SQL Server timestamp column does not actually contain date and time information. Instead, it contains binary data that's unique within the database.
You can define a timestamp column on your SQL Server table, and any time the contents of a row changes, SQL Server will modify the value of the timestamp column for that row. We could add a timestamp column to the Northwind Customers table and make our query look like this:
UPDATE Customers SET CustomerID = 'ALFKI', CompanyName = 'Original Company Name', ContactName = 'New Contact', Phone = '901-345-9856' WHERE CustomerID = 'ALFKI' AND TimestampColumn = 0x00000000000000CC Because the server will generate a new value for the timestamp column each time it updates a row, you can use a combination of the primary key and timestamp columns in the WHERE clause of your query-based updates to ensure that you don't overwrite another user's changes.
Neither the CommandBuilder nor the Data Adapter Configuration Wizard supports generating updating logic using this optimistic concurrency strategy.
NOTE: As of SQL Server 2000, "rowversion" is now synonymous with the timestamp data type. The SQL Server documentation recommends using the "rowversion" keyword instead of timestamp.
This option provides a much simpler updating logic, and the database has fewer columns to examine per update attempt.

4) Include the Primary Key Columns and Modified Columns

By default, the ADO cursor engine includes only the primary key columns and the original values of modified columns in the WHERE clause of its query-based updates. The cursor engine also includes only the modified columns in the SET clause of UPDATE queries.

Let's look at a multi-user example using this updating strategy. Let's say that User A and User B retrieve the same row of customer data at the same time. They each modify a different column of data -- User A changes the CompanyName column, and User B changes the ContactName column. User B submits the pending change to the ContactName column first. User B's UPDATE query looks like this:

UPDATE Customers SET ContactName = 'New Contact' WHERE CustomerID = 'ALFKI' AND ContactName = 'Original Contact'
User A then submits the pending change to the CompanyName column using the following UPDATE query:

UPDATE Customers SET CompanyName = 'New Company Name' WHERE CustomerID = 'ALFKI' AND CompanyName = 'Original Company Name'
The contents of the row will change from :

CustomerID CompanyName ContactName ---------- --------------------- ---------------- ALFKI Original Company Name Original Contact
to

CustomerID CompanyName ContactName ---------- --------------------- ---------------- ALFKI Original Company Name New Contact
and finally to

CustomerID CompanyName ContactName ---------- --------------------- ---------------- ALFKI New Company Name New Contact
Both updates will succeed, and the change made by User A will not overwrite changes made by User B.

The structure of the ADO.NET DataAdapter does not lend itself to this updating strategy because it requires that you change the structure of the query based on the columns that have been modified in the row that contains the pending change. The DataAdapter supplies values for the parameters in its query-based updates on a row-by-row basis, but it does not modify the actual structure of the parameterized query.

You could theoretically write code to dynamically change the structure of the appropriate Command object and use that code while handling the DataAdapter object's RowUpdating event. However, while this updating strategy has benefits, the costs are too high.

Submitting Updates in Transactions

What if you want to submit all of your updates as a single unit so that either all of the updates succeed or none of them do? The simple answer is to wrap your updates in a transaction. But, there is a problem -- the DataAdapter does not expose a Transaction property.

The DataAdapter does not actually submit the updates. It hands the work off to the Command objects in its UpdateCommand, InsertCommand, and DeleteCommand properties. The Command objects expose a Transaction property, so in order to submit the changes using the DataAdapter, you must set the Transaction property of the Command objects that the DataAdapter will use.

The following code shows one way to accomplish this task:

string strConn, strSQL; strConn = "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=Northwind;Trusted_Connection=Yes;"; strSQL = "SELECT OrderID, ProductID, Quantity, UnitPrice FROM [Order Details] WHERE OrderID = 11511" + " ORDER BY ProductID"; DataTable tbl = new DataTable(); OleDbConnection cn = new OleDbConnection(strConn); OleDbDataAdapter da = new OleDbDataAdapter(strSQL, cn); cn.Open(); da.Fill(tbl); //Modify the contents of the DataTable. //Create a new transaction. OleDbTransaction txn = cn.BeginTransaction(); //Set the Transaction property of the DataAdapter's Commands. da.UpdateCommand.Transaction = txn; da.InsertCommand.Transaction = txn; da.DeleteCommand.Transaction = txn; //Submit the changes. da.Update(tbl); //Commit the changes and close the connection. txn.Commit(); cn.Close();

It's more difficult to submit changes in a transaction if you're relying on the CommandBuilder object to generate your updating logic. The CommandBuilder does not actually generate the updating logic when you instantiate it. If you instantiate a CommandBuilder object and later call DataAdapter.Update , the CommandBuilder will not actually build the updating logic until you call the DataAdapter object's Update method. This behavior poses a slight problem if you want to use the CommandBuilder to submit changes in a transaction.

If you add the following line of code just before the call to the DataAdapter object's Update method, your code will succeed:

da.SelectCommand.Transaction = txn

So this summarizes the 4 major approaches to handling concurrency in a mult-user database update scenario using ADO.NET, specifically focusing on the DataAdapter. Hopefully this will provide you with the basis for further study, and some additional ideas on how you may want to structure your database updating logic for your application.

Display nothing instead of cross image mark into DataGridViewImageColumn

if there are some cases into datagridviewimagecolumn like need to display image into first 3 rows and the remianing image column should be empty instead red cross image mark.

set the defaultcellstyle nullvalue property to NULL

for ex:
DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
imgCol.DefaultCellStyle.NullValue = null;

Friday, April 10, 2009

Model View Presenter (MVP) pattern

How large for a single SharePoint content database?

Inspecting The SharePoint Content Database

Office 2000 Compatibility with VSTO-Generated Document

Information about designing Office add-ins by using the .NET Framework

Download Office XP PIAs

2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies

Deployment of Managed COM Add-Ins in Office XP

Virtual Earth Platform Developer Account

Sign Up for the Google Maps API

Know your geo position

Integrating MapPoint Web Services with Virtual Earth

Google Maps API Reference

MapPoint Web Service Software Development Kit (SDK) version 4.5

Getting Started with MapPoint Web Service using .NET Compact Framework

Calculate distance, bearing and more between two Latitude/Longitude points

Wednesday, April 1, 2009

ComboBox with additional properties for setting the size of the AutoComplete Drop-Down window

/* Represents an ComboBox with additional properties for setting the
size of the AutoComplete Drop-Down window. */
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;


public class ComboBoxExtend : ComboBox
{
private int acDropDownWidth = 170;
private int acDropDownHeight = 106;
private const UInt32 CB_ADDSTRING = 0x85;
public ComboBoxExtend(): base()
{
}
//
/// Provides an encapsulation of an Auto complete drop down window
/// handle and window proc.
///
private class ACWindow : NativeWindow
{
private ComboBoxExtend owner;
private static Dictionary ACWindows = null;
private static List owners = null;

#region "Win API Declarations"
private const UInt32 WM_WINDOWPOSCHANGED = 0x47;
private const UInt32 WM_NCDESTROY = 0x82;
private const UInt32 SWP_NOSIZE = 0x1;
private const UInt32 SWP_NOMOVE = 0x2;
private const UInt32 SWP_NOZORDER = 0x4;
private const UInt32 SWP_NOREDRAW = 0x8;
private const UInt32 SWP_NOACTIVATE = 0x10;

private const UInt32 GA_ROOT = 2;
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public Point Location
{
get { return new Point(this.Left, this.Top); }
}
}
private delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);

[DllImport("user32.dll")]
private static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);

[DllImport("user32.dll")]
private static extern IntPtr GetAncestor(IntPtr hWnd, UInt32 gaFlags);

[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();

[DllImport("user32.dll")]
private static extern void GetClassName(System.IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);

[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
#endregion
static ACWindow()
{
ACWindows = new Dictionary();
owners = new List();
}
///
/// Creates a new ACWindow instance from a specific window handle.
///

private ACWindow(IntPtr handle)
{
this.AssignHandle(handle);
}
//public static void test()
//{
// ACWindows = new Dictionary();
// owners = new List();
//}
public static void DestroyOwner(ComboBoxExtend owner)
{
if ((owners.Contains(owner)))
{
owners.Remove(owner);
}
}
///
/// Registers a ComboBoxEx for adjusting the Complete Dropdown window size.
///

public static void RegisterOwner(ComboBoxExtend owner)
{
if ((owners.Contains(owner)))
{
return;
}
//for (int i = 0; i < name ="=">
/// This callback will receive the handle for each window that is
/// associated with the current thread. Here we match the drop down window name
/// to the drop down window name and assign the top window to the collection
/// of auto complete windows.
///
private static bool EnumThreadWindowCallback(IntPtr hWnd, IntPtr lParam)
{
if ((GetClassName(hWnd) == "Auto-Suggest Dropdown"))
{
IntPtr handle = GetAncestor(hWnd, GA_ROOT);
if ((!ACWindows.ContainsKey(handle)))
{
ACWindows.Add(handle, new ACWindow(handle));
}
}
return true;
}
///
/// Gets the class name for a specific window handle.
///

private static string GetClassName(IntPtr hRef)
{
System.Text.StringBuilder lpClassName = new System.Text.StringBuilder(256);
GetClassName(hRef, lpClassName, 256);
return lpClassName.ToString();
}
///
/// Overrides the NativeWindow's WndProc to handle when the window
/// attributes changes.
///

protected override void WndProc(ref System.Windows.Forms.Message m)
{
try
{
if ((m.Msg == WM_WINDOWPOSCHANGED))
{
// If the owner has not been set we need to find the ComboBoxEx that
// is associated with this dropdown window. We do it by checking if
// the upper-left location of the drop-down window is within the
// ComboxEx client rectangle.
if ((owner == null))
{
Rectangle ownerRect = default(Rectangle);
RECT acRect = new RECT();
foreach (ComboBoxExtend cbo in owners)
{
GetWindowRect(this.Handle, ref acRect);
ownerRect = cbo.RectangleToScreen(cbo.ClientRectangle);
if ((ownerRect.Contains(acRect.Location)))
{
this.owner = cbo;
break; // TODO: might not be correct. Was : Exit For
}
}
owners.Remove(this.owner);
}
if (((owner != null)))
{
SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, owner.AutoCompleteDropDownWidth, owner.AutoCompleteDropDownHeight, SWP_NOMOVE SWP_NOZORDER SWP_NOACTIVATE);
}
}

if ((m.Msg == WM_NCDESTROY))
{
ACWindows.Remove(this.Handle);
}
base.WndProc(ref m);
}
catch
{
}
}
}

}