ASP.NET (35) SQL (25) JAVASCRIPT (24) HTML (14) STYLE SHEET (6) ASP (4) SCRIPT (1)

Search me out HERE!!

Join table data from another Database in Same Instance in SQl Server


Passing dbname in current procedure will display join data of current database table1 data and another database of table2 data.
CREATE PROCEDURE [dbo].[JoinDataFromAnotherDB]
@dbName VARCHAR(200)
AS
DECLARE @sql VARCHAR(max)
SET @sql = ‘SELECT t1.*,t2.*
FROM table1 t1 INNER JOIN ‘+@dbName+’.dbo.table2 t2 ON t1.ID = t2.ID’
EXEC (@sql)

Get screen resolution and set style from javascript

function setLogoOnLowScreenRes()
{
if ((window.screen.width<=800))
{
//you can apply styling for screen resolution 800
document.getElementById("wrapper").style.marginLeft = "58px";
}
else if((window.screen.width<=1024))
{
//you can apply styling for screen resolution 1024
document.getElementById("wrapper").style.marginLeft = "69px";
}
}

To get DataSet from Excel file.


By calling following functions returns dataset

string strErrorMessage = “”;
DataSet ds = GetExcel(path,ref strErrorMessage);
using System.Data.OleDb;
public DataSet GetExcel(string strFileName, ref string strErrorMessage)
{
string strConnection = GetExcelConnectionString(strFileName);
DataTable dtNew = null;
OleDbConnection cnExcelConnection = new OleDbConnection(strConnection);
OleDbCommand cmd = new OleDbCommand();
DataSet ds = new DataSet();
try
{
cnExcelConnection.Open();
dtNew = cnExcelConnection.GetSchema("Tables");
if (dtNew.Rows.Count > 0)
{
foreach(DataRow dr in dtNew.Rows)
{
ds.Tables.Add(ReadExcelData(dr["TABLE_NAME"].ToString(), strConnection, ref strErrorMessage));
}
}
else
{
return null;
}
}
catch (Exception ex)
{
strErrorMessage = ex.Message.ToString();
return null;
}
finally
{
if (!(cnExcelConnection.State == ConnectionState.Closed))
{
cnExcelConnection.Close();
}
cnExcelConnection.Dispose();
}
return ds;
}
public string GetExcelConnectionString(string strFileName)
{
string strConnection = "";
string strFileType = GetFileExtention(strFileName, true);
if (strFileType == ".xlsx")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + (strFileName) + ";" + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
if (strFileType == ".xls")
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + (strFileName) + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
return strConnection;
}
public string GetFileExtention(string strFileName, bool blExtensionRequired)
{
if (strFileName == "")
return strFileName;
string[] slots = strFileName.Split('.');
if (blExtensionRequired)
{
return "." + slots[slots.Length - 1];
}
else
{
return slots[slots.Length - 1];
}
}
public DataTable ReadExcelData(string strSheetName, string strConnection, ref string strErrorMessage)
{
OleDbConnection cnExcelConnection = new OleDbConnection(strConnection);
try
{
cnExcelConnection.Open();
DataTable dtImportedData = new DataTable();
dtImportedData.TableName = strSheetName;
OleDbDataAdapter myData = new OleDbDataAdapter("SELECT * FROM [" + strSheetName + "]", cnExcelConnection);
myData.Fill(dtImportedData);
return dtImportedData;
}
catch (Exception ex)
{
strErrorMessage = ex.Message.ToString();
return null;
}
finally
{
if (!(cnExcelConnection.State == ConnectionState.Closed))
{
cnExcelConnection.Close();
}
cnExcelConnection.Dispose();
}
}

Read Web Page Content from ASP.Net

Below code can be used to Read HTML source of any Website in ASP.Net

string strURL = “http://www.yoururl.com/”; 
Uri uri = new Uri(strURL); 
WebRequest request = HttpWebRequest.Create(uri); 
request.Method = WebRequestMethods.Http.Get; 
WebResponse response = request.GetResponse(); 
StreamReader reader = new StreamReader(response.GetResponseStream()); 
string temp = reader.ReadToEnd(); 
response.Close(); 
Response.Write(temp);

Find IPAddress from host name or URL


Below C# code can be used to get IP Address from Host name or UR

string IPs = string.Empty;
try
{
//performs the DNS lookup
IPHostEntry he = Dns.GetHostByName(“yoururl.com”);
IPAddress[] ip_addrs = he.AddressList;
foreach (IPAddress ip in ip_addrs)
{
IPs += ip + ” “;
}
}
catch (System.Exception ex)
{
}