Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;

public partial class ImageUpLoader : System.Web.UI.Page
{
    string sqlstr;
    static string path;
    string strFileName1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    public void uploadDocument()
    {

        if ((FileUpload1.PostedFile != null))
        {

            HttpPostedFile file1 = FileUpload1.PostedFile;
            path =  /* "Documents/" + */     file1.FileName;
            strFileName1 = Server.MapPath("~/Documents/") + file1.FileName;
            file1.SaveAs(strFileName1);

            string ext = Path.GetExtension(path);
            string contenttype = String.Empty;

            switch (ext)
            {
                case ".jpg":
                    contenttype = "image/jpg";
                    break;
                case ".png":
                    contenttype = "image/png";
                    break;
                case ".gif":
                    contenttype = "image/gif";
                    break;
                case ".pdf":
                    contenttype = "image/pdf";
                    break;
                //case ".doc":
                //    contenttype = "Documents/vnd.ms-word";
                //    break;
                //case ".docx":
                //    contenttype = "Documents/vnd.ms-word";
                //    break;
                //case ".xls":
                //    contenttype = "Documents/vnd.ms-excel";
                //    break;
                //case ".xlsx":
                //    contenttype = "Documents/vnd.ms-excel";
                //    break;

            }

        }

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        uploadDocument();
        string date = DateTime.Now.ToString("dd-MM-yyyy");
        string imageid = DAL.getRunningID("4");
        sqlstr = "insert into Images(ImageID,URL,DateTime)values(" + imageid + ",'" + path + "','" + date + "')";
        string sqlUpdate = "update Fms_Autonumber set RNo='" + imageid + "' where RCode='4'";
        List<string> ids = new List<string>();
        ids.Add(sqlstr);
        ids.Add(sqlUpdate);
        int j = DAL.sqlTransaction(ids);
        if (j > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Successfully Inserted", "alert('Image Uploaded Successfully..!')", true);
        }

        mltImageurl.Text = strFileName1;
    }
}
Posted
Updated 2-Dec-14 22:28pm
v2
Comments
DamithSL 3-Dec-14 4:31am    
as per your code, if you click on upload button only data will insert. is that what you want?
CP_vicky 3-Dec-14 4:47am    
After data inserted, if i refresh the page without pressing upload button then also data is inserting..
No, it cannot do like that. Have you put a breakpoint inside the click event and tested whether it is hitting that while refreshing?
CP_vicky 3-Dec-14 5:03am    
It is again hitting while refreshing after putting a breakpoint
Post your aspx code, where Button is there.

Quote:
After data inserted, if i refresh the page without pressing upload button then also data is inserting..


try this:

http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html[^]
 
Share this answer
 
Comments
CP_vicky 3-Dec-14 23:39pm    
public partial class ImageUpLoader : System.Web.UI.Page
{
string sqlstr;
static string path;
string strFileName1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
}
}

public void uploadDocument()
{

if ((FileUpload1.PostedFile != null))
{

HttpPostedFile file1 = FileUpload1.PostedFile;
path = /* "Documents/" + */ file1.FileName;
strFileName1 = Server.MapPath("~/Documents/") + file1.FileName;
file1.SaveAs(strFileName1);

string ext = Path.GetExtension(path);
string contenttype = String.Empty;

switch (ext)
{
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "image/pdf";
break;
}
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{

uploadDocument();
string date = DateTime.Now.ToString("dd-MM-yyyy");
string imageid = DAL.getRunningID("4");
sqlstr = "insert into Images(ImageID,URL,DateTime)values(" + imageid + ",'" + path + "','" + date + "')";
string sqlUpdate = "update Fms_Autonumber set RNo='" + imageid + "' where RCode='4'";
List<string> ids = new List<string>();
ids.Add(sqlstr);
ids.Add(sqlUpdate);
int j = DAL.sqlTransaction(ids);
if (j > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Successfully Inserted", "alert('Image Uploaded Successfully..!')", true);
}
mltImageurl.Text = strFileName1;
}

protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["CheckRefresh"] = Session["CheckRefresh"];
}
}


Again it is inserting like same
King Fisher 4-Dec-14 0:03am    
protected void btnUpload_Click(object sender, EventArgs e)
{
if (Session["CheckRefresh"].ToString() == ViewState["CheckRefresh"].ToString())
{
uploadDocument();
string date = DateTime.Now.ToString("dd-MM-yyyy");
string imageid = DAL.getRunningID("4");
sqlstr = "insert into Images(ImageID,URL,DateTime)values(" + imageid + ",'" + path + "','" + date + "')";
string sqlUpdate = "update Fms_Autonumber set RNo='" + imageid + "' where RCode='4'";
List ids = new List();
ids.Add(sqlstr);
ids.Add(sqlUpdate);
int j = DAL.sqlTransaction(ids);
if (j > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Successfully Inserted", "alert('Image Uploaded Successfully..!')", true);
}
mltImageurl.Text = strFileName1;

Session["CheckRefresh"] =
Server.UrlDecode(System.DateTime.Now.ToString());
}
else
{
Response.Write("Page Refreshed");
}
}
CP_vicky 4-Dec-14 0:15am    
Problem Solved. I checked this as Accept Answer. Thank you very much.
King Fisher 4-Dec-14 0:22am    
Welcome :)
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

put a insert query into a page_load or make a mathod for query insertion of data nad call in page_load event
 
Share this answer
 
Comments
OP is doing the Insert inside the Upload Button click. That is correct. No need to put that inside the !IsPostBack.
CP_vicky 3-Dec-14 5:06am    
Ya

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