2011-10-27 134 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以查看造成問題的原因。

相關問題