我已創建存儲過程以將數據輸入數據庫。數據是從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」。 –