Friday, December 14, 2012

Synchronous and Asynchronous Calls with C#



/***********************************************************************
Synchronous(blocking) calls vs Asynchronous(non-blocking) calls with C#
***********************************************************************/


namespace Test { class Program { static void Add(int a, int b) { Console.WriteLine(a.ToString() + " + " + b.ToString() + " = " + (a + b).ToString()); } static void Mul(int a, int b) { Console.WriteLine(a.ToString() + " X " + b.ToString() + " = " + (a * b).ToString()); } static void Process() { int i; for (i = 0; i < 10; i++) { System.Threading.Thread.Sleep(1000); Console.WriteLine(i.ToString() + " "); } } static void Main(string[] args) { Add(3, 4); Process(); // assume that this will take long time to process some data Mul(2, 3); // this guy has to wait for Process() to complete though there is no dependency } } }
/***********************************************************************
To avoid the waiting situation for Mul(), just change the Main() as below
***********************************************************************/
delegate void ProcessDelegate(); static void Main(string[] args) { ProcessDelegate dlg = new ProcessDelegate(Process); Add(3, 4); IAsyncResult ar = dlg.BeginInvoke(null,null); // no callback after invoking Mul(2, 3); // no waiting for Process() to complete dlg.EndInvoke(ar); }

Tuesday, December 11, 2012

Simple Stored Procedure with C# & SQL

---------------------------------
EmpTable with 2 columns
--------------------------------
EmpID varchar(10)
EmpName varchar(50)

----------------------------------------
Stored Procedure:GetEmpName
-----------------------------------------
ALTER PROCEDURE [dbo].[GetEmpName](@EmpID varchar(10))
AS
BEGIN
               Select EmpName from EmpTable where EmpID = @EmpID
END

------------------------------------------------------------
Executing the above stored procedure with C#
-------------------------------------------------------------


// setup DB connection SqlConnection conn = new SqlConnection(ConnectionString);
// open the connection conn.Open();
// create command for executing the stored procedure SqlCommand cmd = new SqlCommand("GetEmpName", conn);
// set commnad type cmd.CommandType = CommandType.StoredProcedure;
// add parameter to stored procedure cmd.Parameters.Add(new SqlParameter("@EmpID", "ID001"));
// execute the command SqlDataReader rd = cmd.ExecuteReader();
// now you have EmpName for corresponding EmpID passed to the stored procedure
// close the connection conn.Close();

Thursday, August 23, 2012

Read SQL table with VC++ MFC


/***********************************************************************
/* Description: /* : Use the following code to open the database and read the data from the required table */
/* include the header 'afxdb.h' */
***********************************************************************/


CDatabase db;
db.OpenEx(L"server-connection-string");
CRecordset rs(&db);
rs.Open(CRecordset::dynaset,L"select * from [tablename]");
CString strVal;
rs.GetFieldValue(L"CoulmnName",strVal); // you have the value in 'strVal'

Wednesday, August 22, 2012

Execute SQL query with VC++ MFC



/***********************************************************************
/* Description: /* : Use the following code to open the database and execute the SQL query */
/* include the header 'afxdb.h' */
***********************************************************************/



CDatabase db;

try
{
db.OpenEx(L"server-connection-string");
db.ExecuteSQL(L"query");
}
catch(CDBException & e)
{
// handle the exception here in case of querying failures
}



Thursday, July 12, 2012

HTML Headings



/***********************************************************************
/* Description: Description : HTML Headings are defined with <h1> to <h6> tags
/* dialog box.
/* See the example bellow, how to use it
***********************************************************************/




The <h1> tag defines defines the most important heading. <h6> defines the least important heading.:

1.Use HTML headings for headings only. Don't use headings to make text BIG or bold.

2.Search engines use your headings to index the structure and content of your web pages.

3.Since users may skim your pages by its headings, it is important to use headings to show the document structure.

4.H1 headings should be used as main headings, followed by H2 headings, then the less important H3 headings, and so on.

/****** How to use:****************************************************************************************/

<h1>This is a book </h1>
This is a book




Thursday, July 5, 2012

Html <a> Tag



/***********************************************************************
/* Description: Description : Use Html attribute <a>
/* dialog box.
/* See the example bellow, how to use it
***********************************************************************/




The <a> tag defines an anchor. An anchor can be used in two ways:

1.To create a link to another document, by using the href attribute
2.To create a bookmark inside a document, by using the name attribute
The <a> element is usually referred to as a link or a hyperlink.

The most important attribute of the <a> element is the href attribute, which indicates the link’s destination.

By default, links will appear as follows in all browsers:

An unvisited link is underlined and blue
A visited link is underlined and purple
An active link is underlined and red



/****** How to use:****************************************************************************************/

<a href="http://www.codefound.net">Vist Code Found</a>
Vist Code Found




Wednesday, February 8, 2012

C# Open File Dialog (OpenFileDialog)



/***********************************************************************
/* Description: Description : Use the following c# method to get the file name with the full path with the help of
/* dialog box.
/* See the example bellow, how to use it. Code compiled with C# VS2010
***********************************************************************/




public string FileNameFromOpenDialog(string fileExtDescription, string fileExtFilter)
{
System.Windows.Forms.OpenFileDialog openFD = new System.Windows.Forms.OpenFileDialog();

openFD.Filter = fileExtDescription + "(*." + fileExtFilter + ")|*." + fileExtFilter;
openFD.RestoreDirectory = true;

if (openFD.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return null;
}
return openFD.FileName;
}

/****** How to use:****************************************************************************************/

string fileName = FileNameFromOpenDialog("Data Files", "dat");





Wednesday, January 18, 2012

PIC24F DS1307 RTC Communication



/***********************************************************************
/* Description: Use the following code as it is to communicate with external RTC(DS1307).
/* include the file i2c.h(C30 installed) that declares all the I2C module related functions.
/* Code compiled with Microchip C30 compiler.
***********************************************************************/




void initRTCC(void) {
unsigned int config1 = 0;
unsigned int config2 = 0;

CloseI2C2();
Delay10ms(10);// some delay
ConfigIntI2C2(MI2C_INT_OFF); //Disable I2C interrupt
Delay10ms(10);// some delay

config1 = (I2C_ON | I2C_7BIT_ADD);
config2 = 157; //baud rate 100KHz
OpenI2C2(config1, config2);//configure I2C2
Delay10ms(10);// some delay
}

void SetRTCDate(unsigned char dd, unsigned char mm, unsigned char yy) {
// START
IdleI2C2();
StartI2C2();
while (I2C2CONbits.SEN); //Wait till Start sequence is completed
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag

// Control Byte
MasterWriteI2C2(0xD0); //Write Slave address and set master for transmission
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(0x04); // Write address pointer for read
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(dd); // Write slave address for write
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(mm); // Write slave address for write
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(yy); // Write slave address for write
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
StopI2C2(); // Create Stop Sequence
while (I2C2CONbits.PEN); // Wait till stop sequence is completed
}

void SetRTCTime(unsigned char ss, unsigned char mn, unsigned char hh) {
// START
IdleI2C2();
StartI2C2();
while (I2C2CONbits.SEN); //Wait till Start sequence is completed
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag

// Control Byte
MasterWriteI2C2(0xD0); //Write Slave address and set master for transmission
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(0x00); // Write address pointer for read
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(ss); // Write slave address for write
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(mn); // Write slave address for write
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(hh); // Write slave address for write
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
StopI2C2(); // Create Stop Sequence
while (I2C2CONbits.PEN); // Wait till stop sequence is completed
}


/* use this function to read day,hour etc. to read individually by passing
/* the corresponding address value. Refer DS1307 datasheet for address mapping */

unsigned char ReadRTC(unsigned char addr) {
unsigned char data;

// START
IdleI2C2();
StartI2C2();
while (I2C2CONbits.SEN); //Wait till Start sequence is completed
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
// Control Byte
MasterWriteI2C2(0xD0); //Write Slave address and set master for transmission
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
MasterWriteI2C2(addr); // Write address pointer for read
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
RestartI2C2(); // Write Restart sequence
while (I2C2CONbits.RSEN); // Wait for ACK
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag

// Control Byte
MasterWriteI2C2(0xD1); //Write Slave address and set master for transmission
while (I2C2STATbits.TBF); //Wait till address is transmitted
while (!IFS3bits.MI2C2IF); //Wait for ninth clock cycle
MI2C2_Clear_Intr_Status_Bit; //Clear interrupt flag
while (I2C2STATbits.ACKSTAT);
data = MasterReadI2C2(); // Read from I2C the desired byte
NotAckI2C2(); // Make NACK for the read byte
while (I2C2CONbits.ACKEN == 1); // Wait till ACK/NACK sequence is over
StopI2C2(); // Create Stop Sequence
while (I2C2CONbits.PEN); // Wait till stop sequence is completed

return (data); // Return read data
}