简体   繁体   English

ASP.net C#如何做到这一点,因此更新页面中的数据库更新中只有一个字段

[英]ASP.net C# How can I make it so only one field in database updates in update page

I'm fairly new to coding in general so sorry if some of my jargon is wrong. 我一般来说对代码来说还很陌生,所以如果我的一些术语是错误的,请抱歉。

I have a database that stores the image name of files in for my homepage, so that an admin can upload new images to replace the previous images in database so that it posts the new uploaded images to the homepage. 我有一个数据库,用于存储主页的文件的图像名称,以便管理员可以上传新图像以替换数据库中的先前图像,从而将新上传的图像发布到主页。

The problem I am currently having is that when I just want to update one of the images, it uploads that image but the other images are replaced with null values, I only want the field I uploaded the image to, to be updated. 我目前遇到的问题是,当我只想更新其中一张图像时,它会上传该图像,而其他图像都被替换为空值,而我只希望更新上传图像的字段。

Here is my code behind, I have a fair idea what I'm doing wrong, however, I don't really now how to remedy my problem. 这是我的代码,我很清楚自己在做什么错,但是,我现在真的不知道如何解决我的问题。 Any help would be greatly appreciated. 任何帮助将不胜感激。

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_updatehomepage : 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 fileupdate1 = displayedit.EditItem.FindControl("imageupdate1") as FileUpload;
        FileUpload fileupdate2 = displayedit.EditItem.FindControl("imageupdate2") as FileUpload;
        FileUpload fileupdate3 = displayedit.EditItem.FindControl("imageupdate3") as FileUpload;
        FileUpload fileupdate4 = displayedit.EditItem.FindControl("imageupdate4") as FileUpload;
        FileUpload fileupdate5 = displayedit.EditItem.FindControl("imageupdate5") as FileUpload;
        FileUpload fileupdate6 = displayedit.EditItem.FindControl("imageupdate6") as FileUpload;
        FileUpload fileupdate7 = displayedit.EditItem.FindControl("imageupdate7") as FileUpload;
        FileUpload fileupdate8 = displayedit.EditItem.FindControl("imageupdate8") as FileUpload;
        FileUpload fileupdate9 = displayedit.EditItem.FindControl("imageupdate9") as FileUpload;

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

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

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

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

            fileupdate1.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 homepage SET fballimg ='" + newimage + "' WHERE id='" + id + "'";

            SqlCommand myCommand = new SqlCommand(query, myConnection);

            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
        if (fileupdate2.HasFile)
        {
            String fupload = fileupdate2.FileName;

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

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

            fileupdate2.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 homepage SET rugbyimg ='" + newimage + "' WHERE id='" + id + "'";

            SqlCommand myCommand = new SqlCommand(query, myConnection);

            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
        if (fileupdate3.HasFile)
        {
            String fupload = fileupdate3.FileName;

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

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

            fileupdate3.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 homepage SET bballimg ='" + newimage + "' WHERE id='" + id + "'";

            SqlCommand myCommand = new SqlCommand(query, myConnection);

            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
    }
    protected void displayedit_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        info.Text = "<b>*Recommend images are 490px x 190px(or of similar ratio) to avoid image distortion</b>";
    }
    protected void displayedit_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        info.Text = "Not Updating";
    }
}

try this... 尝试这个...

Store the names of each image in the different hiddenfield.... 将每个图像的名称存储在不同的hiddenfield中。

Now while updating check the : 现在在更新时检查:

if (fileupdateId.HasFile)
{
    //then new file name
}
else
{
   //else hiddenfield value
}

do let me know if any query is there.. 让我知道是否有任何查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 ASP.net 中的值使字段只读:C#、ASP.Net - Make a field read only based on value in ASP.net: C#, ASP.Net 如何编码html源代码,以便没有人可以在网上看到它。 asp.net c# - How to encode html source code so that no one can see it on web. asp.net c# C# ASP.NET Core:我只能更新一个表的数据,但其他表没有更新 - C# ASP.NET Core : I am able to update data for only one table but the others are not updating 如何仅从选定的文本框中更新数据库以获取选择性信息? (asp.net C#) - How to update database for selective information from selected textboxes only? (asp.net c#) ASP.Net如何从gridview C#更新数据库 - ASP.Net How to Update a database from a gridview C# 如何通过C#ASP.NET MVC4更新数据库? - How to update database by C# ASP.NET MVC4? 如何通过C#更新同一asp.net页中的标签 - How to update labels in the same asp.net page by C# 如何使asp.net C#中的超链接字段为只读? - How to make a hyperlinkfield in asp.net c# read only? 如何从数据库驱动的数据在C#和asp.net中设置GridView的字段宽度 - How do i set the field widths of a GridView in C# and asp.net from database driven data C#,ASP.NET-如何更新UI,请等待5秒钟,然后再次更新UI? - C#, ASP.NET - How can I Update UI, wait 5 seconds, Update UI again?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM