1.UI大概就是这样的了
头像列 FileUpload control |
ID: xx |
username:xx | |
password:xx | |
signin_date:xx | |
age:xx | |
picture is empty before inserting is done! | |
备注: |
2.页面代码如下:
其实就是一个sqldatasource加一个Formview可新增,修改,删除了。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="formview.aspx.cs" Inherits="Day4_formview" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <style type="text/css"> .style1 { width: 100%; } </style></head><body> <form id="form1" runat="server"> <div> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString %>" DeleteCommand="DELETE FROM [Users] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Users] ([username], [password], [signin_date], [age], [picture]) VALUES (@username, @password, @signin_date, @age, @picture)" SelectCommand="SELECT [ID], [username], [password], [signin_date], [age], [picture] FROM [Users]" UpdateCommand="UPDATE [Users] SET [username] = @username, [password] = @password, [signin_date] = @signin_date, [age] = @age, [picture] = @picture WHERE [ID] = @ID" OnInserting="SqlDataSource1_Inserting"> <DeleteParameters> <asp:Parameter Name="ID" Type="Int64" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="username" Type="String" /> <asp:Parameter Name="password" Type="String" /> <asp:Parameter Name="signin_date" Type="DateTime" /> <asp:Parameter Name="age" Type="Byte" /> <asp:Parameter Name="picture" Type="String" /> <asp:Parameter Name="ID" Type="Int64" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="username" Type="String" /> <asp:Parameter Name="password" Type="String" /> <asp:Parameter Name="signin_date" Type="DateTime" /> <asp:Parameter Name="age" Type="Byte" /> <asp:Parameter Name="picture" Type="String" /> </InsertParameters> </asp:SqlDataSource> <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="ID" DataSourceID="SqlDataSource1" Width="391px" CellPadding="4" ForeColor="#333333"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" /> <EditItemTemplate> ID: <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' /> <br /> username: <asp:TextBox ID="usernameTextBox" runat="server" Text='<%# Bind("username") %>' /> <br /> password: <asp:TextBox ID="passwordTextBox" runat="server" Text='<%# Bind("password") %>' /> <br /> signin_date: <asp:TextBox ID="signin_dateTextBox" runat="server" Text='<%# Bind("signin_date") %>' /> <br /> age: <asp:TextBox ID="ageTextBox" runat="server" Text='<%# Bind("age") %>' /> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> <InsertItemTemplate> <table class="style1"> <tr> <td rowspan="6"> 头像列 <img id="img1" width="90" /> <asp:FileUpload ID="FileUpload1" runat="server" onchange="javascript:img1.src=this.value" /> </td> <td> ID: <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /> </td> </tr> <tr> <td> username: <asp:TextBox ID="usernameTextBox" runat="server" Text='<%# Bind("username") %>' /> </td> </tr> <tr> <td> password: <asp:TextBox ID="passwordTextBox" runat="server" Text='<%# Bind("password") %>' /> </td> </tr> <tr> <td> signin_date: <asp:TextBox ID="signin_dateLabel" runat="server" Text='<%# Eval("signin_date","{0:yyyy-MM-dd}") %>' /> </td> </tr> <tr> <td> age: <asp:TextBox ID="ageLabel" runat="server" Text='<%# Bind("age") %>' /> </td> </tr> <tr> <td> picture is empty before inserting is done! </td> </tr> <tr> <td colspan="2"> 备注: </td> </tr> </table> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </InsertItemTemplate> <ItemTemplate> <table class="style1"> <tr> <td rowspan="6"> 头像列 <img src='<%# Eval("picture").ToString()==""?"nophoto.jpg":Eval("picture")%>' width="90" /> </td> <td> ID: <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /> </td> </tr> <tr> <td> username: <asp:Label ID="usernameLabel" runat="server" Text='<%# Bind("username") %>' /> </td> </tr> <tr> <td> password: <asp:Label ID="passwordLabel" runat="server" Text='<%# Bind("password") %>' /> </td> </tr> <tr> <td> signin_date: <asp:Label ID="signin_dateLabel" runat="server" Text='<%# Eval("signin_date","{0:yyyy-MM-dd}") %>' /> </td> </tr> <tr> <td> age: <asp:Label ID="ageLabel" runat="server" Text='<%# Bind("age") %>' /> </td> </tr> <tr> <td> picture: <asp:Label ID="pictureLabel" runat="server" Text='<%# Bind("picture") %>' /> </td> </tr> <tr> <td colspan="2"> 备注: </td> </tr> </table> <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" /> <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" /> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" /> </ItemTemplate> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> </asp:FormView> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </form></body></html>
3.后台代码
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e) {
try { FileUpload f1 = this.FormView1.FindControl("FileUpload1") as FileUpload;
if (f1.HasFile) { filename = "~/photo/" + f1.FileName;
f1.SaveAs(Server.MapPath(filename));
e.Command.Parameters["@picture"].Value = filename;
this.SqlDataSource1.Insert(); } } catch (Exception ex) {
this.Label1.Text = ex.Message; } }
4.症状:
a.填入信息和上传图片后,按插入,网页报如下错误:
您试图在此 Web 服务器上访问的 Web 应用程序当前不可用。请点击 Web 浏览器中的“刷新”按钮重试您的请求。
管理员注意事项: 详述此特定请求失败原因的错误信息可在 Web 服务器的系统事件日志中找到。请检查此日志项以查明导致该错误发生的原因。
b.查看系统日志,显示如下:
在C:\Windows\system32\Logfiles\W3SVC1有个日志文件,内容显示如下:
#Software: Microsoft Internet Information Services 5.1#Version: 1.0#Date: 2011-04-05 14:39:57#Fields: time c-ip cs-method cs-uri-stem sc-status 14:39:57 127.0.0.1 GET /ADONET/Day4/formview.aspx 20014:39:57 127.0.0.1 GET /ADONET/Images/thu.jpg 30414:40:08 127.0.0.1 POST /ADONET/Day4/formview.aspx 20014:42:01 127.0.0.1 POST /ADONET/Day4/formview.aspx 500
c.在事件查看器中,应用程序日志,显示如下:
aspnet_wp.exe (PID: 4068)意外停止。
d.已经在网上google试验了一下午,还是没搞定。望有经验的来看看!
你调试一下,可能你的 this.SqlDataSource1.Insert(); 报了异常。
你跟踪的时候可以看一看output窗口。