我已创建存储过程以将数据输入数据库。数据是从asp:textboxes输入,然后解析为类和方法和.cs文件。但是,在运行时,我给出了以下错误。 无法插入NULL值插入列无法将NULL值插入列(SQL-Server)
add_question.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Devworks;
namespace OSQARv0._1
{
public partial class WebForm2 : System.Web.UI.Page
{
OscarSQL b;
protected void Page_Load(object sender, EventArgs e)
{
string QuestionnaireName = (string)Session["QuestionnaireName"];
int QuestionnaireID = (int)Session["QuestionnaireID"];
ReturnQnrName.Text = QuestionnaireName + " (ID: " + QuestionnaireID.ToString() + ")";
}
protected void FinishQnrButton_Click(object sender, EventArgs e)
{
b = new OscarSQL();
int testRetn = b.InsertQuestions(QuestionName.Text, Int32.Parse(QuestnType.SelectedValue), Int32.Parse(QuestionnaireID.Text));
int QuestionID = (int)Session["QuestionID"];
testlabel.Text = QuestionID.ToString();
} // End NewQNRButton_Click
} // End WebForm2
} // End new_questionnaire
OscarSQL.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Devworks
{
public class OscarSQL
{
private string _productConnectionString;
private SqlConnection _productConn;
public OscarSQL()
{
_productConn = new SqlConnection();
_productConnectionString += "data source=mssql.database.co.uk; Initial Catalog=devworks_oscar;User ID=username;Password=password";
_productConn.ConnectionString = _productConnectionString;
}
public int InsertQuestions(string QuestionName, int QuestionType, int QuestionnaireID)
{
int retnVal = 0;
SqlCommand myCommand = new SqlCommand("NewQuestion", _productConn);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@QUESTIONNAME", SqlDbType.NVarChar));
myCommand.Parameters.Add(new SqlParameter("@QUESTIONTYPE", SqlDbType.Int));
myCommand.Parameters.Add(new SqlParameter("@QUESTID", SqlDbType.Int));
myCommand.Parameters.Add("@QUESTION_ID", SqlDbType.Int, 0, "QUESTION_ID");
myCommand.Parameters["@QUESTION_ID"].Direction = ParameterDirection.Output;
myCommand.Parameters[0].Value = QuestionName;
myCommand.Parameters[1].Value = QuestionType;
myCommand.Parameters[2].Value = QuestionnaireID;
_productConn.Open();
myCommand.ExecuteNonQuery();
retnVal = (int)myCommand.Parameters["@QUESTION_ID"].Value;
_productConn.Close();
return retnVal;
}
private void insert(SqlCommand myCommand)
{
_productConn.Open();
myCommand.ExecuteNonQuery();
_productConn.Close();
}
}
}
存储Prodcedure
USE [devworks_oscar]
GO
/****** Object: StoredProcedure [hgomez].[NewQuestion] Script Date: 10/27/2011 17:10:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [hgomez].[NewQuestion]
(
@QUESTIONNAME nvarchar(50),
@QUESTIONTYPE int,
@QUESTID int,
@QUESTION_ID int OUTPUT
)
AS
/* SET NOCOUNT ON */
INSERT INTO [Questions] (QuestionText, QuestionType, QuestionnaireID) VALUES (@QUESTIONNAME, @QUESTIONTYPE, @QUESTID)
SET @QUESTION_ID = SCOPE_IDENTITY();
RETURN
在此先感谢。
表中有多少列(超过您提到的4个)?剩下的错误说了什么(它告诉你什么列)?你是否突出了InsertQuestions并检查了参数集合? – gbn
表“问题”的模式是什么?最有可能的是,试图插入空值的列被定义为“NOT NULL”。 –