2
リストビューを使用してデータベース内の画像を更新できますが、できませんでした ファイルのアップロードで常にヌル値が得られ、問題を解決できませんでした。私はasp.netとC#編集アイテムテンプレートのファイルアップロード
で初心者です助け、これは私のコードです:
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" Height="1138px">
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Articles" ForeColor="#FF3300"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name"
Height="16px" Width="267px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:siteDBConnectionString %>"
SelectCommand="SELECT * FROM [Articles]"></asp:SqlDataSource>
<br />
<br />
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Article_No"
DataSourceID="SqlDataSource2" GroupItemCount="3"
onselectedindexchanged="ListView1_SelectedIndexChanged">
<AlternatingItemTemplate>
<td runat="server" style="background-color: #FAFAD2;color: #284775;">
Name:
<asp:Label ID="NameLabel" runat="server"
Text='<%# Eval("Name") %>' />
<br />
Subject:
<asp:Label ID="SubjectLabel" runat="server" Text='<%# Eval("Subject") %>' />
<br />
Date:
<asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
<br />
Picture:
<asp:Label ID="PictureLabel" runat="server" Text='<%# Eval("Picture") %>' />
<br />
Content:
<asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>' />
<br />
Article_No:
<asp:Label ID="Article_NoLabel" runat="server"
Text='<%# Eval("Article_No") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</AlternatingItemTemplate>
<EditItemTemplate>
<td runat="server" style="background-color: #FFCC66;color: #000080;">
Name:
<asp:TextBox ID="NameTextBox" runat="server"
Text='<%# Bind("Name", "{0}") %>' />
<br />
Subject:
<asp:TextBox ID="SubjectTextBox" runat="server"
Text='<%# Bind("Subject", "{0}") %>' />
<br />
Date:
<asp:TextBox ID="DateTextBox" runat="server"
Text='<%# Bind("Date", "{0:g}") %>' />
<br />
Picture:
<asp:FileUpload ID="fuimage" runat="server" />
<br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server"
Text='<%# Bind("Content", "{0}") %>' TextMode="MultiLine" />
<br />
Article_No:
<asp:Label ID="Article_NoLabel1" runat="server" Enabled="False"
Text='<%# Eval("Article_No", "{0}") %>' />
<br />
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
OnClick="UpdateButton_Click1" Text="Update" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
<br />
</td>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td runat="server" />
</EmptyItemTemplate>
<GroupTemplate>
<tr ID="itemPlaceholderContainer" runat="server">
<td ID="itemPlaceholder" runat="server">
</td>
</tr>
</GroupTemplate>
<InsertItemTemplate>
<td runat="server" style="">
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
<br />
Subject:
<asp:TextBox ID="SubjectTextBox" runat="server" Text='<%# Bind("Subject") %>' />
<br />
Date:
<asp:TextBox ID="DateTextBox" runat="server" Text='<%# Bind("Date") %>' />
<br />
Picture:
<asp:TextBox ID="PictureTextBox" runat="server" Text='<%# Bind("Picture") %>' />
<br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<%# Bind("Content") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
<br />
</td>
</InsertItemTemplate>
<ItemTemplate>
<td runat="server" style="background-color: #FFFBD6;color: #333333;">
Name:
<asp:Label ID="NameLabel" runat="server"
Text='<%# Eval("Name") %>' />
<br />
Subject:
<asp:Label ID="SubjectLabel" runat="server" Text='<%# Eval("Subject") %>' />
<br />
Date:
<asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
<br />
Picture:
<asp:Label ID="PictureLabel" runat="server" Text='<%# Eval("Picture") %>' />
<br />
Content:
<asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>' />
<br />
Article_No:
<asp:Label ID="Article_NoLabel" runat="server"
Text='<%# Eval("Article_No") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="groupPlaceholderContainer" runat="server" border="1"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr ID="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="text-align: center;background-color: #FFCC66;font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;">
<asp:DataPager ID="DataPager2" runat="server" PageSize="12">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<td runat="server"
style="background-color: #FFCC66;font-weight: bold;color: #000080;">
Name:
<asp:Label ID="NameLabel" runat="server"
Text='<%# Eval("Name") %>' />
<br />
Subject:
<asp:Label ID="SubjectLabel" runat="server" Text='<%# Eval("Subject") %>' />
<br />
Date:
<asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
<br />
Picture:
<asp:Label ID="PictureLabel" runat="server" Text='<%# Eval("Picture") %>' />
<br />
Content:
<asp:Label ID="ContentLabel" runat="server" Text='<%# Eval("Content") %>' />
<br />
Article_No:
<asp:Label ID="Article_NoLabel" runat="server"
Text='<%# Eval("Article_No") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
</td>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:siteDBConnectionString %>"
DeleteCommand="DELETE FROM [Articles] WHERE [Article_No] = @Article_No"
InsertCommand="INSERT INTO [Articles] ([Name], [Subject], [Date], [Picture], [Content]) VALUES (@Name, @Subject, @Date, @Picture, @Content)"
SelectCommand="SELECT [Name], [Subject], [Date], [Picture], [Content], [Article_No] FROM [Articles] WHERE ([Name] = @Name)"
UpdateCommand="UPDATE [Articles] SET [Name] = @Name, [Subject] = @Subject, [Date] = @Date, [Picture] = @Picture, [Content] = @Content WHERE [Article_No] = @Article_No">
<DeleteParameters>
<asp:Parameter Name="Article_No" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Subject" Type="String" />
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Picture" Type="String" />
<asp:Parameter Name="Content" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Name"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Subject" Type="String" />
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Picture" Type="String" />
<asp:Parameter Name="Content" Type="String" />
<asp:Parameter Name="Article_No" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
</asp:Panel>
</div>
</form>
、これはそのアイブプットの背後にあるコードです:
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.SqlClient;
namespace WebApplication2
{
public partial class WebForm4 : System.Web.UI.Page
{
SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Moeman\My Documents\siteDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DateTextBox_TextChanged(object sender, EventArgs e)
{
}
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void UpdateButton_Click1(object sender, EventArgs e)
{
FileUpload fuimage = (FileUpload)ListView1.FindControl("fuimage");
String fupload = fuimage.FileName;
if (fuimage != null && fuimage.HasFile)
{
fuimage.SaveAs(Server.MapPath("~\\Images\\" + fupload.Trim()));
string path = "~\\Images\\" + fupload.Trim();
cnn.Open();
SqlCommand cmd = new SqlCommand("update Articles set [email protected] ", cnn);
if (fuimage.HasFile)
{
cmd.Parameters.AddWithValue("@Picture", path);
}
else
{
cmd.Parameters.AddWithValue("@Picture", "");
}
cmd.ExecuteNonQuery();
cnn.Close();
}
}
}
}
IVEは、それは私にエラーを与えるdoesntのが、それは実際にはすでに空のDB内の画像フィールドは、それが空のままにしている場合、データベース – ninoal
に空の文字列を追加して、今これを試してみました、それはそのエラーを与えていない場合(文字列またはバイナリ ステートメントが終了しました) – ninoal
CommandNameを 'Update'に設定し、それに応じて' NewValues'プロパティを設定しているので、代わりにFormViewのItemUpdatingイベントを使用してみてください。 http://stackoverflow.com/a/1132414/284240 –