2011-10-27 138 views
-2

我已创建存储过程以将数据输入数据库。数据是从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 

在此先感谢。

+0

表中有多少列(超过您提到的4个)?剩下的错误说了什么(它告诉你什么列)?你是否突出了InsertQuestions并检查了参数集合? – gbn

+0

表“问题”的模式是什么?最有可能的是,试图插入空值的列被定义为“NOT NULL”。 –

回答

1

您没有将这些列之一标记为“允许空值”。因此你正在得到上述错误。检查您在“QuestionText”,“QuestionType”或“QuestionnaireID”任一项中插入的值是否为空,并且该列未标记为“允许null”。

它也可能是该表中没有标记为“允许空”的其他列,并且您没有向其中插入任何值,因此默认情况下它试图插入空值。

+0

这不完全是答案:上面的2条评论已经涵盖了这一点。这些注释还有其他选项。 – gbn

+0

@gbn - 这最重要的是解决鲁珀特的问题。 –

+1

@Ramhound:这个问题是在5分钟的宽限期内编写的,以提及其他专栏,否则我不会评论... – gbn

0

假设确切的错误消息是“无法将NULL值插入列”,并且您不知道哪一列为空。要找出哪一列被试图插入为空,首先创建一个表捕获你的错误:

CREATE TABLE Errors (QuestionName NVARCHAR(50), QuestionType INT, QuestId INT, ErrorMessage NVARCHAR(4000), ErrorTime datetime default getdate()) 

接下来,添加的try/catch到存储过程:

BEGIN TRY 
    /* SET NOCOUNT ON */ 
    INSERT INTO [Questions] (QuestionText, QuestionType, QuestionnaireID) VALUES (@QUESTIONNAME, @QUESTIONTYPE, @QUESTID) 
    SET @QUESTION_ID = SCOPE_IDENTITY(); 
    RETURN 
END TRY 

BEGIN CATCH 
    INSERT Errors (QuestionName,QuestionType, QuestId, ErrorMessage) 
     SELECT @QUESTIONNAME, @QUESTIONTYPE,@QUESTID, ERROR_MESSAGE(); 
END CATCH 

从现在起,来自此存储过程的错误将记录到错误表中。 NULL值应该在此处显示以供进一步研究。

0

运行分析器。您需要为某个需要具有值的列发送Null值。无论如何,您希望查看正在生成的SQl以查看造成问题的原因。

相关问题