Search Results:

Monday, September 28, 2009

Connection Strings Made easy .

Visit this website for various connection strings such as to connect to Access,Excel etc...

Click Here


Once you get the connection string it is similar to any other database access. use ole db connection objects.

Friday, September 25, 2009

Get Open Dialog to browse system files

The OpenFileDialog class is defined in Windows.Forms class. You need to add to reference to System.Windows.Form.dll library and call using System.Windows.Forms before using the class.

The OpenFileDialog class can be used to open a file similar to CFileDialog 's Open method in VC++. This class is derived from FileDialog. OpenFile method of this class opens a file which can be read by a steam.

In this sample code, I have use OpenFileDialog class to browse a file.

Private fdlg As OpenFileDialog = New OpenFileDialog()
Private fdlg.Title = "C# Corner Open File Dialog"
Private fdlg.InitialDirectory = "c:\"
Private fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
Private fdlg.FilterIndex = 2
Private fdlg.RestoreDirectory = True
If fdlg.ShowDialog() = DialogResult.OK Then
textBox1.Text = fdlg.FileName
End If

Title member let you set the title of the open dialog. Filter member let you set a filter for types of files to open.

Sunday, September 13, 2009

Delete Duplicate Rows in SQL Server.

Deleting duplicate rows in SQL Server

--1.Deleting rows from a table having primary key

CREATE TABLE TEST1
( ID INT NOT NULL PRIMARY KEY
,VALUE CHAR(2)
)

INSERT INTO dbo.TEST1 SELECT '1','A' UNION ALL SELECT '2','A' UNION ALL SELECT '3','A' UNION ALL SELECT '4','B' UNION ALL SELECT '5','B' UNION ALL SELECT '6','B'

The original table with duplicates

DELETE FROM dbo.TEST1 WHERE ID NOT IN (SELECT MIN(ID) FROM dbo.TEST1 GROUP BY VALUE)

Now the resulting table with no duplicate rows

2. Deleting rows from a table without a primary key

I have a table dbo.MYDATABASE with following data---


now deleting the duplicate rows using ROW_NUMBER()


WITH TEMP_TABLE AS ( SELECT ROW_NUMBER() OVER( PARTITION BY CUSTID ORDER BY CustID ) AS ROWNUMBER,* FROM dbo.MYDATABASE )

DELETE FROM TEMP_TABLE WHERE ROWNUMBER > 1


The result looks like

Thursday, September 10, 2009

Visual Studio Debug skips the break point

The main issue i found out is the problem with the browser. (IE8)
IE 8 has a feature called Loosely-Coupled Internet Explorer (LCIE) which results in IE running across multiple processes.
http://www.microsoft.com/windows/internet-explorer/beta/readiness/developers-existing.aspx#lcie

Older versions of the Visual Studio Debugger get confused by this and cannot figure out how to attach to the correct process. You can work around this by disabling the process growth feature of LCIE. Here's how:

1) Open RegEdit
2) Browse to HKEY_LOCALMACHINE -> SOFTWARE -> Microsoft -> Internet Explorer -> Main
3) Add a dword under this key called TabProcGrowth
4) Set TabProcGrowth to 0

Since you are running on Windows Server 2003, this is all you should need to do. If you run into the same problem on Vista or newer, you will also need to turn off protected mode.

Friday, September 4, 2009

Catch SMTP Exception and re-send Email.

SmtpException Class


Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation.
Namespace: System.Net.MailAssembly: System (in System.dll)

Code Sample


public static void RetryIfBusy(string server)
{
MailAddress from = new MailAddress("ben@contoso.com");
MailAddress to = new MailAddress("jane@contoso.com");
MailMessage message = new MailMessage(from, to);
// message.Subject = "Using the SmtpClient class.";
message.Subject = "Using the SmtpClient class.";
message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
// Add a carbon copy recipient.
MailAddress copy = new MailAddress("Notifications@contoso.com");
message.CC.Add(copy);
SmtpClient client = new SmtpClient(server);
// Include credentials if the server requires them.
client.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an e-mail message to {0} using the SMTP host {1}.",
to.Address, client.Host);
try
{
client.Send(message);
}
catch (SmtpFailedRecipientsException ex)
{
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable)
{
Console.WriteLine("Delivery failed - retrying in 5 seconds.");
System.Threading.Thread.Sleep(5000);
client.Send(message);
}
else
{
Console.WriteLine("Failed to deliver message to {0}",
ex.InnerExceptions[i].FailedRecipient);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in RetryIfBusy(): {0}",
ex.ToString() );
}
}