Thursday, August 27, 2009

iTextsharp simple Image to PDF c#

Very simple code.

public void ImagetoPDF()
{
string pdfpath = Server.MapPath("PDFs");
string imagepath = Server.MapPath("Images");
Document doc = new Document();
try
{
PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Images.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(Server.MapPath("") + "/testasp3.gif");
png.ScaleToFit(475f, 1500f);
doc.Add(png);
}
catch (DocumentException dex)
{
Response.Write(dex.Message);
}
catch (IOException ioex)
{
Response.Write(ioex.Message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
doc.Close();
Response.Redirect("PDFs/Images.pdf");
}
}

Wednesday, August 26, 2009

Wireless Zero Configuration auto start on boot

Start > Run > sc config wzcsvc start= auto&sc start wzcsvc


Done. :)

UserControl Javascript Worst problem and Best Solution

Working on Jquery is fun and exciting. Same time it makes us to scratch our head countless times. :)

I was making a User Control which will have a dropdown and a image control, in which dropdown which loads countries.
On selecting a country in dropdown it has to show selected country flag image and the tooltip is to show the details of the country yes without any postback and using Jquery.

It was simple and used this...


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ddlCountries.Attributes.Add("onchange", "getCountryDescription()");
}
}




and ASPX:


<script type="text/javascript">
function getCountryDescription() {
$.ajax({
type: "POST",
url: "http://localhost/OnActionC/JqueryMethods/JqueryAjax.aspx/LoadFlag",
data: "{'StrCountryText':'" + $("#<%=ddlCountries.ClientID %> option:selected").text() + "', 'StrCountryValue':'" + $('#<%=ddlCountries.ClientID %>').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#<%=imgCountry.ClientID %>').attr("Src", (msg.d[0]));
$('#<%=imgCountry.ClientID %>').attr("title", (msg.d[1]));
}
});
}
</script>




<div>
<asp:DropDownList ID="ddlCountries" class="grey" ForeColor="#FAFAFA" style="font-size:0.8em; background-color:#414141;" runat="server" >
</asp:DropDownList>
<a href="#" runat="server" id="countrytooltip" >
<img id="imgCountry" runat="server" alt="" />
</a>
</div>

This was pretty good and I used this user control in a screen which has 2 seperate controls in a single page...







Ohh!! there is a problem...i found.. we are calling a single function getCountryDescription
for both controls, even though the contorlid of individual are different it was causing the problem since one control was refereing to other...

So the possible solutions are :
1. Better create as many as functions as we use the user control in a single page.
Ohhh!! no... I cant do this... I cant afford to deal with this messy issue.

2. So the next idea was to make the function dynamic , yes so each control will refer to its corresponding function only, so the possible way is to play with ClientId.

then I made the following changes.





function getCountryDescription<%=ddlCountries.ClientID %>() {
$.ajax({
type: "POST",
url: "http://localhost/OnActionC/JqueryMethods/JqueryAjax.aspx/LoadFlag",
data: "{'StrCountryText':'" + $("#<%=ddlCountries.ClientID %> option:selected").text() + "', 'StrCountryValue':'" + $('#<%=ddlCountries.ClientID %>').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#<%=imgCountry.ClientID %>').attr("Src", (msg.d[0]));
$('#<%=imgCountry.ClientID %>').attr("title", (msg.d[1]));
}
});
}

and





protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ddlCountries.Attributes.Add("onchange", "getCountryDescription"+this.ddlCountries.ClientID+"()");
}
}

in c#.


Yes now i am able to use as many as user controls as i wish...

Happy coding.



Jquery javascript Dropdown selected value , Text

How to get the selected value of the dropdown in Jquery

var StrCountryValue = $('#<%=ddlCountries.ClientID %>').val();

to get selected text of the dropdown

var StrCountryText = $("#<%=ddlCountries.ClientID %> option:selected").text();

Friday, August 21, 2009

Infinity Scrolling

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InfiniteScroll.aspx.cs" Inherits="Common_UserControls_Lab_InfiniteScroll" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>

<link href="styles.css" rel="stylesheet" type="text/css" />
<script src="jquery-1.2.6.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() {
var pageIndex = 1; // page number (if set to -1 no additional calls will be made)
var pageSize = 10; // number of records to display per page
var count = 20; // total counter

// function OnDivScroll() {
$('#content').scroll(function() {
// if there is more to show

if (pageIndex != -1) {
// get content
var content = document.getElementById('<%=content.ClientID %>');

if (content.scrollTop < content.scrollHeight - 500)
return;

// leave this method if loading is in progress
var indicator = document.getElementById('indicator');
if (indicator.style.display == '')
return;
// Get data for next page
pageIndex += 1;
indicator.style.display = '';
// CountryWS.GetMoreCountriesObject(pageIndex, pageSize, HandleRetrievedData);
var abdd = '';


debugger;
alert('going to ajax');
$.ajax({
type: "POST",
data:"{}",
url: "http://localhost/OnActionC/Common/UserControls/Lab/InfiniteScroll.aspx/a",
// data: "pageIndex=" + pageIndex + "&pageSize=" + pageSize,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
//$("#ya").attr("value", msg.d);

}
});

}
});
});




function HandleRetrievedData(result) {
var indicator = document.getElementById('indicator');
var content = document.getElementById('<%=content.ClientID %>');

// Parse if web service returned data, otherwise stop calling it
if (result.length > 0) {
// calculate and show total number of items
count += result.length;
document.getElementById('divCount').innerHTML = count + " countries loaded."

// iterate through result set and create a div for each item
for (var i = 0; i <= result.length - 1; i++) {
indicator.style.display = 'none';
content.innerHTML += "<div class='entry'><b>" +
result[i].CountryName + "</b> (" + result[i].Symbol + ") - " +
result[i].InternetUsers + " internet user(s)</div>";
}
}
else {
pageIndex = -1;
document.getElementById('divCount').innerHTML += " All items has been loaded.";
indicator.style.display = 'none';
}
}

</script>

</head>
<body>
<form id="form1" runat="server">
<%--<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/CountryWS.asmx" />
</Services>
</asp:ScriptManager>--%>
<div>
<div class="header">
<b>List of countries having more than a million internet users</b></div>
<div class="container">
<div id="content" runat="server" >
<%--Items will be rendered here--%>
</div>
<div id="indicator" style="display: none;">
Loading more items...
</div>
</div>
<div id="divCount">
20 countries loaded.
</div>
</div>
</form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class Common_UserControls_Lab_InfiniteScroll : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
CountryClass a = new CountryClass();
this.GetAllCountryDetails(1, 20);

//string newItems = "";
//for (int i = 0; i < countries.Count; i++)
//{
// newItems += "
" + countries[i].CAPITAL + " (" + countries[i].COUNTRY1+ ") - " + countries[i].CURRENCY + "
";
//}
//content.InnerHtml = newItems;




}

[System.Web.Services.WebMethod]
public static void a()
{
}
// [System.Web.Services.WebMethod]
public void GetAllCountryDetails(int PageIndex, int PageSize)
{
GeneralDataContext db = new GeneralDataContext();

var objCountry = from p in db.Countries// orderby p.COUNTRY1
select p;// select new { p.ID, p.CAPITAL, p.COUNTRY1, p.CURRENCY, p.ISO2, p.ISO3, p.POPULATION, p.REGIONS, p.TLD };
objCountry = objCountry.Skip((PageIndex - 1) * PageSize).Take(PageSize);
// List objCountryList = new List();
List countries= objCountry.ToList();

string newItems = "";
for (int i = 0; i < countries.Count; i++)
{
newItems += "
" + countries[i].CAPITAL + " (" + countries[i].COUNTRY1 + ") - " + countries[i].CURRENCY + "
";
}
content.InnerHtml = newItems;


}
}

Thursday, August 6, 2009

System.InvalidOperationException: Cannot serialize the DataTable.

I cracked my head on this problem

System.InvalidOperationException: Cannot serialize the DataTable. to get a datatable as return from a webservice.

The solution is simple:
Just give a name to the datatable then you will be able to call the datatable object .

Enjoy coding. :)