Updatepage |当我更新时,为什么我的图像字段在db中被空值覆盖

本文关键字:db 字段 覆盖 空值 图像 我的 更新 为什么 Updatepage | 更新日期: 2023-09-27 18:03:48

我是新手,很抱歉我的一些术语是错误的。

我正在尝试制作一个更新页面,将更新数据库值。所有其他字段更新得很好,但是每当我尝试更新数据库而不上传图像时,它将用空值替换当前字段值。任何帮助,将不胜感激,以补救这个问题。确切的代码会很好,正如我之前说过的,我是编码新手,特别是asp.net和c#,所以有些术语对我来说是新的。

下面是我的代码后面的文件,我想这是我的问题的原因。

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 ='" + newimage + "', Image ='" + newimage + "' 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";
    }
} 

如果需要,这是前端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Copy of updatenews.aspx.cs" Inherits="admin_updatenews" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="../css/responsive.css" rel='stylesheet' type='text/css' />
        <link href="../css/gui.css" rel='stylesheet' type='text/css' />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="newseditrows" runat="server" 
            ConnectionString='<%$ ConnectionStrings:newsconnection %>' 
            SelectCommand="SELECT News.id, News.headline, News.Image, News.paragraph, Sportslist.sportname, News.Sport,  News.date, News.lead, News.authorID, Sportslist.id, News.postheadline, News.postimage, News.postparagraph, News.postsport, News.postdate, News.postlead, News.postauthorid
                            FROM News 
                            INNER JOIN Sportslist ON News.Sport = Sportslist.id
                            ORDER BY News.id DESC"
            UpdateCommand="UPDATE [News] SET [headline]=@headline, [Image]=@Image, [paragraph]=@paragraph, [Sport]=@sport, [date]=@date, [lead]=@lead, [authorid]=@authorid, [postheadline]=@headline, [postimage]=@Image, [postparagraph]=@paragraph, [postsport]=@sport, [postdate]=@date, [postlead]=@lead, [postauthorid]=@authorid WHERE [id]=@id">
            <UpdateParameters>
                <asp:Parameter Name="headline" Type="String" />
                <asp:Parameter Name="Image" Type="String" />
                <asp:Parameter Name="paragraph" Type="String" />
                <asp:Parameter Name="Sport" Type="Int32" />
                <asp:Parameter Name="date" Type="DateTime" />
                <asp:Parameter Name="lead" Type="String" />
                <asp:Parameter Name="authorid" Type="Int32" />
                <asp:Parameter Name="id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="sportlist" runat="server" 
            ConnectionString='<%$ ConnectionStrings:newsconnection %>' 
            SelectCommand="SELECT [id], [sportname] FROM [Sportslist]">
        </asp:SqlDataSource>
        <asp:Label ID="info" runat="server" Text="Not Updating"></asp:Label>
        <br />
        <asp:ListView ID="displayedit" runat="server" 
            DataSourceID="newseditrows" 
            DataKeyNames="id" 
            OnItemUpdated="displayedit_ItemUpdated" 
            OnItemEditing="displayedit_ItemEditing" 
            OnItemCanceling="displayedit_ItemCanceling">
            <AlternatingItemTemplate>
                <span style="">id:
                    <asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
                    headline:
                    <asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
                    Image:
                    <asp:Image ID="ImageLabel" runat="server" ImageURL='<%# "../images/" + Eval("Image") %>' Width="100px" />
                    <br />
                    paragraph:
                    <asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
                    Sport:
                    <asp:Label Text='<%# Eval("sportname") %>' runat="server" ID="SportLabel" /><br />
                    date:
                    <asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
                    lead:
                    <asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
                    authorID:
                    <asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
                    <asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
                    <br />
                    <br />
                </span>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                    <span style="">id:
                    <asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel1" /><br />
                    headline:
                    <asp:TextBox Text='<%# Bind("headline") %>' runat="server" ID="headlineTextBox" /><br />
                    Image:
                    <asp:Image ID="ImageTextBox1" runat="server" ImageUrl='<%# "../images/" + Eval("Image") %>' Width="100px"/>
                    <asp:FileUpload ID="imageupdate" runat="server" />
                    <br />
                    paragraph:
                    <asp:TextBox Text='<%# Bind("paragraph") %>' runat="server" ID="paragraphTextBox" /><br />
                    Sport:
                    <%--<asp:TextBox Text='<%# Bind("Sport") %>' runat="server" ID="SportTextBox" />--%>
                    <asp:DropDownList ID="SportsDropdown" runat="server" SelectedValue='<%# Bind("Sport") %>'>
                        <asp:ListItem Value="1">Football</asp:ListItem>
                        <asp:ListItem Value="2">Rugby</asp:ListItem>
                        <asp:ListItem Value="3">Basketball</asp:ListItem>
                        <asp:ListItem Value="4">Motorsport</asp:ListItem>
                        <asp:ListItem Value="5">NFL</asp:ListItem>
                        <asp:ListItem Value="6">Cricket</asp:ListItem>
                        <asp:ListItem Value="7">Tennis</asp:ListItem>
                        <asp:ListItem Value="8">Golf</asp:ListItem>
                        <asp:ListItem Value="9">Other</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    lead:
                    <asp:TextBox Text='<%# Bind("lead") %>' runat="server" ID="leadTextBox" /><br />
                    <asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" /><asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" /><br />
                    <br />
                </span>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <span>No data was returned.</span>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <span style="">headline:
                    <asp:TextBox Text='<%# Bind("headline") %>' runat="server" ID="headlineTextBox" /><br />
                    Image:
                    <asp:TextBox Text='<%# Bind("Image") %>' runat="server" ID="ImageTextBox" /><br />
                    paragraph:
                    <asp:TextBox Text='<%# Bind("paragraph") %>' runat="server" ID="paragraphTextBox" /><br />
                    Sport:
                    <asp:TextBox Text='<%# Bind("Sport") %>' runat="server" ID="SportTextBox" /><br />
                    date:
                    <asp:TextBox Text='<%# Bind("date") %>' runat="server" ID="dateTextBox" /><br />
                    lead:
                    <asp:TextBox Text='<%# Bind("lead") %>' runat="server" ID="leadTextBox" /><br />
                    authorID:
                    <asp:TextBox Text='<%# Bind("authorID") %>' runat="server" ID="authorIDTextBox" /><br />
                    <asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" /><asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" /><br />
                    <br />
                </span>
            </InsertItemTemplate>
            <ItemTemplate>
                <span style="">id:
                    <asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
                    headline:
                    <asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
                    Image:
                    <asp:Image ID="ImageLabel" runat="server" ImageURL='<%# "../images/" + Eval("Image") %>' Width="100px" />
                    <br />
                    paragraph:
                    <asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
                    Sport:
                    <asp:Label Text='<%# Eval("sportname") %>' runat="server" ID="SportLabel" /><br />
                    date:
                    <asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
                    lead:
                    <asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
                    authorID:
                    <asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
                    <asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
                    <br />
                    <br />
                </span>
            </ItemTemplate>
            <LayoutTemplate>
                <div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
                <div style="">
                    <asp:DataPager runat="server" ID="DataPager1">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
                            <asp:NumericPagerField></asp:NumericPagerField>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
                        </Fields>
                    </asp:DataPager>
                </div>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <span style="">id:
                    <asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
                    headline:
                    <asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
                    Image:
                    <asp:Label Text='<%# Eval("Image") %>' runat="server" ID="ImageLabel" /><br />
                    paragraph:
                    <asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
                    Sport:
                    <asp:Label Text='<%# Eval("Sport") %>' runat="server" ID="SportLabel" /><br />
                    date:
                    <asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
                    lead:
                    <asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
                    authorID:
                    <asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
                    <asp:Button runat="server" CommandName="Edit" Text="Edit" ID="EditButton" />
                    <br />
                    <br />
                </span>
            </SelectedItemTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>

Updatepage |当我更新时,为什么我的图像字段在db中被空值覆盖

下面是使用ur查询的update语句的示例。

UPDATE News 
    SET postimage = ISNULL(YourNewValue, postimage), Image = ISNULL(YourNewValue, Image) 
WHERE id = YourID
编辑:

尝试在数据源updateCommand中添加以下内容。如果这不能解决问题,那么试着逐级执行,看看在哪里添加了null。

UpdateCommand="UPDATE [News] SET [headline]=@headline, [Image]= ISNULL(@Image, [Image]), [paragraph]=@paragraph, [Sport]=@sport, [date]=@date, [lead]=@lead, [authorid]=@authorid, [postheadline]=@headline, [postimage]=@Image, [postparagraph]=@paragraph, [postsport]=@sport, [postdate]=@date, [postlead]=@lead, [postauthorid]=@authorid WHERE [id]=@id">