添加记录到数据库失败!泪奔~~

e78640946 2011-03-17 09:17:00
没有办法添加BookDesc,老是提示错误:另一个SqlParameterCollection 中已包含 SqlParameter.
-----------AddBook.aspx内容如下
<%@ Page Language="C#" MasterPageFile="~/admin/MasterPage.master" AutoEventWireup="true" CodeFile="AddBook.aspx.cs" Inherits="admin_AddBook" Title="无标题页" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td colspan="5">
目前已有产品数量:<asp:Label ID="lbBookId" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td style="width: 292px">
产品编号:</td>
<td colspan="2">
<asp:Label ID="txtboxBookId" runat="server"></asp:Label>
</td>
<td style="width: 77px">

</td>
<td rowspan="8">
<asp:Image ID="Image2" runat="server" Width="100px" Height="120" ImageUrl="7he7_com.jpg"/></td>
</tr>
<tr>
<td style="width: 292px">
唯一编码:</td>
<td colspan="2">
<asp:TextBox ID="txtboxOnlyId" runat="server" Height="22px" Width="300px"></asp:TextBox>
</td>
<td style="width: 77px">
 </td>
</tr>
<tr>
<td style="width: 292px">
图书名称:</td>
<td colspan="2">
<asp:TextBox ID="txtboxBookName" runat="server" Height="22px" Width="300px"
MaxLength="100" Wrap="False"></asp:TextBox>
</td>
<td style="width: 77px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtboxBookName" ErrorMessage="该项不能为空" ValidationGroup="1">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 292px">
图书售价:</td>
<td colspan="2">
<asp:TextBox ID="txtboxBookPrice" runat="server" Height="22px" Width="300px"
MaxLength="100" Wrap="False"></asp:TextBox>
</td>
<td style="width: 77px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="txtboxBookPrice" ErrorMessage="该项不能为空" ValidationGroup="1">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 292px">
图书数量:</td>
<td colspan="2">
<asp:TextBox ID="txtboxBookQty" runat="server" Height="22px" Width="300px"
MaxLength="100" Wrap="False"></asp:TextBox>
</td>
<td style="width: 77px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtboxBookQty" ErrorMessage="该项不能为空" ValidationGroup="1">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 292px">
目录名称:</td>
<td colspan="2">

<asp:DropDownList ID="dropdownCategoryId" runat="server" Height="22px" Width="300px"
DataTextField="Name" DataValueField="Id" AutoPostBack="True"></asp:DropDownList>

</td>
<td style="width: 77px">
 </td>
</tr>
<tr>
<td style="width: 292px">
缩略图名:</td>
<td colspan="2">

<asp:TextBox ID="lbsmall" runat="server" Height="22px" Width="300px"></asp:TextBox>

</td>
<td style="width: 77px">
 </td>
</tr>
<tr>
<td style="width: 292px">
原图名称:</td>
<td colspan="2">

<asp:TextBox ID="lbfull" runat="server" Height="22px" Width="300px"></asp:TextBox>

</td>
<td style="width: 77px">
 </td>
</tr>
<tr>
<td style="width: 292px">
热点推荐:</td>
<td colspan="2">

<asp:DropDownList ID="dropdownIsHeadLine" runat="server" Height="22px" Width="300px">
</asp:DropDownList>


</td>
<td colspan="2">
<asp:FileUpload ID="FileUpload1" runat="server" Width="191px" />
</td>
</tr>
<tr>
<td style="width: 292px">
经典推荐:</td>
<td colspan="2">
<asp:DropDownList ID="dropdownIsClassic" runat="server" Height="22px" Width="300px">
</asp:DropDownList>
</td>
<td colspan="2">
<asp:Button ID="btnUpload" runat="server" onclick="btnUpload_Click"
Text="开始上传" />
<asp:Label ID="Label1" runat="server" ForeColor="#FF0066"></asp:Label>
</td>
</tr>
<tr>
<td style="width: 292px">
产品描述:</td>
<td colspan="2">
<asp:TextBox ID="TextBoxDesc" runat="server" Height="106px" Width="300px"
TextMode="MultiLine"></asp:TextBox>
</td>
<td colspan="2">
 </td>
</tr>
<tr>
<td style="width: 292px">
 </td>
<td style="width: 243px">
<asp:Button ID="btnAddOk" runat="server" Text="添加" Width="85px"
onclick="btnAddOk_Click" ValidationGroup="1"/>

<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label></td>
<td style="width: 211px">

<asp:Button ID="btnAddCancel" runat="server" Text="返回" Width="85px"
onclick="btnAddCancel_Click" CausesValidation="False" />

</td>
<td colspan="2">
 </td>
</tr>
<tr>
<td colspan="5">
<asp:Label ID="ImgUrl" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="5">
<asp:Image ID="Image1" runat="server" Width="200px" Height="200px" ImageUrl="7he7_com.jpg" />
</td>
</tr>
</table>
</asp:Content>
...全文
81 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
net5354 2011-03-17
  • 打赏
  • 举报
回复
太多代码了,最好用最简洁的
e78640946 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wxr0323 的回复:]
引用 3 楼 e78640946 的回复:

劳烦各位高手帮帮忙~~~小妹我看了一天,快给整死了~~~~
能够成功添加BookId,BookName
就是没有办法添加BookDesc

看完你的代码 我也泪奔了。。。

我Ctrl+f找insert 就是没找到。。。

你把添加的代码发来看看。。
[/Quote]

不好意思嘛~~~我是新手
e78640946 2011-03-17
  • 打赏
  • 举报
回复
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[InsertBookBrief]
@BookId varchar(10),
@CategoryId smallint,
@IsHeadLine Bit,
@IsClassic Bit,
@BookName varchar(80),
@BookDesc varchar(255),
@BookImage varchar(80),
@OnlyId varchar(50)
AS
insert into BookBrief
(BookId, CategoryId, IsHeadLine, IsClassic, BookName, BookDesc, BookImage, OnlyId)
values (@BookId, @CategoryId, @IsHeadLine, @IsClassic, @BookName, @BookDesc, @BookImage, @OnlyId)
e78640946 2011-03-17
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BookShop.Model;
using BookShop.Common;


namespace BookShop.DAL
{
public class BookBriefAccess
{
public bool DelectCategory(int CategoryId)
{
//判断BookBrief表是否存在Category的记录
SqlParameter[] parm = { new SqlParameter("@CategoryId", SqlDbType.Int) };
parm[0].Value = CategoryId;
//执行查询
DataBase db = new DataBase();
SqlDataReader dr = db.ExcuteDataReader(CommandType.StoredProcedure, "SelectBookBriefById", parm);
//如果在BookBrief中找到用户输入的CategoryId
if (dr.Read())
{
//关闭连接
dr.Close();
return false;

}
dr.Close();
//执行删除操作
SqlParameter[] parm2 = { new SqlParameter("@CategoryId", SqlDbType.Int) };
parm2[0].Value = CategoryId;
db.ExcuteNonQuery(CommandType.StoredProcedure, "DelCategory", parm2);
return true;


}
//更新
public void AddBookBrief(BookBriefInfo bbf)
{
SqlParameter[] parm = {
new SqlParameter("@BookId",SqlDbType.VarChar,10),
new SqlParameter("@CategoryId", SqlDbType.Int),
new SqlParameter("@IsHeadLine",SqlDbType.Bit),
new SqlParameter("@IsClassic",SqlDbType.Bit),
new SqlParameter("@BookName",SqlDbType.VarChar,80),
new SqlParameter("@BookDesc",SqlDbType.VarChar,255),
new SqlParameter("@BookImage",SqlDbType.VarChar,80),
new SqlParameter("@OnlyId",SqlDbType.VarChar,50)
};
parm[0].Value = bbf.BookId;
parm[1].Value = bbf.CategoryId;
parm[2].Value = bbf.IsHeadLine;
parm[3].Value = bbf.IsClassic;
parm[4].Value = bbf.BookName;
parm[5].Value = bbf.BookDesc;
parm[6].Value = bbf.BookImage;
parm[7].Value = bbf.OnlyId;
DataBase db = new DataBase();
SqlDataReader sdr = db.ExcuteDataReader(CommandType.StoredProcedure, "SelectBookBriefByBookId", parm);

//如果有库存,则更新热点,经典推荐类型及书名
if (sdr.Read())
{

db.ExcuteNonQuery(CommandType.StoredProcedure, "UpdateBookBriefIs", parm);

}
else
{
SqlParameter[] parm1 = {
new SqlParameter("@BookId",SqlDbType.VarChar,10),
new SqlParameter("@CategoryId", SqlDbType.Int),
new SqlParameter("@IsHeadLine",SqlDbType.Bit),
new SqlParameter("@IsClassic",SqlDbType.Bit),
new SqlParameter("@BookName",SqlDbType.VarChar,80),
new SqlParameter("@BookDesc",SqlDbType.VarChar,255),
new SqlParameter("@BookImage",SqlDbType.VarChar,80),
new SqlParameter("@OnlyId",SqlDbType.VarChar,50)
};
parm1[0].Value = bbf.BookId;
parm1[1].Value = bbf.CategoryId;
parm1[2].Value = bbf.IsHeadLine;
parm1[3].Value = bbf.IsClassic;
parm1[4].Value = bbf.BookName;
parm1[5].Value = bbf.BookDesc;
parm1[6].Value = bbf.BookImage;
parm1[7].Value = bbf.OnlyId;
DataBase db1 = new DataBase();
db1.ExcuteNonQuery(CommandType.StoredProcedure, "InsertBookBrief", parm1);
}

}

//执行图书概要修改操作

public void UpdateBookBriefByBookId(BookBriefInfo bbf)
{
DataBase db = new DataBase();
SqlParameter[] parm ={new SqlParameter("@BookId",SqlDbType.VarChar,10),
new SqlParameter("@IsHeadLine",SqlDbType.Bit),
new SqlParameter("@IsClassic",SqlDbType.Bit)};

parm[0].Value = bbf.BookId;
parm[1].Value = bbf.IsHeadLine;
parm[2].Value = bbf.IsClassic;

db.ExcuteNonQuery(CommandType.StoredProcedure, "UpdateBookBriefByBookId", parm);
}
}
}
子夜__ 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 e78640946 的回复:]

劳烦各位高手帮帮忙~~~小妹我看了一天,快给整死了~~~~
能够成功添加BookId,BookName
就是没有办法添加BookDesc
[/Quote]
看完你的代码 我也泪奔了。。。

我Ctrl+f找insert 就是没找到。。。

你把添加的代码发来看看。。
gemzboh888 2011-03-17
  • 打赏
  • 举报
回复
我是新手,进来学习的。请忽略我!
e78640946 2011-03-17
  • 打赏
  • 举报
回复
劳烦各位高手帮帮忙~~~小妹我看了一天,快给整死了~~~~
能够成功添加BookId,BookName
就是没有办法添加BookDesc
e78640946 2011-03-17
  • 打赏
  • 举报
回复

---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BookShop.Model
{

/// <summary>
/// Business entity used to model a product
/// </summary>
[Serializable]
public class BookBriefInfo {

// Internal member variables
private string bookId;//书号

public string BookId
{
get { return bookId; }
set { bookId = value; }
}
private string bookName;//图书概要名称

public string BookName
{
get { return bookName; }
set { bookName = value; }
}
private string bookDesc;//图书介绍

public string BookDesc
{
get { return bookDesc; }
set { bookDesc = value; }
}
private string bookImage;//图书概要图片

public string BookImage
{
get { return bookImage; }
set { bookImage = value; }
}
private string onlyId;//产品唯一编码

public string OnlyId
{
get { return onlyId; }
set { onlyId = value; }
}

private string categoryId;//目录编号

public string CategoryId
{
get { return categoryId; }
set { categoryId = value; }
}
private DateTime addTime;//添加时间

public DateTime AddTime
{
get { return addTime; }
set { addTime = value; }
}


private bool isHeadLine;//热点

public bool IsHeadLine
{
get { return isHeadLine; }
set { isHeadLine = value; }
}


private bool isClassic;//经典

public bool IsClassic
{
get { return isClassic; }
set { isClassic = value; }
}


/// <summary>
/// Default constructor
/// </summary>
public BookBriefInfo() { }

/// <summary>
/// Constructor with specified initial values
/// </summary>

public BookBriefInfo(string id, string oid, string name, string description, string image, string categoryId,DateTime addtime,bool headline,bool classic) {
this.bookId = id;
this.bookName = name;
this.bookDesc = description;
this.bookImage = image;
this.OnlyId = oid;
this.categoryId = categoryId;
this.addTime = addtime;
this.isHeadLine = headline;
this.isClassic = classic;
}



}
}
e78640946 2011-03-17
  • 打赏
  • 举报
回复
-----------AddBook.aspx.cs内容如下
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BookShop.BLL;
using BookShop.Model;
using System.IO;
using System.Data.SqlClient;

public partial class admin_AddBook : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/**//** */
string strConn = System.Configuration.ConfigurationManager.AppSettings["conn"].ToString();
SqlConnection Conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("SELECT MAX(BookId) FROM BookBrief", Conn);
Conn.Open();
lbBookId.Text = cmd.ExecuteScalar().ToString();
int i = int.Parse(lbBookId.Text);
i = i + 1;
txtboxBookId.Text = i.ToString();
Conn.Close();

Image1.Attributes.Add("onclick", "rem(this.src)");
Image2.Attributes.Add("onclick", "rem(this.src)");

if (!IsPostBack)
{
this.Bind();
}


}


public void Bind()
{

//GetCategoryManager cm = new GetCategoryManager();
//gvCategorys.DataSource = cm.GetCategoryInfos();
//gvCategorys.DataBind();

//添充下拉列表
GetCategoryManager cm1 = new GetCategoryManager();
dropdownCategoryId.DataSource = cm1.GetCategoryInfos();
dropdownCategoryId.DataBind();

string[] f = { "是", "否" };
dropdownIsHeadLine.DataSource = f;
dropdownIsHeadLine.DataBind();

dropdownIsClassic.DataSource = f;
dropdownIsClassic.DataBind();
}


protected void btnAddOk_Click(object sender, EventArgs e)
{
BookBriefInfo bbf = new BookBriefInfo();
bbf.BookId = txtboxBookId.Text.Trim();
bbf.CategoryId = dropdownCategoryId.SelectedValue;
bbf.BookName = txtboxBookName.Text.Trim();
bbf.BookDesc = TextBoxDesc.Text.Trim();
bbf.BookImage = lbsmall.Text.Trim();
bbf.OnlyId = txtboxOnlyId.Text.Trim();

string isClassic = dropdownIsClassic.SelectedValue;
if (isClassic.Equals("是"))
{
bbf.IsClassic = true;
}
else
{
bbf.IsClassic = false;
}


string isHeadLine = dropdownIsHeadLine.SelectedValue;
if (isHeadLine.Equals("是"))
{
bbf.IsHeadLine = true;
}
else
{
bbf.IsHeadLine = false;
}
//开始更新BookBrief表
BookBriefManager bbm = new BookBriefManager();
bbm.AddBookBrief(bbf);

//收集Item表的数据
ItemInfo info = new ItemInfo();
info.BookId = txtboxBookId.Text.Trim();
info.BookPrice = Decimal.Parse(txtboxBookPrice.Text.Trim());
info.Qty = Int32.Parse(txtboxBookQty.Text.Trim());

//开始更新Item表
ItemManager im = new ItemManager();
im.AddItem(info);


lblmsg.Text = "添加成功!";

}
protected void btnAddCancel_Click(object sender, EventArgs e)
{
Response.Redirect("BookManager.aspx");
}

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧