Saturday, October 2, 2010

Crawl any type of webpage using this function

this function reuires following

1. Name Space
using System.Net;

2.Function requires 3 Paramaters
a. Page URL - From which you want to crawl data
b. Start text - Find out the start Unqiue Text from where you want to copy the data
c. endText - After Start Text the Last Unique Text upto whcih you want to Crawl

Functions
=================================================================================
public static string returnResultTable(string url, string startText, string EndText)
{
try
{
string WFLocation = url;
int startpos;
int endpos;
string location = WFLocation;
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(location);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
string sde= resp.ContentType;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
startpos = results.IndexOf(startText);
results = results.Substring(startpos, (results.Length - startpos));
endpos = results.IndexOf(EndText);
results = results.Substring(0, endpos);
return results;
}
catch (Exception ex) { return ex.Message; }
}

************************************************************************

It will return you the required block of html code from the particular page uRL

Upload File using JQuery in Asp.Net

There is 3 parts

1. JQuery Javascript file
2. Aspx Page Integration
3. Upload code on aspx.cs


1. JQuery Javascript file
=================================================================================
2 Files Needed to integrate that are as follows
a. jquery-1.3.2.js
b. jquery.MultiFile.js // To upload multiple files using JQuery


2. Aspx Page Integration
===============================================================================
Inculde jquery-1.3.2.js, in your aspxpage










3. Upload code on aspx.cs
==============================================================================
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
// Get the HttpFileCollection
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath("MyFiles") + "\\" + System.IO.Path.GetFileName(hpf.FileName));
Response.Write("File: " + hpf.FileName + " Size: " +
hpf.ContentLength + " Type: " + hpf.ContentType + " Uploaded Successfully
");
}
}
}
catch (Exception ex)
{

}
}

Here I am attaching the whole source code thourgh which all of you can easly understand how it will works

Friday, October 1, 2010

Change The Size of you tube video dynamically

If we are displaying the youtube video on our site using embed tag then many times we need to change the size of video.. (Its only for those developers who keeping the embed tag in database and displayed from database)

Functional Block
========================================================================
//Valuetobereplace is embed tag which will pass with height and width
public string ReplaceValue(string Valuetobereplace, int height, int width)
{
string strMani = Valuetobereplace.Replace("width", "*");
string[] spliVal = strMani.Split('*');
int devcount = 0;
for (int i = 1; i < spliVal.Length; i++)
{
for (int j = 0; j < spliVal[i].Length; j++)
{
try
{
if (devcount != 0)
break;
int.Parse(spliVal[i].Substring(j, j + 1));
devcount = j;
}
catch
{
continue;
}
}
spliVal[i] = spliVal[i].Replace(spliVal[i].Substring(devcount, 3), width.ToString());
devcount = 0;
}

string SecondLast = spliVal[0];
for (int i = 1; i < spliVal.Length; i++)
{
SecondLast = SecondLast + "width" + spliVal[i];
}
SecondLast = SecondLast.Replace("height", "*");
spliVal = SecondLast.Split('*');
for (int i = 1; i < spliVal.Length; i++)
{
for (int j = 0; j < spliVal[i].Length; j++)
{
try
{
if (devcount != 0)
break;
int.Parse(spliVal[i].Substring(j, j + 1));
devcount = j;
}
catch
{
continue;
}
}
spliVal[i] = spliVal[i].Replace(spliVal[i].Substring(devcount, 3), height.ToString());
devcount = 0;
}

string FinalValue = spliVal[0];
for (int i = 1; i < spliVal.Length; i++)
{
FinalValue = FinalValue + "height" + spliVal[i];
}
return FinalValue;
}

Use chop string function to avoid design level fault

I have seen most of developers using string without chop string function it will destroy the whole website design how try and see


Enter long characters set (without any spaces like aaaaaaaaaaaaaaaaaaaaaaa - upto 5000) and save this info and when you will get this info on page you will see it has been distroyed you page

Use below function to avoid these type of issues
================================================================================

public string ChopStringWithLen(string StrName, int Len)
{
if (StrName.Length > Len)
return (StrName.Substring(0, Len) + "....");
else
return StrName;
}

Its mostly done with descriptive field where info is too long

Fill All dropdownlist using 3 functions using Asp.Net C#

Its a general function that will reduce the code size of your porject. I have seen many developers those are writing lines of code everywhere where they are using dropdownlist. Here I have writeen a function which will reduce the lines of code just you need to pass the parameter values it will automatically fill the drop downlist see how...........

Function To get result with database
=========================================================================
private DataSet GetRecordsToFillCombo(String valuefield, String displayfield, String tablename)
{
DataSet ds;
String sql;
sql = "select " + valuefield + "," + displayfield + " from " + tablename + " order by " + displayfield + "";
ds = SqlHelper.ExecuteDataset(ConnectionString.CnSettings.cnString1, CommandType.Text, sql);
return ds;
}

****************************************************************************

Function with normal drop down filling
============================================================================
public void FillCombo(String valuefield, String displayfield, String tablename, DropDownList ddl, String stype)
{
DataSet ds;
ds = GetRecordsToFillCombo(valuefield, displayfield, tablename);
ddl.DataSource = ds;
ddl.DataTextField = displayfield;
ddl.DataValueField = valuefield;
ddl.DataBind();
ListItem li = new ListItem();
if (stype == "A")
{
li.Text = "All";
li.Value = "0";
ddl.Items.Insert(0, li);
}
else
{
li.Text = "Select";
li.Value = "0";
ddl.Items.Insert(0, li);
}
// ddl.SelectedValue = "0";
}

***************************************************************************
Function to fillfiltered record into dropdownlist
===========================================================================
public void FillCombo_Filter(String valuefield, String displayfield, String tablename, DropDownList ddl, String stype, String Wherefield, String Comparefield)
{

DataSet ds;
ds = GetRecordsToFillCombo_Filter(valuefield, displayfield, tablename, Wherefield, Comparefield);
ddl.DataSource = ds;
ddl.DataTextField = displayfield;
ddl.DataValueField = valuefield;
ddl.DataBind();
ListItem li = new ListItem();
if (stype == "A")
{
li.Text = "All";
li.Value = "0";
ddl.Items.Insert(0, li);
}
else
{
li.Text = "Select";
li.Value = "0";
ddl.Items.Insert(0, li);
}
}


***********************************************************************
How to call on page
========================================================================
create an object of your class file where function is written
like
................................................
abc obj= new abc();
obj.GetRecordsToFillCombo("EmpId", "EmpName", "Employee", ddl(dorpdownlist box Id),"S For Select And A for All")

obj.FillCombo_Filter("EmpId", "EmpName", "Employee",ddl(dorpdownlist box Id),"S For Select And A for All", "EmpId", "123")

===================================================================

You can use order by class just passing one parameter to all functions

Send Mail Using Gmail

If you are using Gmail mail or your client using GOOGLE Mail settings(like Googles Educations free email) Then you can use the following code to send send mail from your project or web sites.


Function Block
============================================================================

public void SendEmailGmail(string pGmailEmail, string pGmailPassword, string pTo, string pSubject, System.Web.Mail.MailFormat pFormat, string pAttachmentPath, string mailfrom, string mailbody)
{
try
{
System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
//sendusing: cdoSendUsingPort, value 2, for sending the message using
//the network.

//smtpauthenticate: Specifies the mechanism used when authenticating
//to an SMTP
//service over the network. Possible values are:
//- cdoAnonymous, value 0. Do not authenticate.
//- cdoBasic, value 1. Use basic clear-text authentication.
//When using this option you have to provide the user name and password
//through the sendusername and sendpassword fields.
//- cdoNTLM, value 2. The current process security context is used to
// authenticate with the service.
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//Use 0 for anonymous
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", pGmailEmail);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pGmailPassword);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
myMail.From = mailfrom;
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;


myMail.Body = mailbody;

if (pAttachmentPath.Trim() != "")
{
System.Web.Mail.MailAttachment MyAttachment = new System.Web.Mail.MailAttachment(pAttachmentPath);
myMail.Attachments.Add(MyAttachment);
myMail.Priority = System.Web.Mail.MailPriority.High;

}

System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
System.Web.Mail.SmtpMail.Send(myMail);

}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex);
throw;
}
}


Send SMS Using any Third Party SMS API In Dot Net

This function will demonstrate you that how you can use any SMS API to send Single or Bulk SMS.I have written this code in Asp.Net using C# but the function can be used with both platform (Win & Web)

To Send Single SMS

Use name Spece
*****************************
using System.Net;
*****************************
Function Block to Send SMS
=============================================================================
public void GetSMSResponse(string mobileno, string msg)
{
// Use Your API Link ( like http://abc.com/pushsms.php?userid=&password=&mobileno=&msg=&rpt=1
string WFLocation = "Your API URL";
string location = WFLocation;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(location);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
}


===========================================================================
Function Block to get credit Limit
===========================================================================
public string getSMSCredit()
{
string WFLocation = "Your API to get SMS Credit";
string location = WFLocation;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(location);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
return results;
}