Tuesday, July 23, 2013

Rating using AjaxToolkit, C#










Style For Rating tool
 {
            font-size: 0pt;
            width: 13px;
            height: 12px;
            margin: 0px;
            padding: 0px;
            cursor: pointer;
            display: block;
            background-repeat: no-repeat;
        }
     
        .filledRatingStar
        {
            background-image: url(Images/FilledStar.png);
        }
     
        .emptyRatingStar
        {
            background-image: url(Images/EmptyStar.png);
        }
     
        .savedRatingStar
        {
            background-image: url(Images/SavedStar.png);

        }


Rating Control
  
                                               <asp:Rating ID="Rating1" runat="server"
                                         CurrentRating="0" EmptyStarCssClass="emptyRatingStar"
                                         FilledStarCssClass="filledRatingStar" OnChanged="Rating1_Changed" StarCssClass="ratingStar"
                                         Style="float:left;" WaitingStarCssClass="savedRatingStar"
                                         Tag='<%# Eval("fid") %>'>
                                             

                                              </asp:Rating>


Images







Code


 rotected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        //Rating code

        string str = "insert into tbl_rate(r_p_id,r_rate) values(" + e.Tag + "," + e.Value + ")";
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();

        //Fill Data List

        string sel = "select * from tbl_image";
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter(sel, con);
        adp.Fill(dt);
        dlstImage.DataSource = dt;
        dlstImage.DataBind();

     

    }
    protected void dlstImage_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        HiddenField h = (HiddenField)e.Item.FindControl("HiddenField1");
        Rating re = (Rating)e.Item.FindControl("Rating1");
        string sel = "select AVG(r_rate) as rating from tbl_rate where r_p_id=" + h.Value + "";
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter(sel, con);
        adp.Fill(dt);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["rating"] != DBNull.Value)
                {
                    re.CurrentRating = Convert.ToInt32(dt.Rows[0]["rating"]);
                }
            }
        }

    }



Monday, July 22, 2013

Password Encryption and Decryption In C#

Code

using System.Text;


    protected void btnOk_Click(object sender, EventArgs e)
    {
        string strOriginal = txtPassword.Text;
        string strEncrypted = Encryptdata(strOriginal);
        string strDecrypted = Decryptdata(strEncrypted);
        lblOriginal.Text  = strOriginal;
        lblEncrypted.Text = strEncrypted;
        lblDecrypted.Text  = strDecrypted;

    }
    private string Encryptdata(string password)
    {
        string strmsg = string.Empty;
        byte[] encode = new byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        strmsg = Convert.ToBase64String(encode);
        return strmsg;
    }
    private string Decryptdata(string encryptpwd)
    {
        string decryptpwd = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new String(decoded_char);
        return decryptpwd;

    }

Wednesday, July 10, 2013

Split and Replace Functions Asp.net C#

public partial class ReplaceSplit : System.Web.UI.Page
{
    Cls_ADO obj = new Cls_ADO();
    static int flag, idno;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            fillDOB();
            fillSqn();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string name = txtuname.Text;
        string birth=ddlDD.Text+"/"+ddlMM.Text+"/"+ddlYY.Text;
        string squet = ddlQn.SelectedValue;

 squet = squet.Replace("'", "$");  /* here '  repalce with $ , because ' split the query , eg: squet = squet.Replace("'", "’’");*/


        if (flag == 1)
        {
            string upqry = "update tbl_reg set uname='" + name + "',dob='" + birth + "',sqn='" + squet + "' where uid=" + idno + "";
            obj.execute_Query(upqry);
            flag = 0;
        }
        else
        {
            string inqry = "insert into tbl_reg(uname,dob,sqn)values('" + name + "','" + birth + "','" + squet + "')";
            obj.execute_Query(inqry);
        }
       

        string selqry = "select * from tbl_reg";
        obj.fillGrid(grdDetails, selqry);

    }
protected void  btnShow_Click(object sender, EventArgs e)
{
    string selqry="select * from tbl_reg";
    obj.fillGrid(grdDetails, selqry);
}
    protected void fillDOB()
    {
        int i;
        for (i = 1; i <= 31; i++)
        {
            ddlDD.Items.Add(i.ToString());
        }
        for (i = 1; i <= 12; i++)
        {
            ddlMM.Items.Add(i.ToString());
        }
        for (i = 1990; i <= 2000; i++)
        {
            ddlYY.Items.Add(i.ToString());
        }
    }
    protected void fillSqn()
    {
        ddlQn.Items.Insert(0, "--select--");
        ddlQn.Items.Add("U r pet's name ?");
        ddlQn.Items.Add("Favourite Color ?");


   
       
    }
    protected void grdDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "editme")
        {
            
            flag = 1;
            idno = Convert.ToInt32(e.CommandArgument.ToString());
            string str = "select * from tbl_reg where uid=" + idno + "";
            DataTable dt = obj.select_Query(str);


            if (dt != null)
            {

                txtuname.Text = dt.Rows[0]["uname"].ToString();
               

          string squet = dt.Rows[0]["sqn"].ToString();
     
ddlQn.SelectedValue = squet.Replace("$", "'"); // here $ repalce with '

                string[] s1 = new string[3];
                string brth = dt.Rows[0]["dob"].ToString();
                 s1 = brth.Split('/'); // here split the string based on /
                ddlDD.SelectedValue = s1[0];
                ddlMM.SelectedValue = s1[1];
                ddlYY.SelectedValue = s1[2];

            }
        }
    }
}


Monday, July 8, 2013

Upload and Download Files in ASP.Net C#


protected void btnSave_Click(object sender, EventArgs e)
    {
       
string fname = Path.GetFileName(flupUpload.PostedFile.FileName.ToString());
string ext = Path.GetExtension(fname);  // to get extension of file
       
if (flupUpload.HasFile)
  {

flupUpload.SaveAs(Server.MapPath("fileCollection/" + fname));
string ins = "insert into tbl_upload(file_caption,file_name)values('" + txtcaption.Text + "','" + fname + "')";
obj.execute_Query(ins);
 }
       
}
    protected void btnShow_Click(object sender, EventArgs e)
    {
        string sel = "select * from tbl_upload";
        obj.fillGrid(grdFiles, sel);
    }










    protected void grdFiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "download")
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            string str = "select * from tbl_upload where file_id=" + id + "";
            DataTable dt = new DataTable();
            dt = obj.select_Query(str);
            if (dt.Rows.Count > 0)
            {
                string name = dt.Rows[0]["file_name"].ToString();
               
    filedownload(name);
            }
        }
    }


protected void filedownload(string name)
 {
         Response.AppendHeader("content-disposition","attachment;filename=" + name + "");
        Response.TransmitFile(Server.MapPath("fileCollection/" + name));
        Response.End();
    }
}


Media Player using ASP.net C#


protected void btnSave_Click(object sender, EventArgs e)
    {
        string fname = Path.GetFileName(flupUpload.PostedFile.FileName.ToString());
        string ext = Path.GetExtension(fname);
       
            if (flupUpload.HasFile)
            {
                flupUpload.SaveAs(Server.MapPath("fileCollection/" + fname));
                string ins = "insert into tbl_upload(file_caption,file_name)values('" + txtcaption.Text + "','" + fname + "')";
                obj.execute_Query(ins);
            }
       
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
        string sel = "select * from tbl_upload";
        obj.fillGrid(grdFiles, sel);
    }

    protected void grdFiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "play")
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            Response.Redirect("Player.aspx?fid=" + id + "");
        }
    }
  
}

Player Page
public partial class MediaPlayer_Player : System.Web.UI.Page
{
    public static string videoname;
    Cls_ADO obj = new Cls_ADO();
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["fid"]);
        string str = "select * from tbl_upload where file_id=" + id + "";
        DataTable dt = new DataTable();
        dt = obj.select_Query(str);
        if (dt.Rows.Count > 0)
        {
            videoname = dt.Rows[0]["file_name"].ToString();
        }
    }
}

Source Code
<table class="style1">
            <tr>
                <td>
                    <object id="mediaPlayer" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"
                        codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
                        standby="Loading Microsoft Windows Media Player components..."
                        style="width: 846px; height: 416px;" type="application/x-oleobject">
        <param name="fileName" value="fileCollection/<%=videoname %>" />
                        <param name="animationatStart" value="true" />
                        <param name="transparentatStart" value="true" />
                        <param name="autoStart" value="true" />
                        <param name="showControls" value="true" />
                        <param name="loop" value="true" />
   <embed id="EMBED1" autosize="-1" autostart="true" bgcolor="darkblue"
                            designtimesp="5311" displaysize="4" height="285" loop="true" name="mediaPlayer"
                            pluginspage="http://microsoft.com/windows/mediaplayer/en/download/"
                            showcontrols="true" showdisplay="0" showstatusbar="-1" showtracker="-1"
                         
src="fileCollection/<%=videoname %>"
type="application/x-mplayer2" videoborder3d="-1" width="320"></embed>
      </EMBED>
      </object>
                </td>
            </tr>

        </table>