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();
}
}