Skip to content Skip to sidebar Skip to footer

How To Put An Image Into Selected Postition And Store In Database

What i have is a page to upload images and save their path into database with priority of the images. Now this is just an example i have a 5 images in my database with their priori

Solution 1:

You have to update each row in your DB. Try something like this :

Image newImage;        //Your image uploaded
List<Image> listImages; //All the images from your DB (before insertion)

foreach(Image img in listImages) {
   if(img['Priority'] < newImg['Priority']) 
   {
      SqlCommandcmd=newSqlCommand(
                  "UPDATE TestImage SET Priority=" + img['Priority'] - 1 + ";"); 
   }
   else
   {
      SqlCommandcmd=newSqlCommand(
                  "UPDATE TestImage SET Priority=" + img['Priority'] + 1 + ";"); 

   }

}

Solution 2:

Finally i got my answer

protectedvoid ChangePriority_Click(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 controlstring imgName = fileuploadimages.FileName.ToString();
    //sets the image path if exist then store image in that place else create onestring 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);
}

Post a Comment for "How To Put An Image Into Selected Postition And Store In Database"