更新和编辑的时候,数据已修改成功,但会报一个令人不解的异常。
[已关闭] 更新和编辑的时候,数据已修改成功,但会报一个令人不解的异常。
当前页面:http://www.senparc.com/SZD-258
{ 收藏当前页面 }
30
[Web开发 > ASP.NET > WebForms]
实现功能:
就是类似一个名牌更新程序,包括头像,ID(自动产生),姓名,年龄,注册日期,头像路径这么几个字段。可实现增删改查
问题:
更新和编辑的时候,数据已修改成功,但会报一个异常。
为,不能将值 NULL 插入列 'username',表 'DEV.dbo.Users';列不允许有空值。INSERT 失败。语句已终止。
我Debug进去到,sqldatasource_inserting事件结束后,除了
picture正常有取到值后,其它的username什么的都为空。这就奇怪了,在InsertItem模板中,我其它字段都是用的Bind的语法,这应该是没有问题的,请指教,谢谢!
页面代码:
<%@ 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 * FROM [Users]"
UpdateCommand="UPDATE [Users] SET [username] = @username, [password] = @password, [signin_date] = @signin_date, [age] = @age, [picture] = @picture WHERE [ID] = @ID"
OnInserting="SqlDataSource1_Inserting" onupdating="SqlDataSource1_Updating"
>
<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="AliceBlue" />
<EditItemTemplate>
<table class="style1">
<tr>
<td rowspan="6">
头像列
<img id="img2" src='<%# Eval("picture").ToString()==""?"nophoto.jpg":Eval("picture")%>' width="90" />
<asp:FileUpload ID="FileUpload1" runat="server" onchange="javascript:img2.src=this.value" />
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("picture") %>'></asp:TextBox>
</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_dateTextBox" runat="server" Text='<%# Bind("signin_date","{0:yyyy-MM-dd}") %>' />
</td>
</tr>
<tr>
<td>
age:
<asp:TextBox ID="ageTextBox" runat="server" Text='<%# Bind("age") %>' />
</td>
<tr>
<td colspan="2">
备注:
</td>
</tr>
</table>
<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 will be generated automatically when inserting!
</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_dateTextBox" runat="server" Text='<%# Bind("signin_date","{0:yyyy-MM-dd}") %>' />
</td>
</tr>
<tr>
<td>
age:
<asp:TextBox ID="ageTextBox" 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" OnClick="InsertButton_Click" />
<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='<%# Eval("username") %>' />
</td>
</tr>
<tr>
<td>
password:
<asp:Label ID="passwordLabel" runat="server" Text='<%# Eval("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='<%# Eval("age") %>' />
</td>
</tr>
<tr>
<td>
picture:
<asp:Label ID="pictureLabel" runat="server" Text='<%# Eval("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>
后台代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Day4_formview : System.Web.UI.Page
{
string filename = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
//e.Command.Parameters["@username"].Value = "zzmissyou";
//e.Command.Parameters["@password"].Value = "pwd";
//e.Command.Parameters["@signin_date"].Value = "2011-09-09";
//e.Command.Parameters["@age"].Value = "90";
e.Command.Parameters["@picture"].Value = filename;
}
protected void InsertButton_Click(object sender, EventArgs e)
{
try
{
FileUpload f1 = this.FormView1.FindControl("FileUpload1") as FileUpload;
if (f1.HasFile)
{
filename = "../photo/" + f1.FileName;
f1.SaveAs(Server.MapPath(filename));
this.SqlDataSource1.Insert();
}
}
catch (Exception ex)
{
this.Label1.Text = ex.Message;
}
}
protected void SqlDataSource1_Updating(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;
}
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
//try
//{
// FileUpload f1 = this.FormView1.FindControl("FileUpload1") as FileUpload;
// if (f1.HasFile)
// {
// filename = "../photo/" + f1.FileName;
// f1.SaveAs(Server.MapPath(filename));
// this.SqlDataSource1.Insert();
// }
//}
//catch (Exception ex)
//{
// this.Label1.Text = ex.Message;
//}
}
}
就是类似一个名牌更新程序,包括头像,ID(自动产生),姓名,年龄,注册日期,头像路径这么几个字段。可实现增删改查
问题:
更新和编辑的时候,数据已修改成功,但会报一个异常。
为,不能将值 NULL 插入列 'username',表 'DEV.dbo.Users';列不允许有空值。INSERT 失败。语句已终止。
我Debug进去到,sqldatasource_inserting事件结束后,除了
picture正常有取到值后,其它的username什么的都为空。这就奇怪了,在InsertItem模板中,我其它字段都是用的Bind的语法,这应该是没有问题的,请指教,谢谢!
页面代码:
<%@ 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 * FROM [Users]"
UpdateCommand="UPDATE [Users] SET [username] = @username, [password] = @password, [signin_date] = @signin_date, [age] = @age, [picture] = @picture WHERE [ID] = @ID"
OnInserting="SqlDataSource1_Inserting" onupdating="SqlDataSource1_Updating"
>
<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="AliceBlue" />
<EditItemTemplate>
<table class="style1">
<tr>
<td rowspan="6">
头像列
<img id="img2" src='<%# Eval("picture").ToString()==""?"nophoto.jpg":Eval("picture")%>' width="90" />
<asp:FileUpload ID="FileUpload1" runat="server" onchange="javascript:img2.src=this.value" />
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("picture") %>'></asp:TextBox>
</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_dateTextBox" runat="server" Text='<%# Bind("signin_date","{0:yyyy-MM-dd}") %>' />
</td>
</tr>
<tr>
<td>
age:
<asp:TextBox ID="ageTextBox" runat="server" Text='<%# Bind("age") %>' />
</td>
<tr>
<td colspan="2">
备注:
</td>
</tr>
</table>
<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 will be generated automatically when inserting!
</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_dateTextBox" runat="server" Text='<%# Bind("signin_date","{0:yyyy-MM-dd}") %>' />
</td>
</tr>
<tr>
<td>
age:
<asp:TextBox ID="ageTextBox" 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" OnClick="InsertButton_Click" />
<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='<%# Eval("username") %>' />
</td>
</tr>
<tr>
<td>
password:
<asp:Label ID="passwordLabel" runat="server" Text='<%# Eval("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='<%# Eval("age") %>' />
</td>
</tr>
<tr>
<td>
picture:
<asp:Label ID="pictureLabel" runat="server" Text='<%# Eval("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>
后台代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Day4_formview : System.Web.UI.Page
{
string filename = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
//e.Command.Parameters["@username"].Value = "zzmissyou";
//e.Command.Parameters["@password"].Value = "pwd";
//e.Command.Parameters["@signin_date"].Value = "2011-09-09";
//e.Command.Parameters["@age"].Value = "90";
e.Command.Parameters["@picture"].Value = filename;
}
protected void InsertButton_Click(object sender, EventArgs e)
{
try
{
FileUpload f1 = this.FormView1.FindControl("FileUpload1") as FileUpload;
if (f1.HasFile)
{
filename = "../photo/" + f1.FileName;
f1.SaveAs(Server.MapPath(filename));
this.SqlDataSource1.Insert();
}
}
catch (Exception ex)
{
this.Label1.Text = ex.Message;
}
}
protected void SqlDataSource1_Updating(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;
}
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
//try
//{
// FileUpload f1 = this.FormView1.FindControl("FileUpload1") as FileUpload;
// if (f1.HasFile)
// {
// filename = "../photo/" + f1.FileName;
// f1.SaveAs(Server.MapPath(filename));
// this.SqlDataSource1.Insert();
// }
//}
//catch (Exception ex)
//{
// this.Label1.Text = ex.Message;
//}
}
}
其他参考答案(1)
提交失败!请检查错误!错误信息:
以下信息或许对您有用:
- [已解决] 20 ASP.NET Webforms中的Page的事件执行顺序是怎么样的? 2010/10/15 12:25:05
- [已解决] 5 CS0029: Cannot implicitly convert type '... 2010/5/14 21:18:31
- [已解决] 20 类型“System.Web.UI.WebControls.SessionPara... 2010/5/7 17:31:20
- [已解决] 5 如何用Javascript获取ASP.NET WebForms中服务器空间Lab... 2010/3/26 0:07:33
- [已解决] 20 ASP.NET WebForms中服务器控件的ID和Name有什么关系? 2010/3/26 0:06:24