Sunday, July 10, 2011
Wednesday, April 13, 2011
using System.Net;
string hostName = Dns.GetHostName();
Response.Write("Host Name = " + hostName);
IPHostEntry local = Dns.GetHostByName(hostName);
foreach (IPAddress ipaddress in local.AddressList)
{
Response.Write("IPAddress = " + ipaddress.ToString());
}
How to get connection string from web.config in ASP.Net
public static SqlConnection ConStr = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString);
How to fill datatable dynamically in ASP.Net
public DataTable Fill_Datatable(string strSQL)
{
SqlDataAdapter adp = new SqlDataAdapter(strSQL, ConStr);
DataTable dt = new DataTable();
adp.Fill(dt);
return dt;
}
How to fill gridview dynamically in ASP.Net
public void Fill_Grid(GridView myGrid, string strSQL)
{
SqlDataAdapter adp = new SqlDataAdapter(strSQL, ConStr);
DataTable dt = new DataTable();
adp.Fill(dt);
myGrid.DataSource = dt;
myGrid.DataBind();
}
How to fill dropdownlist dynamically in ASP.Net
public void Fill_DropDownList(DropDownList myDDL, string strSQL, string strDisplayField, string strValueField, string strDefaultText)
{
SqlDataAdapter adp = new SqlDataAdapter(strSQL, ConStr);
DataTable dt = new DataTable();
adp.Fill(dt);
myDDL.DataSource = dt;
myDDL.DataTextField = strDisplayField;
myDDL.DataValueField = strValueField;
myDDL.DataBind();
myDDL.Items.Insert(0, new ListItem(strDefaultText, "0"));
}
How to get aggregate value dynamically in ASP.Net
public string Fill_ScalerValue(string strSQL)
{
string strReturn = string.Empty;
SqlCommand cmd = new SqlCommand(strSQL, ConStr);
ConStr.Open();
strReturn = cmd.ExecuteScalar().ToString();
ConStr.Close();
return strReturn;
}
How to execute SQL query in ASP.Net
public void ExecuteQuery(string strSQL)
{
SqlCommand cmd = new strSQL, ConStr);
ConStr.Open();
cmd.ExecuteNonQuery();
ConStr.Close();
}
IsNull function in ASP.Net
public string ISNull(string strValue, string strReplaceValue)
{
if (strValue == string.Empty)
{
strValue = strReplaceValue;
}
return strValue;
}
IIF function in ASP.Net
public string IIF(bool strValue, string strThen, string strElse)
{
return (strValue ? strThen : strElse );
}
Upload File function in ASP.Net
public void UploadFiles(FileUpload myFileUpload, string strPath)
{
if (myFileUpload.HasFile)
{
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
myFileUpload.PostedFile.SaveAs(strPath +
myFileUpload.FileName);
}
}
CLS function in ASP. Net
public void Xcls(Control prnt)
{
foreach (Control c in prnt.Controls)
{
if (c.Controls.Count > 0)
{
Xcls(c);
}
else
{
switch (c.GetType().ToString())
{
case "System.Web.UI.WebControls.Label":
((Label)c).Text = "";
break;
case "System.Web.UI.WebControls.TextBox":
((TextBox)c).Text = "";
break;
case "System.Web.UI.WebControls.DropDownList":
((DropDownList)c).Text="";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)c).Checked = false;
break;
}
}
}
}
How to send email in ASP. Net
public string SendEmail(string strFrom, string strTo,
string strSubject,string strMessage, string strSMTPServer,
int strPort, string strUserid, string strPassowrd,
bool strEnableSSL, string SuccessMessage)
{
try
{
SmtpClient smtpClnt = new SmtpClient
(strSMTPServer, strPort);
smtpClnt.Credentials = new System.Net.NetworkCredential
(strUserid, strPassowrd);
smtpClnt.EnableSsl = strEnableSSL;
smtpClnt.Send(strFrom, strTo, strSubject, strMessage);
return SuccessMessage;
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
Friday, March 4, 2011
Grid View Sorting / Paging
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex= e.NewPageIndex;
BindGrid();
}
protected voidGridView1_Sorting(object sender,GridViewSortEventArgs e)
{
DataView dv = new DataView(new Udf().Fill_Datatable("SELECT* FROM [Users]"));
dv.Sort =e.SortExpression + " " +new Udf().IIF((e.SortDirection==SortDirection.Ascending),"
asc"," desc");
GridView1.DataBind();
}
Friday, December 31, 2010
Friday, June 18, 2010
Email Sending Function
Namespace :
using System.Net.Mail;
Function # 1:
public void SendEmail_ByHostSettings(string mySubject,string myMessageBody,string Email_To)
{
MailMessage mm = new MailMessage(Convert.ToString(DotNetNuke.Common.Globals.HostSettings["HostEmail"]), Email_To, mySubject, myMessageBody);
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPServer"]));
if (DotNetNuke.Common.Globals.HostSettings["SMTPEnableSSL"].ToString() == "Y")
{
smtp.EnableSsl = true;
}
else
{
smtp.EnableSsl = false;
}
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPUsername"]), Convert.ToString(DotNetNuke.Common.Globals.HostSettings["SMTPPassword"])); //From user credentails
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mm);
}
Function # 2:
public void SendEmail(string mySubject, string myMessageBody, string Email_To, string Email_From, string SMTPServer, bool EnableSsl, string SMTPUsername, string SMTPPassword)
{
MailMessage mm = new MailMessage(Email_From, Email_To, mySubject, myMessageBody);
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(SMTPServer);
smtp.EnableSsl = EnableSsl;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(SMTPUsername,SMTPPassword);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mm);
}
How to Call:
new SendEmail_ByHostSettings("TEST MAIL", "Thanks for your interest", "razarajwani@live.com");
DNN Parametrized Redirection
Function :
public string GetTabIDURL(int PortalID, string ModuleName)
{ DataTable dt = new udf.Udf().Fill_DataTable("SELECT * FROM vw_Modules where modulename='" + ModuleName + "' and portalid=" + PortalID + " and tabid is not null");
return "~/tabid/" + dt.Rows[0]["tabid"].ToString() + "/default.aspx";
}
How to Call:
protected void GoToDetail_Click(object sender, EventArgs e)
{
int JobID = Convert.ToInt32((((LinkButton)sender).CommandArgument));
Response.Redirect(new udf.Udf().GetTabIDURL(this.PortalId, "bl_JobDisplay").ToString() + "?JobID=" + JobID);
}