Monday, March 14, 2016

How to upload picture/file to server/folder

here is the example to upload the file for the various file formate.

you can uplaod the file with the below paradigm, the below programme is to upload the jpg file formate.


protected void btnUpload_Click(object sender, EventArgs e)
    {
        string checkEmpPhoto ;

        checkEmpPhoto = txtEmpid.Text + ".JPG";

        if (upESDPhoto.HasFile)
        {
            string extension = System.IO.Path.GetExtension(upESDPhoto.FileName);
            if (extension.ToUpper() == ".JPG")
            {
                if (checkEmpPhoto.ToString().ToUpper() == upESDPhoto.FileName.ToString().ToUpper())
                {
                     if upload to same application folder
                    upESDPhoto.SaveAs(Server.MapPath("Folder Name") + "\\" + upESDPhoto.FileName);
                 
                    if upload to some other server folder
                    upESDPhoto.SaveAs(@"\\\\Server Name or Server IP\Folder Name" + "\\" + upESDPhoto.FileName);
                    lblMsg.Text = upESDPhoto.FileName + " uploaded Successfully.";
                }
                else
                {
                    lblMsg.Text = "Please check the Photo Name.";
                }
            }
            else if (extension.ToUpper() != ".JPG")
            {
                lblMsg.Text = "Wrong File Formate.";
            }
        }
        else
        {
            lblMsg.Text = "Please select File first.";
            upESDPhoto.Focus();
        }
    }


GridView Row Command

GridviewRowCommand is very useful event of Gridview. User can delete,view details or something else need to do with the selected Item Row within the gridview.

before need to do with rowCommand event, Grid should contain the item template with rowIndex item

<asp:Label id="lnkDocNo" runat="server" Text='<%# Container.DataItemIndex + 1 %>' ></asp:Label>

<asp:LinkButton id="lnkDocNo" runat="server" Text='<%# Eval("DocNo")%>' CommandName ="V" CommandArgument="<%# ((GridViewRow)Container).RowIndex%>"></asp:LinkButton>

on row command property of gridview

event handler of GridView is powerful property of gridview to do various action or to play with data .

protected void onRowCommand(object sender, GridViewCommandEventArgs e)
    {
        int rowIndex = Convert.ToInt16(e.CommandArgument);
        LinkButton lnkDocNo = (LinkButton)GridView.Rows[rowIndex].FindControl("lnkDocNo");

        if (e.CommandName == "V")
        {
           //do the Action with the Data here.
        }
    }