Question

I'm new to coding so sorry for any misuse of terms and my lack of understanding.

So, it's possible that the ISNULL in the update query is not the problem, as I said, I'm new to this.

Basically, I am creating a newsletter website for an assignment. An author can upload an article but the editor must authorise the upload. This is the page where I am having trouble. Every field updates fine except for the Image field. It holds the pathway to an image which has been uploaded by the author. If I was to edit any field, for example, the headline field and update the database the data within the Image field gets erased and is replaced by "NULL". Now I obviously don't want this. If no image has been uploaded in the edit page I want that value to remain. If I was to select a new image, it updates completely fine, the problem lies when no image has been selected. I hope someone can help me with this problem as it's been bothering me all day, it's probably something simple and would appreciate any help.

Below is my code behind with the UPDATE statement, I believe this is where the problem lies.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class admin_updatenews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void displayedit_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
    {
        info.Text = "Item Updated";

        FileUpload fileupdate = displayedit.EditItem.FindControl("imageupdate") as FileUpload;

        Label recordid = displayedit.EditItem.FindControl("idlabel1") as Label;
        Int32 id = Convert.ToInt32(recordid.Text);

        if (fileupdate.HasFile)
        {
            String fupload = fileupdate.FileName;

            Random r = new Random();
            int rInt = r.Next(0, 10000);

            String imgpath = "../images/" + rInt + fupload;

            fileupdate.SaveAs(Server.MapPath(imgpath));

            String newimage = rInt + fupload;

            string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString;
            SqlConnection myConnection = new SqlConnection(newsconnection);

            //myConnection.ConnectionString is now set to connectionString.
            myConnection.Open();

            String query = "UPDATE News SET postimage = ISNULL('" + newimage + "', postimage), Image = ISNULL('" + newimage + "', Image) WHERE id='" + id + "'";

            SqlCommand myCommand = new SqlCommand(query, myConnection);

            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
    }
    protected void displayedit_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        info.Text = "I am editing";
    }
    protected void displayedit_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        info.Text = "Not Updating";
    }
} 

Since I'm not great at explaining all the terms I'll provide images of my process to hopefully make things clear.

Inserted data

enter image description here

Inserted Data displayed

enter image description here

Edit Inserted Data

enter image description here

Update Inserted Data (No image)

enter image description here

Updated Database

enter image description here

No correct solution

OTHER TIPS

ISNULL('" + newimage + "', NULL) makes no sense -you're always seeing if a string constant is NULL which is never true.

I suspect you want:

"UPDATE News SET postimage = " + newImage == null ? "null" : "'" + newimage + "'," ...

however for several reasons, you should be using parameters:

String query = "UPDATE News SET postimage = @postImage, Image = @Image WHERE id=@id";

SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@postImage",postImage ?? DBNull.Value);
myCommand.Parameters.AddWithValue("@Image",Image ?? DBNull.Value);
myCommand.Parameters.AddWithValue("@id",id);


myCommand.ExecuteNonQuery();
myConnection.Close();

Notice that if postImage or Image are null that DBNull.Value is used instead of null.

In your example, "newimage" will always have a string value pointing to the file. So there's no need to check for null, and the ISNULL expressions will do nothing. Your images imply that there's other code updating data including "postimage" and "Image". If you have similar ISNULL expressions in other code, my guess is that you'd want the following to ensure that the values don't change if your new "newimage" values are null:

String query = "UPDATE News SET postimage = ISNULL('" + newimage + "', postimage), Image = ISNULL('" + newimage + "', Image) WHERE id='" + id + "'";

This way the "ISNULL" expression says: "if the new value is null, set to the current value".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top