SubmitChanges后就报一个错 code:4004. Category:ManagedRuntimeError

这是由于数据库中插入的表没有设置主键而造成的。

注意事项:

  1. 数据库中的表一定要有主键。因为我不是专业人事,我一直没认为主键是个必须的东西,所以我的个别数据表因为想偷懒而没有设置主键,尤其在我设置了自增值字段如***id以后。但是如果在这种情况下你执行SubmitChanges()程序会处于停滞状态,也不报错。我实验了半天,终于想起了生成ado实体数据时vs2010给我的警告,如果没有主键只能生成只读的表。

  2.还是数据库问题。本地测试的项目请不要使用windows用户连接你的数据库资源,最好使用sa用户连接。如果使用了请注意修改web.config。否则发布到本地的iis上会有问题。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是我的代码,using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class index : System.Web.UI.Page { //先新建一个Linq的操作对象 StudentDataDataContext stdDb = new StudentDataDataContext (); protected void Page_Load(object sender, EventArgs e) { //给下拉框添加年龄选项 for(int i = 15; i < 22; i++) { ListItem item = new ListItem(); item.Text = i.ToString(); item.Value = i.ToString(); DropDownList1.Items.Add(item); } } protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) { } protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) { } //添加按钮的处理代码 protected void Button1_Click(object sender, EventArgs e) { //用异常捕获语句 try { //新建student的信息存储对象 Student student = new Student(); //为实体类对象指定需要收集的信息 student.SID = this.TextBox1.Text.Trim(); student.SName = this.TextBox2.Text.Trim(); student.Age = Convert.ToInt32(this.DropDownList1.SelectedValue); //添加到数据库中 stdDb.Student.InsertOnSubmit(student); //提交修改 stdDb.SubmitChanges(); //提示添加成功并返回信息给页面 Button5_Click(sender, e); this.TextBox3.Text = "添加成功!"; } catch (Exception es) { this.TextBox3.Text = "添加失败!误原因是"+es.Message; } } //显示全部数据的按钮点击槽函数 protected void Button5_Click(object sender, EventArgs e) { //1.先将查询的结果存储在结果对象中 var result = from Student in stdDb.Student orderby Student.SID select Student; //2.将数据源指定给sqlDataSource控件 this.GridView2.DataSourceID = ""; this.GridView2.DataSource = result; //3.更新表格 this.GridView2.DataBind(); } }请帮我查看为什么报错
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值