Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when am inserting the image it is storing in DB but Viewing in folder
but i have to view from DB only what can i do?



this is my code ....
C#
protected void Page_Load(object sender, EventArgs e)
        {
            selectnode();
            //SelectedNode = TreeView1.SelectedNode.Text;
            //TreeView1.Nodes node = TreeView1.FindNode("NMBM").Text;
            //TreeView1.SelectedNode.Text = TreeView1.FindNode("NMBM").Text;
            //TreeView1.FindNode("NMBM");
            clsUser LogedinUser = new clsUser();
            List<clspageobj> ChkList = new List<clspageobj>(); 
              ChkList=  LogedinUser.UserRolesObj(Session["UserPkid"].ToString(), Session["UserRolePkid"].ToString(), Session["UserLocationPkid"].ToString());
              var b = from ch in ChkList
                      where ch.obj_name == "wpaLocationDetails.aspx"
                      select ch.obj_status;

              bool testFrmval = (bool)b.Single();

              var SaveBtn = from ch in ChkList
                            where ch.obj_name == "btnMulSave"
                      select ch.obj_status;

              bool testSaveBtntval = (bool)SaveBtn.Single();
              if (!(bool)SaveBtn.Single())
              {
                  btnMulSave.Enabled = false;
              }
            if (IsPostBack)
            {
 
            }

            if (!IsPostBack)
            {
                Addnew();
                fill_Tree2();
                addparenttolist();
                TreeView1.ExpandAll();
                pnlright.Enabled = false;
                rightpanelbtn(false);
                leftpanelbtn(true);
                msgmulliteral.Text = "";
                clsUser MyUser = new clsUser();
                MyUser.BrowseUser(ddlmanager);
            }
            //retriveolddata(SelectedNode);
 
            setPageProperties("wpaLocationsDetails");

        }

        public bool checkFileExtension(string fname) 
        {

            if (fname.IndexOf(".") == -1)
                return false;

        string[] validExtensions = new string[5];

            

        string ext = fname.Substring(fname.LastIndexOf('.') + 1).ToLower();
        //Add valid extentions in this array
        validExtensions[0] = "jpg";
        //validExtensions[1] = 'pdf';
        for (int i = 0; i < validExtensions.Length; i++)
        {
            if (ext == validExtensions[0].ToString())
                return true;
        }

        return false;
        }


        IMA.DataClasses1DataContext forselectdb = new IMA.DataClasses1DataContext();
        private void Addnew()
        {
            //For getting the LocationID

            string test = string.Empty;
            var nodes =
                from nd in forselectdb.tbllocations
                 orderby nd.locid descending
                 select new { nd.locid, nd.locname, nd.locparentid, nd.locshortdesc };

            foreach (var lo in nodes.Take(1))
            {
                test = lo.locid.ToString();
            }

            string a = "";
            int tempid = Convert.ToInt32(test.Remove(0, 2)) + 1;
            if (tempid < 10)
            {
                a = "AA000" + tempid.ToString();
            }
            else if (tempid < 100)
            {
                a = "AA00" + tempid.ToString();
            }

            else if (tempid < 1000)
            {
                a = "AA0" + tempid.ToString();
            }

            else if (tempid < 10000)
            {
                a = "AA" + tempid.ToString();
            }

            txtMunicipalID.Text = a.ToString();
            txtMunicipalID.DataBind();
        }

        public clsLocation mylocation = new clsLocation();
        protected void btnMulSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtMulName.Text == "")
            {
                string str1 = "Name field cannot be left blank!!!";
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + str1 + "');", true);
            }
            else
            {
                if (txtMulName.Text == ddlparent.Text)
                {
                    msgmulliteral.Text = "Municipal Name and its Parent Node cannot be same.";
                }

                else
                {

                    if (fileup.HasFile)
                    {
                        file = fileup.FileName;
                        bool ans = checkFileExtension(file);
                        if (ans)
                        {


                            try
                            {

                                fileadd = (Server.MapPath("Image\\") + txtMulShortName.Text + "." + file.Substring(file.LastIndexOf(".") + 1).ToString());
                                fileup.SaveAs(fileadd);
                            }
                            catch (Exception ex) { }
                            locLogo.ImageUrl = "~/Image/" + txtMulName.Text + "." + file.Substring(file.LastIndexOf(".") + 1).ToString();
                        }
                        else
                        {
                            msgmulliteral.Text = "Invalid file type of Logo, select only JPG files";
                        }</clspageobj></clspageobj>
Posted
Updated 16-Feb-12 21:11pm
v2

To store image in DB refer this article

Store or Save images in SQL Server[^]


C#
fileadd = (Server.MapPath("Image\\") + txtMulShortName.Text + "." + file.Substring(file.LastIndexOf(".") + 1).ToString());
fileup.SaveAs(fileadd);


this is code for creating the image in folder comment if u dont want it
 
Share this answer
 
Hi,

See my Article if could help...

Display/Store and Retrieve Image Data from Database to Gridview

Please do not forget to vote if could help...

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900