Archive for April, 2009

clickable tr and Show/Hide Panel

April 28, 2009

Sometimes we may need to code to make an entire row clickable, I mean, the user should click any cell on a tr and a required functionality should be performed as the result of this click. When you cannot use server side coding and rely entirely on client side coding, the below could be one of the approaches to this problem.

        <asp:Panel ID=”_pnlContactList” runat=”server” CssClass=”tableBorder” Height=”200px” ScrollBars=”Vertical”>
               <table cellspacing=”0″ style=”width: 970px; margin-top:5px; margin-left:10px; margin-bottom:5px; margin-right:5px” class=”tableBorder FontNameValue”>
                <tr>
                    <td align=”center” class=”FontName tableBorder tdheader”> Name</td>
                    <td align=”center” class=”tableBorder tdheader FontName”>Title</td>
                    <td align=”center” class=”FontName tableBorder tdheader”>Phone Number</td>
                    <td align=”center” class=”tableBorder tdheader FontName”>Email</td>
                </tr>
               
                <tr align=”left” onclick=”Show(1)”>
                    <td align=”left” class=”tableBorder”>Bill</td>                   
                    <td align=”left” class=”tableBorder”>Gates</td>
                    <td class=”tableBorder”>800-000-0000</td>
                    <td class=”tableBorder”>bill.gates@microsoft.com</td>
                </tr>
               
                <tr onclick=”Show(2)”>
                    <td align=”left” class=”tableBorder”>Bill</td>                      
                    <td align=”left” class=”tableBorder”>O’Reilly</td> 
                    <td class=”tableBorder”>800-000-0000</td>
                    <td class=”tableBorder”>bill.reilly@foxnews.com</td>
                </tr></table></asp:Panel>

Javascript functions inside <head>

<script type=”text/javascript”>
var nameArray = [];
nameArray[1] = “Bill Gates”;
nameArray[2] = “Bill O’Reilly”;
nameArray[3] = “Bill Clinton”;
nameArray[4] = “Donald Trump”;
nameArray[5] = “Donald Duck”;
nameArray[6] = “Donald Rumsfeld”;
nameArray[7] = “Home Foreclosure”;
nameArray[8] = “Credit Card”;

var emailArray = [];
emailArray[1] = “
bill.gates@microsoft.com“;
emailArray[2] = “
bill.reilly@foxnews.com“;
emailArray[3] = “
bill.clinton@capitol.com“;
emailArray[4] = “
donald.trump@empire.com“;
emailArray[5] = “
donald.duck@disney.com“;
emailArray[6] = “
donald.rumsfeld@state.com“;
emailArray[7] = “
home.foreclosure@states.com“;
emailArray[8] = “
credit.card@debt.com“;

 

    function Hide()
    {
        document.getElementById(‘_divPnl’).style.visibility = ‘hidden’;
    }

    function Show()
    {
        document.getElementById(‘_divPnl’).style.visibility = ‘visible’;
    }
   
    function Show(elem)
    {       
        for(var i=1; i<=8; i++)
        {
            if(elem == i) 
            {         
                document.getElementById(‘name’).innerHTML = nameArray[i];
                document.getElementById(‘email’).innerHTML = emailArray[i];
                document.getElementById(‘_divPnl’).style.visibility = ‘visible’;
            }
        }
   }

</script>

coming back to markup:

in my case, i want to show a panel when a tr is clicked, this panel will contain detailed information relevant to the <tr>. this panel would be initially hidden, and when the <tr> is clicked, will be displayed.

            <div id=”_divPnl” style=”visibility:hidden”>
        <asp:Panel ID=”_pnlContactDetails” runat=”server” CssClass=”tableBorder” Height=”170px”>
               <table cellspacing=”0″ style=”width: 970px; margin-top:5px; margin-left:10px” class=”tableBorder”>
                <tr>
                    <td align=”center” class=”FontName tableBorder tdheader”>
                        Address</td>
                        <td class=”tableBorder tdheader” style=”color:Silver”>none</td>
                    <td align=”center” class=”FontName tableBorder tdheader” style=”color:Silver”>none
                        </td>
                        <td class=”tableBorder tdheader” style=”color:Silver”>none</td>
                </tr>
               
                <tr>
                    <td align=”left” class=”tableBorder FontName”>Name</td>
                    <td id=”name” align=”left” class=”tableBorder FontNameValue”></td> 
//empty <td>, value will be dynamically displayed using <script> functions
                    <td class=”tableBorder FontName”>Title</td>
                    <td class=”tableBorder FontNameValue”>Mr. </td>
                </tr>
                <tr>
                    <td align=”left” class=”tableBorder FontName”>
                        Address Line 1</td>
                    <td align=”left” class=”tableBorder FontNameValue”>I-275 W
                    </td>
                    <td class=”tableBorder FontName”>Telephone Number</td>
                    <td class=”tableBorder FontNameValue”>888-890-9021</td>
                </tr>
               
                <tr>
                    <td class=”tableBorder FontName”>Address Line 2</td>
                    <td class=”tableBorder FontNameValue”>Apt O</td>
                    <td class=”tableBorder FontName”>Cell Phone Number</td>
                    <td class=”tableBorder FontNameValue”>888-888-8888</td>
                </tr>
               
                <tr>
                    <td class=”tableBorder FontName”>City</td>
                    <td class=”tableBorder FontNameValue”>Cincinnati</td>
                    <td class=”tableBorder FontName”>Fax Number</td>
                    <td class=”tableBorder FontNameValue”>800-000-0000</td>
                </tr>
               
                <tr>
                    <td class=”tableBorder FontName”>State</td>
                    <td class=”tableBorder FontNameValue”>OH</td>
                    <td class=”tableBorder FontName”>Email Address</td>
                    <td class=”tableBorder FontNameValue”><a href=”
someone@somewhere.com” id=”email”></a></td>
                </tr>

                <tr>
                    <td class=”tableBorder FontName”>Zip Code</td>
                    <td class=”tableBorder FontNameValue”>00000</td>
                    <td class=”tableBorder FontName”>Contact Type</td>
                    <td class=”tableBorder FontNameValue”>Customer</td>
                </tr>
            </table>
        </asp:Panel>
        </div>

 

References:

1. http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=312

2. http://www.devx.com/tips/Tip/13638

3. http://www.tizag.com/javascriptT/javascript-getelementbyid.php

SELECT from EXEC StoredProc

April 20, 2009

If you want to retrieve the results of EXECuting a Stored Proc the most common way is to create a temp table and insert the results of EXEC stored proc into this table. The table designs of the temp table and the result of the EXEC storedproc should be the same.

Example:

CREATE TABLE #TEMP
(CONTACTID INT,
CONTACTTITLE VARCHAR(50),
CONTACTFIRSTNAME VARCHAR(50),
CONTACTLASTNAME VARCHAR(50),
FAX VARCHAR(20),
PHONE1 VARCHAR(20),
PHONE2 VARCHAR(20),
EMAIL VARCHAR(50),
CONTACTTYPEID INT,
LOCATIOND INT
)
INSERT INTO #TEMP EXEC wtg_ContactMasterGet 4
SELECT * FROM #TEMP

Here the stored proc wtg_ContactMasterGet  takes a parameter 4 and returns a result set. The types of the result set must match with the table design of the temp table.

 

References: http://www.sqlteam.com/article/return-recordsets-from-dynamic-queries-called-by-exec

ASP.NET WebParts on SharePoint – For a Beginner

April 3, 2009

Environment: .NET 3.5, ASP.NET, SharePoint, WSS 3.0, VS 2008, C#

With VS 2008 comes SharePoint templates. To get to this:

Step 1: New Project-> Windows -> SharePoint (LHS) -> Click on WebPart template (RHS)

Step 2: Open the WebPart1 project

Step 3: Uncomment the C# code in WebPart1.cs in CreateChildControls()

Step 4: Save. In Solution Explorer, Right click on Deploy.

Step 5: In C:\Inetpub\wwwroot\wss\VirtualDirectories\80\web.config (aka 12-hive) do the following:

5.1 <compilation> set debug = true To enable debugging on WebPart code

5.2 <customErrors> set Off. As a dev you want to see the actual error output.

5.3 <CallStack> set True. You want to see the error stack trace to pinpoint the error

5.4 <ApplicationPageLevelTrace> set True. You want to view page level tracing

Step 6: Build, Run

Step 7: http://localhost will change to http://mossdev/default.aspx?PageView=Shared. Site Actions->Edit Mode -> Click ‘Add a Web Part’. Select WebPart1 from the list. Click Add.  

Reference:

http://visualstudiomagazine.com/features/article.aspx?editorialsid=2644

Open a .doc or .xls as .pdf aka Convert .doc or .xls to .pdf

April 1, 2009

One of my recent tasks was to code to open a .doc or .xls as .pdf. The file should open as .pdf and then it’s upto the user to save the .pdf. I tell you I had to scourge the internet looking for good leads. There are many articles in www.codeproject.com, many threads in asp.net forums, and one project called iTextDotNet in www.sourceforge.net, a few blogs – all related to the same topic. But it was in www.msdn.com I found what I needed. I immediately tested it and cooooollllllll……gotcha. For developers, here you go….

I did this in VS 2008 and Office 2007. I know this works good on .docx, .xlsx(Office 2007), .doc (Microsoft Office Word 97 – 2003 Document), .xls (Microsoft Office Excel 97-2003 Worksheet) – I couldn’t try this code on Office 2003 directly but I saved the files in Office 2003 Compatible format and it works good.

TODO:

1. Add namespaces

 

 

 

using MSExcel = Microsoft.Office.Interop.Excel;//Add References-> COM
 
2. Install Addin : SaveAsPDFAndXPS
 
//code
object Unknown = Type.Missing; 
 
MSWord.Application newApp = new Microsoft.Office.Interop.Word.Application();
 
//source document
object src_doc = “C:\Users\admin\Desktop\DocumentViewer\test.docx”; 
 
newApp.Documents.Open(ref src_doc, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,  ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);  //# of params 16, # of Unknowns 14
 
//save as PDF
object format = MSWord.WdSaveFormat.wdFormatPDF;
 
newApp.ActiveDocument.SaveAs(ref des_doc, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
 
object des_doc= “C:\Users\admin\Desktop\DocumentViewer\test.pdf”;
 
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
 
//now to open .xls as .pdf 

MSExcel.Application newExcelAppn = new Microsoft.Office.Interop.Excel.Application ();

MSExcel.Workbook newbook = null; 
 
string src_xls = “C:\Users\admin\Desktop\DocumentViewer\To_Bret_Conard.xlsx”; 
 object des_xls = “C:\Users\admin\Desktop\DocumentViewer\To_Bret_Conard.pdf”;
 
newbook = newExcelAppn.Workbooks.Open(src_xls, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);
 
//specify the format. PDF 
MSExcel.XlFixedFormatType paramExportFormat = MSExcel.XlFixedFormatType.xlTypePDF; 
 
 // Save it in the target format.
newbook.ExportAsFixedFormat(paramExportFormat, des_xls, MSExcel.XlFixedFormatQuality.xlQualityStandard, true, true, Unknown, Unknown, false, Unknown);
 
newExcelAppn.Quit();

  //view the pdf in browser 

 byte[] buffer = new byte[1024]; 
 
long byteCount;
 
inStr =File.OpenRead(“C:\Users\admin\Desktop\DocumentViewer\To_Bret_Conard.pdf”); 
 
while((byteCount = inStr.Read(buffer, 0, buffer.Length)) > 0)
{
if(Context.Response.IsClientConnected)
{
Context.Response.ContentType = “application/pdf”;
Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
Context.Response.Flush();
}
}
 
References:

http://msdn.microsoft.com/en-us/library/bb407651.aspx

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f8989c05-d04a-4b4a-be0f-fc0055691de7