更新页面|为什么我更新时db中的图像字段会被空值覆盖?

时间:2014-05-15 19:24:10

标签: c# asp.net

我是编码的新手,很抱歉,如果我的一些行话是错误的。

我正在尝试制作一个更新数据库值的更新页面。所有其他字段都更新正常,但每当我尝试在没有图像上载的情况下更新数据库时,它将使用空值替换当前字段值。任何帮助将不胜感激,以解决这个问题。确切的代码会非常好,正如我之前所说的那样,我对编码很感兴趣,特别是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>

1 个答案:

答案 0 :(得分:0)

以下是使用ur查询的更新语句示例。

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">