Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
Hi coders i have an upload image page in which i have file up-loader control and one drop-down list. flow of my code

First user select file from the system using file up-loader and then select department from the drop-down list and click on submit button when user click on submit button image path save in the database and images save into the selected department folder.

C#
protected void Page_Load(object sender, EventArgs e)
{
    AutoNumber();
}
public void AutoNumber()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT COUNT(Priority) as Tot FROM Images", con);
    SqlDataReader dr;

    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        int i = Convert.ToInt32(dr["tot"]);

        if (i > 0)
        {
            int j = i + 1;
            lblPriority.Text = "0" + j.ToString();
        }
        else
        {
            lblPriority.Text = "1";
        }

    }
    con.Close();
}
protected void btnSubmit_Click1(object sender, EventArgs e)
{

    //con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WebGallery"].ConnectionString;
    string DepartmentID = ddlDepartment.SelectedValue;
    string Description = tbImageName.Text.Trim();
    string Priority = lblPriority.Text.Trim();

    //Get Filename from fileupload control
    string imgName = fileuploadimages.FileName.ToString();
    //sets the image path if exist then store image in that place else create one
    string imgPath = "Images/Departments/" + "" + ddlDepartment.SelectedValue + "/";
    bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
    if (!IsExists)
        System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
    //then save it to the Folder
    fileuploadimages.SaveAs(Server.MapPath(imgPath + imgName));
    //Open the database connection
    con.Open();
    //Query to insert * into images into database
    SqlCommand cmd = new SqlCommand("insert into Images(ImageName, Description, Path, Priority,DepartmentID) values (@ImageName, @Description, @Path, @Priority,@DepartmentID)", con);
    //Passing parameters to query
    cmd.Parameters.AddWithValue("@ImageName", imgName);
    cmd.Parameters.AddWithValue("@Description", Description);
    cmd.Parameters.AddWithValue("@Path", imgPath + imgName);
    cmd.Parameters.AddWithValue("@Priority", lblPriority.Text);
    cmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
    cmd.ExecuteNonQuery();
    //Close dbconnection
    con.Close();
    tbImageName.Text = string.Empty;
}
}


Now what i want :

Now i want add one more drop-down list in which priority of images come from the database. when user browse image select department and select priority that image store image at that priority and image which is already at that image move by 1 and so on till the last image.

this is my related reference question their i did'nt get any solution so i thought to modify my question ask again i hope you don't mind guys


http://stackoverflow.com/questions/17209459/how-to-put-an-image-into-selected-postition-and-store-in-database/17210510?noredirect=1#comment24930508_17210510[^]
Posted
Updated 8-Jul-13 5:29am
v2

1 solution

Finally i got my answer:

C#
protected void btnSubmit_Click1(object sender, EventArgs e)
   {
       //con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WebGallery"].ConnectionString;
       string DepartmentID = ddlDepartment.SelectedValue;
       string Description = tbImageName.Text.Trim();
       //string Priority = lblPriority.Text.Trim();
       string Priority = ddlPriority.SelectedValue;
       //Get Filename from fileupload control
       string imgName = fileuploadimages.FileName.ToString();
       //sets the image path if exist then store image in that place else create one
       string imgPath = "Images/Departments/" + "" + ddlDepartment.SelectedValue + "/";
       bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
       if (!IsExists)
           System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
       //then save it to the Folder
       fileuploadimages.SaveAs(Server.MapPath(imgPath + imgName));
       //Open the database connection
       con.Open();
       //Query to insert * into images into database

       SqlCommand cmd = new SqlCommand("Update Images set Priority=Priority+1 where Priority>='" + ddlPriority.SelectedValue + "'" + "Insert into Images(ImageName,Description,Path,Priority,DepartmentID) values(@ImageName,@Description,@Path,@Priority,@DepartmentID)", con);
       //Passing parameters to query
       cmd.Parameters.AddWithValue("@ImageName", imgName);
       cmd.Parameters.AddWithValue("@Description", Description);
       cmd.Parameters.AddWithValue("@Path", imgPath + imgName);
       cmd.Parameters.AddWithValue("@Priority", Priority);
       cmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
       cmd.ExecuteNonQuery();
       //Close dbconnection
       con.Close();
       tbImageName.Text = string.Empty;
      // Response.Redirect(Request.RawUrl);
   }
 
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