Wednesday, November 2, 2011

How to Convert Image as Binary and Binary to Image

string StrExtension = Path.GetExtension(FUHeaderLogo.PostedFile.FileName.ToString());
byte[] ImgBinary = System.IO.File.ReadAllBytes(FUHeaderLogo.PostedFile.FileName.ToString());

Method 1

void GetPrescriptionImages(int AppId, int CenterId)
{

DataTable DtImgs = ObjAppointmentBL.GetPrescriptionImages(AppId, CenterId);
if (DtImgs.Rows.Count > 0)
{
Session["PrescriptionUrl"] = DtImgs.Rows[0]["PrescriptionUrl"].ToString();
foreach (DataRow _Row in DtImgs.Rows)
{
string[] _Url = _Row[1].ToString().Split('\\');
string DirectoryPath = System.Web.Hosting.HostingEnvironment.MapPath("~/ScanImagePrescriptions/") + _Url[2].ToString() + "\\";
if (Directory.Exists(DirectoryPath) == false)
{
Directory.CreateDirectory(DirectoryPath);
}
if (!File.Exists(DirectoryPath + _Url[3].ToString()))
{
byte[] _Img = (byte[])_Row[2];
MemoryStream ms = new MemoryStream(_Img);
FileStream fs = new FileStream
 (DirectoryPath + _Url[3].ToString(), FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();
}
}
}
else
Session["PrescriptionUrl"] = "";
}

Method 2

void CreateBinayImages(int CenterId, string filename, DataTable DtBinaryImgs)
{
try
{
if (DtBinaryImgs.Rows.Count > 0)
{
string DirectoryPath = filename.Replace("DownLoadReports.pdf", "");
byte[] _Img = (byte[])DtBinaryImgs.Rows[0]["ImgHeaderBinayByte"];
MemoryStream ms = new MemoryStream(_Img);
FileStream fs = new FileStream
 (DirectoryPath + Convert.ToString(CenterId) + DtBinaryImgs.Rows[0]["ImgExtension"].ToString(), FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();
}
}
catch (Exception Ex)
{
}
}

Method 3

public void GetLoadedImages()
{
try
{
DataSet DsImages = ObjUsersBL.SelectDataSet(Convert.ToString(Session["_ConString"]), "Select ImgHeaderBinayByte,ImgExtension from tblCentreSelection where Centreid='" + Session["_SelCen"] + "'");
if (DsImages.Tables[0].Rows.Count > 0)
{
string DirectoryPath = System.Web.Hosting.HostingEnvironment.MapPath("~/HeaderLogos/");
byte[] _Img = (byte[])DsImages.Tables[0].Rows[0]["ImgHeaderBinayByte"];
MemoryStream ms = new MemoryStream(_Img);
FileStream fs = new FileStream
 (DirectoryPath + "\\" + Session["_SelCen"].ToString() + DsImages.Tables[0].Rows[0]["ImgExtension"].ToString(), FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();
if (File.Exists(DirectoryPath + Session["_SelCen"].ToString() + DsImages.Tables[0].Rows[0]["ImgExtension"].ToString()))
{
ImgView.Visible = true;
ImgView.ImageUrl = "~/HeaderLogos/" + Session["_SelCen"].ToString() + DsImages.Tables[0].Rows[0]["ImgExtension"].ToString();
}
}
}
catch (Exception Ex)
{
log.Info("GetLoadedImages() : ADMIN : ImgHeaderLogoUpdater.aspx", Ex);
}
}

Tuesday, October 25, 2011

List Declaration in Different Types

List Declaration


List<Locations> ObjLoc = Obj;

            GridView1.DataSource = (from L in ObjLoc select L);
      GridView1.DataBind();


List<Locations> Obj
        {
            get
            {
                List<Locations> ObjLoc = new List<Locations>{
                new Locations { Id=1, Name="A", Age=21,Phone=021},
                new Locations { Id=2, Name="B", Age=22,Phone=022},
                new Locations { Id=3, Name="C", Age=23,Phone=023},
                new Locations { Id=4, Name="D", Age=24,Phone=024},
                new Locations { Id=5, Name="E", Age=25,Phone=025},
                new Locations { Id=6, Name="F", Age=26,Phone=026},
                new Locations { Id=7, Name="G", Age=27,Phone=027},
                new Locations { Id=8, Name="H", Age=28,Phone=028},
                new Locations { Id=9, Name="I", Age=29,Phone=029},
                new Locations { Id=10, Name="J", Age=30,Phone=030},
                new Locations { Id=11, Name="K", Age=31,Phone=031},
                new Locations { Id=12, Name="L", Age=32,Phone=032},
                new Locations { Id=13, Name="M", Age=33,Phone=033},
                new Locations { Id=14, Name="M", Age=34,Phone=034}
            };
                return ObjLoc;
            }
        }

Wednesday, October 19, 2011

How to Read Key & Values From AppSettings and Assign in List

public class URLs
    {
        public string Name { get; set; }
        public string Url { get; set; }
    }

    void BindGrid()
        {
            List<URLs> ObjUrls = new List<URLs>();
            foreach (string key in ConfigurationSettings.AppSettings)
            {
                string value = ConfigurationManager.AppSettings[key];
                ObjUrls.Add(new URLs() { Name = key, Url = value });
            }
            if (ObjUrls.Count > 0)
            {
                GrdAppUrls.DataSource = ObjUrls;
                GrdAppUrls.DataBind();
            }
        }

Friday, October 14, 2011

String Replace and Sorting in Java Script

<script language="javascript" type="text/javascript">

var HdnChkIds = '';
var ImgPathList = '';
function GetChkValues(chk, ImgPath) {
var ImgPaths = replaceAll(ImgPath, '~', '\\');
if (chk.checked == true) {
ImgPathList = document.getElementById('<%= HdnImgPaths.ClientID %>').value += ImgPaths + '^';
HdnChkIds = document.getElementById('<%= HdnChkBoxValues.ClientID %>').value += parseInt(chk.id) + 1 + ',';
var Ids_array = document.getElementById('<%= HdnChkBoxValues.ClientID %>').value.split(",");
document.getElementById('<%= LblSelectedImgs.ClientID %>').style.display = "block";
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').style.display = "block";
document.getElementById('<%= LblCheckImgs.ClientID %>').style.display = "block";
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').style.display = "block";
var Sorting = HdnChkIds.substring(0, HdnChkIds.length - 1).split(',');
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').innerText = Sorting.sort(sortNumber);
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').innerText = Ids_array.length - 1;
    }
    else
{
HdnChkIds = document.getElementById('<%= HdnChkBoxValues.ClientID %>').value.replace(parseInt(chk.id) + 1 + ',', "");
document.getElementById('<%= HdnChkBoxValues.ClientID %>').value = HdnChkIds;
var Ids_array = document.getElementById('<%= HdnChkBoxValues.ClientID %>').value.split(",");

ImgPathList = document.getElementById('<%= HdnImgPaths.ClientID %>').value.replace(ImgPaths + '^', "");
document.getElementById('<%= HdnImgPaths.ClientID %>').value = ImgPathList;

if (Ids_array.length - 1 != 0) {
var Sorting = HdnChkIds.substring(0, HdnChkIds.length - 1).split(',');
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').innerText = Sorting.sort(sortNumber);
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').innerText = Ids_array.length - 1;
     }
else {
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').innerText = "0";
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').innerText = "0";
document.getElementById('<%= LblSelectedImgs.ClientID %>').style.display = "none";
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').style.display = "none";
document.getElementById('<%= LblCheckImgs.ClientID %>').style.display = "none";
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').style.display = "none";
                }
            }
        }

function checkedAll(checkAllState, cbGroup) {

var Status = 0;
            // Check that the group has more than one element
            if (checkAllState.checked == true) {
                Status = 1;
            }
           
if (cbGroup.length > 0) {
                // Loop through the array
                for (i = 0; i < cbGroup.length; i++) {
                    cbGroup[i].checked = checkAllState.checked;
                }
               
if (Status == 1) {
document.getElementById('<%= HdnImgPaths.ClientID %>').value = document.getElementById('<%= HdnChkAllImgPaths.ClientID %>').value;
HdnChkIds = document.getElementById('<%= HdnChkBoxValues.ClientID %>').value = document.getElementById('<%= HdnChkAllImgIds.ClientID %>').value;
var Sorting = HdnChkIds.substring(0, HdnChkIds.length - 1).split(',');
document.getElementById('<%= LblSelectedImgs.ClientID %>').style.display = "block";
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').style.display = "block";
document.getElementById('<%= LblCheckImgs.ClientID %>').style.display = "block";
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').style.display = "block";
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').innerText = Sorting.sort(sortNumber);
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').innerText = document.getElementById('<%= TotalCountImgsShow.ClientID %>').innerText;
                }
               
if (Status == 0) {
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').innerText = "0";
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').innerText = "0";
document.getElementById('<%= LblSelectedImgs.ClientID %>').style.display = "none";
document.getElementById('<%= LblSelectedImgsShow.ClientID %>').style.display = "none";
document.getElementById('<%= LblCheckImgs.ClientID %>').style.display = "none";
document.getElementById('<%= LblCheckImgsCountShow.ClientID %>').style.display = "none";
document.getElementById('<%= HdnImgPaths.ClientID %>').value = "";
document.getElementById('<%= HdnChkBoxValues.ClientID %>').value = "";
                }
            }
            else {
                // Single element so not an array
                cbGroup.checked = checkAllState.checked;
            }

        }


function replaceAll(txt, replace, with_this) { return txt.replace(new RegExp(replace, 'g'), with_this); }
        function sortNumber(a, b) { return a - b; }       
   
</script>

Wednesday, October 12, 2011

Get Current Pc IP Address in Asp.Net

string IP = HttpContext.Current.Request.UserHostAddress+ '-' + Request.Browser.Platform.ToString() + '-' + Request.Browser.Browser.ToString() + '-' + System.Net.Dns.GetHostName() + '-' + DateTime.Now + DateTime.Now.Minute + DateTime.Now.Millisecond + "-" + strName;

protected string GetLocalPCIP()
{
string LocalIP = null;
System.Net.IPHostEntry IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress IPAddress in IPHostEntry.AddressList)
{
if (IPAddress.AddressFamily.ToString() == "InterNetwork")
{
LocalIP = IPAddress.ToString();
}
}
return LocalIP;
}

String Encoding & Decoding

string DBDetails = "ashok";
string _DBString = Convert.ToBase64String(Encoding.Unicode.GetBytes(DBDetails));

byte[] stringBytes = Convert.FromBase64String(_DBString);
string decodedViewState = System.Text.Encoding.ASCII.GetString(stringBytes);
string Name = decodedViewState.Replace("\0", "");

Tuesday, October 11, 2011

How to Get Webservice Url from Web.config




this.Url = System.Configuration.ConfigurationSettings.AppSettings["WebService"].ToString();