Sunday, March 10, 2013

Databinding to a Button Text in ASP.NET, C#


/*********************************************************************************
/* 
/* Databinding to a Button Text in ASP.NET, C#
/* 
/*********************************************************************************
Add this to your ASPX page

background-repeat: no-repeat; background-position: left;
      padding-left: 15px;" runat="server" Height="40px" BorderStyle="None" 
Text='<%#DataBinder.Eval(Container.DataItem,"ColumnName")%>'
      Width="93px" OnClick="ColumnNameChanged" />


    ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
    SelectCommand="SELECT [ColumnName] FROM [TableName]">


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