Saturday, June 28, 2014

C# Text box only allow two decimal points and numbers using text box keypress event

private void txtScore_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b') && (e.KeyChar != '.'))
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
            if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
            {
                TextBox tb = sender as TextBox;
                int cursorPosLeft = tb.SelectionStart;
                int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
                string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
                string[] parts = result.Split('.');
                if (parts.Length > 1)
                {
                    if (parts[1].Length > 2 || parts.Length > 2)
                    {
                        e.Handled = true;
                    }
                }
            }

        }

Friday, June 20, 2014

C#.NET - How to open windows application form from winword using Com objects ( Macro)

Step 1
1.      Add Class Library name "WordConnector"



Step 2

Rename Class1.cs as "clsConnector.cs"



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WordConnector
{
    public class clsConnector
    {
        public void Start(Word.Document objDocument)
        {
            new frmWord(objDocument).Show();
        }
    }
}

----------------------------------------
Add form rename as "frmWord.cs"

-------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WordConnector
{
    public partial class frmWord : Form
    {
        string sDocumentName = string.Empty;
        public frmWord()
        {
            InitializeComponent();
        }
        public frmWord(Word.Document objDocument)
        {
            InitializeComponent();
            this.sDocumentName = objDocument.Name.ToString();
            this.Text = sDocumentName.Replace(".doc", string.Empty).ToUpper();
           
        }
    }
}




Step 3



Step 4

Click on Assembly Information button



Step 5

Check Make assembly COM-Visible


Step 6

Click Build and Scroll down check Register for COM interop.





Step 7

Click Signing menu



Step 8
1. Check sign the assembly.
2. Click choose a strong name key file combo box <New>


Step 9

Give key file name as "Key_WordConnector"



Step 10

Copy
WordConnector.dll and WordConnector.tlb from project bin folder



Step 11

Pate WordConnector.dll and WordConnector.tlb in " C:\WINDOWS\system32"

Step 12
1. Open winword document and Press Alt+F11
2. Write macro in module.



3.Click Tools -> References - >


5. Browse for WordConnector.tlb



Check Wordconnector



Add this macro in module




Press F8 and see form will open from worddocument

Wednesday, June 18, 2014

MSSQL - Getting all dates between from date and to date.

declare @col nvarchar(max) 
declare @query nvarchar(max)

declare @StartDate as date 
declare @EndDate as date 

set @StartDate = '2014-06-01'
set @EndDate = '2014-06-18'

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL DROP TABLE #TempTable
create TABLE #TempTable 
( 
      Rep_Date date 
) 
 
while @StartDate <= @EndDate 
begin 
 insert into #TempTable (Rep_Date) values(@StartDate) 
 set @StartDate = DATEADD(dd,1,@StartDate) 
end 
  
select * from  #TempTable 
 

drop table #TempTable  

Saturday, June 14, 2014

C#.NET - BackgroundWorker that accepts more than one argument.

Class MyClass
{
     private string m_Username = string.Empty;
     private int m_Targetnumber;

     public MyClass(){}

     public string Username
     {
         get { return m_Username; }
         set { m_Username = value; }
     }

     public int TargetNumber
     {
         get { return m_TargetNumber; }
         set { m_TargetNumber = value; }
     }
 }



// setup/init:

BackgroundWorker startCallWorker = new BackgroundWorker();
startCallWorker.DoWork += new DoWorkEventHandler(StartCallWorker_DoWork);
...

MyClass thisClass = new MyClass();
thisClass.Username = "abcd";
thisClass.TargetNumber = 1234;
startCallWorker.RunWorkerAsync(thisClass);


// the handler:
private void StartCallWorker_DoWork(object sender, DoWorkEventArgs e)
{
     MyClass args = (MyClass)e.Argument;
     string userName = args.Username;
     string targetNumber = args.TargetNumber;
}

Thursday, June 12, 2014

MSSQL - Communicating one database to another using opendatasource

Communicating one database to another using opendatasource.

Example;

select UserId , Username , EmployeeId

from OPENDATASOURCE('SQLOLEDB', 'Data Source=172.166.10.116; User ID=sa;Password=12345678').hrms.dbo.employee_list  

Wednesday, June 11, 2014

C#.NET - Check decimal value in DataGridView Cell

Where to use?




Step 1: Add DataGridViewTextBoxColumn column in DataGridView.
Step 2: Add EditingControlShowing events in DataGridView

Code:

private void dgvClientBilling_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
            System.Windows.Forms.TextBox txt = (System.Windows.Forms.TextBox)e.Control;
            txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
}


public void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b') && (e.KeyChar != '.'))
                {
                    e.Handled = true;
                }
                else
                {
                    e.Handled = false;
                }
                if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
                {
                    TextBox tb = sender as TextBox;
                    int cursorPosLeft = tb.SelectionStart;
                    int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
                    string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
                    string[] parts = result.Split('.');
                    if (parts.Length > 1)
                    {
                        if (parts[1].Length > 2 || parts.Length > 2)
                        {
                            e.Handled = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleException(ex.ToString());
            }
        }


C#.NET - ContextMenuStrip Enable / Disable while opening

Where to use?

When there is a need to enable/disable particular menu we can use this code.

Control: ContextMenuStrip ( Name : cmsQA)

Event: ( Opening)



Code :

private void cmsQA_Opening(object sender, CancelEventArgs e)
{
            try
            {
                e.Cancel = !(lsvFiles.SelectedItems.Count > 0);
                if (lsvFiles.SelectedItems.Count == 1)
                {
                    cmsAllocation.Items["tsmAssignFile"].Enabled = false;
                    cmsAllocation.Items["tsmUpload"].Enabled = false;
                    cmsAllocation.Items["tsmChangeTemplate"].Enabled = false;
                    cmsAllocation.Items["tsmDelete"].Enabled = false;

                    int iIndex = 5;

                    int[] iAssigneFileStatus = new int[] { 1, 5, 9 };
                    if (iAssigneFileStatus.Contains(iIndex))
                    {
                        cmsAllocation.Items["tsmAssignFile"].Enabled = true;
                    }


                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleException(ex.ToString());
            }

        }