请大虾们帮我修改一下这个简单的与sql server连接的网页注册程序
一个注册程序,在aspx界面里有姓名,邮箱,密码。对于sql server的数据库我建立了一个ebuy,里面有一个表customer,这个表里有CustomerID,CustomerName,EmailAddress,Password四项。
请各位帮我看看,这个程序我运行后提示:无法将 NULL 值插入列 'CustomerID',表'EBuy.dbo.Customers';该列不允许空值。INSERT 失败。语句已终止。
该web程序与数据库ebuy的连接是正常的,问题就出在customerid上,当我把该选项在数据库里
取消,在程序中也取消的时候,是能完成注册的。(即姓名,邮箱,密码的插入语句我想是没有问题的)
strCustomerID = AccountSystem.InsertCustomer(objcustomer)
intCustomerID = sqlcmdInsertCustomer.Parameters.Item("@intCustomerID").Value
程序中与customerid有关的代码我是从书上看来的,不大懂它们的具体含义。
所以我想请大虾们帮我看看,在保持这个程序大体结构不变的情况上,帮我改动一点,并请给我解释与customerID相关的代码,使
其能完成简单的注册功能,谢谢啦!
下面是该web程序的部分代码。
Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Public connstring As String = "Server=(local);database=Ebuy;uid=sa;pwd=;"
Public Class Customer
Public CustomerName As String
Public EmailAddress As String
Public Password As String
End Class
Public Function InsertCustomer(ByVal objCustomer As Customer) As String
Dim sqlcmdInsertCustomer As New SqlCommand("sp_Customers_INS")
With sqlcmdInsertCustomer
.CommandType = CommandType.StoredProcedure
With .Parameters
.Add("@intCustomerID", SqlDbType.Int, 4)
.Add("@nvchrCustomerName", SqlDbType.NVarChar, 50)
.Add("@nvchrEmailAddress", SqlDbType.NVarChar, 50)
.Add("@nvchrPassword", SqlDbType.NVarChar, 10)
.Item("@intCustomerID").Direction = ParameterDirection.Output
.Item("@nvchrCustomerName").Value = objCustomer.CustomerName
.Item("@nvchrEmailAddress").Value = objCustomer.EmailAddress
.Item("@nvchrPassword").Value = objCustomer.Password
End With
End With
Dim sqlconCustomers As New SqlConnection(connstring)
sqlconCustomers.Open()
sqlcmdInsertCustomer.Connection = sqlconCustomers
sqlcmdInsertCustomer.ExecuteNonQuery()
sqlconCustomers.Close()
Dim intCustomerID As Integer
intCustomerID = sqlcmdInsertCustomer.Parameters.Item("@intCustomerID").Value
Return intCustomerID.ToString
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim strCustomerID As String
Dim AccountSystem As New EBuyComm.Services.DBCustomer
Dim myShopper As New EBuyComm.Services.ShoppingCart
Dim tempCartID As String = myShopper.GetCartID
Dim strUserName As String = TextBox1.Text
Dim strPassWord As String = TextBox3.Text
Dim strEmailAddress As String = TextBox2.Text
Dim objcustomer As New EBuyComm.Services.DBCustomer.Customer
objcustomer.CustomerName = strUserName
objcustomer.Password = strPassWord
objcustomer.EmailAddress = strEmailAddress
strCustomerID = AccountSystem.InsertCustomer(objcustomer)
If strCustomerID.ToString.Trim <> "" Then
myShopper.Migrate(tempCartID, strCustomerID)
System.Web.Security.FormsAuthentication.SetAuthCookie(strCustomerID, False)
Response.Cookies("customerName").Value = TextBox1.Text
End If
End Sub
这个是数据库ebuy的存储过程
ALTER PROCEDURE sp_Customers_INS
@intCustomerID int output,
@nvchrCustomerName NVarChar(50),
@nvchrEmailAddress NVarChar(50),
@nvchrPassword NVarChar(10)
AS
INSERT INTO Customers
(
CustomerName ,
EmailAddress,
Password
)
VALUES
(
@nvchrCustomerName,
@nvchrEmailAddress,
@nvchrPassword
)
select @intCustomerID=@@Identity
问题点数:60、回复次数:7Top
1 楼zhuminnick(炅儿)回复于 2005-08-23 19:50:04 得分 0
upTop
2 楼zhilunchen(他山居士)回复于 2005-08-23 21:06:15 得分 35
你的CustomerID字段自定义成IDENTITY属性(即该字段自动增长),在表设计器里选中该字段,将下面的"标识"设为"是"即可.Top
3 楼zhuminnick(炅儿)回复于 2005-08-24 11:10:00 得分 0
呵呵,谢谢zhilunchen(他山居士)的指导 ! 我做出来了。 没想到是这个小地方了,我还一直以为是代码出了问题呢!我以前用access就没这样的问题。
我对其中的代码不大理解,请问:
1、 .Item("@intCustomerID").Direction = ParameterDirection.Output
这个ParameterDirection.Output 是什么意思?
2、 intCustomerID = sqlcmdInsertCustomer.Parameters.Item("@intCustomerID").Value
这一句等号右边是不是要获取自动生成的主键序号??
3、 strCustomerID = AccountSystem.InsertCustomer(objcustomer)
这一句objcustomer是得到一个怎样的返回值呢?
Top
4 楼zhuminnick(炅儿)回复于 2005-08-24 12:19:46 得分 0
upTop
5 楼zhuminnick(炅儿)回复于 2005-08-24 20:12:21 得分 0
没有人回答Top
6 楼jacky_gaoying(碧海长吟)回复于 2005-08-24 20:58:49 得分 25
1,.Item("@intCustomerID").Direction = ParameterDirection.Output ,ParameterDirection.Output是一个枚举,意思是将存储过程参数@intCustomerID的方向设过输出型
2,intCustomerID = sqlcmdInsertCustomer.Parameters.Item("@intCustomerID").Value
你的理解没错
3、objcustomer将返回Customer类的一个对象值,具体你提供的信息不全,看不太清楚Top
7 楼zhuminnick(炅儿)回复于 2005-08-25 17:29:45 得分 0
谢谢,我懂了Top




