Monday, June 16, 2014

Password Encryption Code

Code to encrypt a password
public string ecrypt(string pwd)
{
string temp = "";
int len = pwd.Length;
if (len == 5)
{
temp = pwd + pwd.Substring((len - 3), 3);
}
else if (len == 6)
{
temp = pwd + pwd.Substring((len - 2), 2);
}
else if (len == 7)
{
temp = pwd + pwd.Substring((len - 7), 1);
}
char[] chartemparr = new char[temp.Length];
chartemparr=temp.ToCharArray();
int[] inttemparr = new int[chartemparr.Length];
 //chartemparr.CopyTo(inttemparr, 0);
int i=0,j=0;

for (i = 0; i < temp = "" i =" 0;" j =" inttemparr[i]+1;">);
}

return temp;
}

MDI Parent child relation in windows application.

This post is for MDI Parent child relation in windows application.
the code detects whether child form is already opened or not.
If already opened then it did not allow to create a new child and reopens (maximise) the existed form.

here is the code for that
on click event of menu
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
// to check whether form is opened
if (!CheckExistingForm("VanMaster"))
{
VanMaster vanMaster = new VanMaster(this);
vanMaster.Show();
}
}
******************************
// method to check if propvided form is exists or open before
private bool CheckExistingForm(string formName)
{
int flag = 0;
Form[] forms = this.MdiChildren;
if (forms.Length > 0)
{
for (int i = 0; i < forms.Length; i++)
{
if (forms[i].Name == formName)
{
forms[i].WindowState = FormWindowState.Normal;
flag = 1;
break;
}
}
if (flag == 1)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

**********************************************************8
at VanMaster.cs file



public partial class VanMaster : Form
    {
        public VanMaster(Form Parent)
        {
            InitializeComponent();
            this.Parent = Parent;
        }
    }

Code to Open pdf/doc/any file

This post is for opening any document in website
// function to open file
protected void OpenDocument()
{
// Get the physical Path of the file(test.doc)
string filepath = Server.MapPath("Document Location");

// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo file = new FileInfo(filepath);

// Checking if file exists
if (file.Exists)
{
// Clear the content of the response
Response.ClearContent();

// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

// Add the file size into the response header
Response.AddHeader("Content-Length", file.Length.ToString());

// Set the ContentType
Response.ContentType = ReturnExtension(file.Extension.ToLower());

// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(file.FullName);

// End the response
Response.End();
}
}
// Function to get extenstion of the file
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}

Implementing Paging In Repeater

aspx Page


  <asp:Repeater ID="rptPages" runat="server" OnItemCommand="rptPages_ItemCommand">
                            <HeaderTemplate>
                                <table cellpadding="0" cellspacing="0" border="0">
                                    <tr class="text">
                                        <td>
                                            <b>Page:</b>&nbsp;</td>
                                        <td>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%#
                         Container.DataItem %>" CssClass="text" runat="server"><%# Container.DataItem %>
                                </asp:LinkButton>&nbsp;
                            </ItemTemplate>
                            <FooterTemplate>
                                </td> </tr> </table>
                            </FooterTemplate>
                        </asp:Repeater>
                        <asp:Repeater ID="rptItems" runat="server">
                            <HeaderTemplate>
                            
                            </HeaderTemplate>
                            <ItemTemplate>
                                <table>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblquestion" runat="server" Text="Question:" Font-Bold="True" ForeColor="Red"></asp:Label>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblques" runat="server" Text='<%#Eval("Question") %>'></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblans" runat="server" Text="Answer:" Font-Bold="True" ForeColor="Red"></asp:Label>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblanswer" runat="server" Text='<%#Eval("Answer") %>'></asp:Label>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                            <FooterTemplate>
                            </FooterTemplate>
                        </asp:Repeater>


C# File

  public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }
  protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        LoadData();
    }
    private void LoadData()
    {
        DataTable dt = YatraGuruBL.Interface.Methods.Help.Get();

        PagedDataSource pgitems = new PagedDataSource();
        DataView dv = new DataView(dt);
        pgitems.DataSource = dv;
        pgitems.AllowPaging = true;
        pgitems.PageSize = 3;
        pgitems.CurrentPageIndex = PageNumber;
        if (pgitems.PageCount &gt; 1)
        {
            rptPages.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i &lt; pgitems.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPages.DataSource = pages;
            rptPages.DataBind();
        }
        else
            rptPages.Visible = false;
        rptItems.DataSource = pgitems;
        rptItems.DataBind();
    }

Binding Tree From Database

in aspx Page


  <asp:TreeView ID="TreeView1" runat="server">
            </asp:TreeView>

-----------------------------------------------------------------------------------------------------

in  Source Code (c#) page


 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
            BindTreeView(null,0);
        }
    }
    protected void GetData()
    {
        DataTable dt = new DataTable();
        string Constring = @"Data Source=MICROSOF-SXZEHI;Initial Catalog=test;Integrated Security=SSPI;";
        SqlConnection con = new SqlConnection(Constring);
        SqlDataAdapter da = new SqlDataAdapter("Select * from tree_Test", con);
        da.Fill(dt);
    
        ViewState["dtTree"] = dt;
    }
    protected void BindTreeView(TreeNode tn, int PaerntId)
    {
        DataTable dtTree = (DataTable)ViewState["dtTree"];
        DataView dvTree = new DataView(dtTree);
        dvTree.RowFilter = "parentid=" + PaerntId;
        if (dvTree.Count > 0)
        {
            for (int iLoop = 0; iLoop < dvTree.Count; iLoop++)
            {
                TreeNode tnn = new TreeNode(dvTree[iLoop]["name"].ToString(), dvTree[iLoop]["id"].ToString());
                BindTreeView(tnn, Convert.ToInt32(dvTree[iLoop]["id"].ToString()));
                if (tn == null)
                    TreeView1.Nodes.Add(tnn);
                else
                    tn.ChildNodes.Add(tnn);
            }
        }
    }


Binding Menu from Database

in aspx page



   <asp:Menu ID="Menu1" runat="server">
            </asp:Menu>


-------------------------------------------------------------------------------------------

In  Source Code (c#) page




protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();    
            BindMenu(null, 0);
        }
    }
    protected void GetData()
    {
        DataTable dt = new DataTable();
        string Constring = @"Data Source=MICROSOF-SXZEHI;Initial Catalog=test;Integrated Security=SSPI;";
        SqlConnection con = new SqlConnection(Constring);
        SqlDataAdapter da = new SqlDataAdapter("Select * from tree_Test", con);
        da.Fill(dt);
      
        ViewState["dtTree"] = dt;
    }

protected void BindMenu(MenuItem mn, int ParentId)
    {
        DataTable dtTree = (DataTable)ViewState["dtTree"];
        DataView dvTree = new DataView(dtTree);
        dvTree.RowFilter = "parentid=" + ParentId;
        if (dvTree.Count > 0)
        {
            for (int iLoop = 0; iLoop < dvTree.Count; iLoop++)
            {
                MenuItem mnn = new MenuItem(dvTree[iLoop]["name"].ToString(), dvTree[iLoop]["id"].ToString());
                BindMenu(mnn, Convert.ToInt32(dvTree[iLoop]["id"].ToString()));
                if (mn == null)
                    Menu1.Items.Add(mnn);
                else
                    mn.ChildItems.Add(mnn);
            }
        }
    }



Inserting Multiple Row in database in one time


alter PROCEDURE  [dbo].[p_InsertDetail]
(
@Combined nvarchar(200)
)
as
begin try
begin tran
Declare @ii int,@jj int,@AllId nvarchar(max)
Declare @iItemId int,@Item varchar(200),@Quantity int,@UnitPrice float,@Total float
set @ii=-1
set @jj=-1
while(@ii<>len(@Combined) and len(@Combined)<>0)
Begin
set @AllId=substring(@Combined,@ii+1,CharIndex('/',@Combined,@ii+1)-@ii-1)
set @ii=CharIndex('/',@Combined,@ii+1)
while(@jj<>len(@AllId) and len(@AllId)<>0)
Begin
set @iItemId=convert(int,substring(@AllId,@jj+1,charIndex(',',@AllId,@jj+1)-@jj-1))
Set @jj=CharIndex(',',@AllId,@jj+1)
set @Item=substring(@AllId,@jj+1,charIndex(',',@AllId,@jj+1)-@jj-1)
Set @jj=CharIndex(',',@AllId,@jj+1)
set @Quantity=convert(int,substring(@AllId,@jj+1,charIndex(',',@AllId,@jj+1)-@jj-1))
Set @jj=CharIndex(',',@AllId,@jj+1)
set @UnitPrice=convert(float,substring(@AllId,@jj+1,charIndex(',',@AllId,@jj+1)-@jj-1))
Set @jj=CharIndex(',',@AllId,@jj+1)
set @Total=convert(float,substring(@AllId,@jj+1,charIndex(',',@AllId,@jj+1)-@jj-1))
Set @jj=CharIndex(',',@AllId,@jj+1)
    Insert into t_ItemDetail(ItemId,Item,Quantity,UnitPrice,Total)
    values(@iItemId,@Item,@Quantity,@UnitPrice,@Total)

End    
Set @jj=-1
End
commit tran
end try
begin catch
rollback tran
end catch



---- 1,Item1,5,10.0,50.0,/2,Item2,2,10.30,20.60,/3,Item3,5,11,55,/4,Item4,6,20,120,/