我有一個存儲過程,即將行同時插入多個表。存儲過程中存在的問題是它沒有被解僱。你可以看看這個語法是否正確並正常工作。將行插入多個表失敗
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InquiryPostReply]
(
@Inquiry_id VARCHAR(50),
@User_id VARCHAR(50),
@Priority_type VARCHAR(25),
@Status_name VARCHAR(50),
@Inquiry_Content VARCHAR(1024),
@Question_id NUMERIC(18,0),
@user_name VARCHAR(50) OUT,
@user_email VARCHAR(50) OUT,
@NewId VARCHAR(50) OUT
)
AS
BEGIN
SET NOCOUNT ON;
declare @var1 int
declare @var2 int
declare @uniqueRef char(14)
set @uniqueRef = dbo.UniqueRefNum(rand(), rand(), rand(), rand())
set @var1= (SELECT [Id] FROM [OmStocks].[dbo].[tbl_Status_master] WHERE ([email protected]_name))
set @var2= (SELECT [Id] FROM [OmStocks].[dbo].[tbl_Priority_master] WHERE ([email protected]_type))
set @user_email=(SELECT [Email_Address] FROM [OmStocks].[dbo].[tbl_Inquiry_History] WHERE ([email protected]_id))
set @user_name= (SELECT tbl_User_master.Full_Name FROM tbl_Inquiry_History INNER JOIN tbl_User_master ON tbl_Inquiry_History.User_id = tbl_User_master.Id WHERE([email protected]_id))
IF @var1 = '1'
BEGIN
INSERT INTO [OmStocks].[dbo].[tbl_Inquiry_master]
([Id],[Inquiry_id],[User_id],[Priority_id],[Status_id],[body])
VALUES
(@uniqueRef,@Inquiry_id,@User_id,@var2,@var1,@Inquiry_Content)
INSERT INTO [OmStocks].[dbo].[tbl_Answer]
([Question_id],[body],[Created_date])
VALUES
(@Question_id,@Inquiry_Content,CONVERT(DATETIME,GETDATE(), 101))
UPDATE [OmStocks].[dbo].[tbl_Inquiry_History]
SET [Priority_id] = @var2,[Status_id] = @var1,[IsDisplay] = 1,[IsReplied] = 1,[TimeStamp] = CONVERT(DATETIME,GETDATE(), 101)
WHERE ([email protected]_id)
END
ELSE IF @var1 = '2'
BEGIN
INSERT INTO [OmStocks].[dbo].[tbl_Inquiry_master]
([Id],[Inquiry_id],[User_id],[Priority_id],[Status_id],[body])
VALUES
(@uniqueRef,@Inquiry_id,@User_id,@var2,@var1,@Inquiry_Content)
INSERT INTO [OmStocks].[dbo].[tbl_Answer]
([Question_id],[body],[Created_date])
VALUES
(@Question_id,@Inquiry_Content,CONVERT(DATETIME,GETDATE(), 101))
UPDATE [OmStocks].[dbo].[tbl_Inquiry_History]
SET [Priority_id] = @var2,[Status_id] = @var1,[IsDisplay] = 1,[IsReplied] = 1,[TimeStamp] = CONVERT(DATETIME,GETDATE(), 101)
WHERE ([email protected]_id)
END
ELSE IF @var1 = '3'
BEGIN
INSERT INTO [OmStocks].[dbo].[tbl_Inquiry_master]
([Id],[Inquiry_id],[User_id],[Priority_id],[Status_id],[body])
VALUES
(@uniqueRef,@Inquiry_id,@User_id,@var2,@var1,@Inquiry_Content)
INSERT INTO [OmStocks].[dbo].[tbl_Answer]
([Question_id],[body],[Created_date])
VALUES
(@Question_id,@Inquiry_Content,CONVERT(DATETIME,GETDATE(), 101))
UPDATE [OmStocks].[dbo].[tbl_Inquiry_History]
SET [Priority_id] = @var2,[Status_id] = @var1,[IsDisplay] = 0,[IsReplied] = 1,[TimeStamp] = CONVERT(DATETIME,GETDATE(), 101),[Activity_expire_time] = CONVERT(DATETIME,GETDATE(), 101)
WHERE ([email protected]_id)
END
SET @NewId = @uniqueRef
END
怎麼過這個存儲過程工作正常,而我從管理工作室從代碼執行的背後它的成功執行,但不插入記錄表。那裏爲我包括這個databasehelper.cs代碼:
/// <summary>
/// Adds answer in the [tbl_Inquiry_master] table.
/// </summary>
public static void AddPostReply(string inquiry_id,string user_id,string priority_type, string status_name, string inquiry_content,int question_id,out string reply_id,out string user_name,out string user_email)
{
using (SqlConnection con = Util.GetConnection())
{
con.Open();
AddPostReply(con,inquiry_id,user_id,priority_type, status_name, inquiry_content, question_id,out reply_id,out user_name,out user_email);
}
}
/// <summary>
/// Adds answer in the [tbl_Inquiry_master] table.
/// </summary>
public static void AddPostReply(SqlConnection con, string inquiry_id, string user_id, string priority_type, string status_name, string inquiry_content, int question_id, out string reply_id, out string user_name, out string user_email)
{
AddPostReply(con, null, inquiry_id, user_id, priority_type, status_name, inquiry_content, question_id, out reply_id, out user_name, out user_email);
}
/// <summary>
/// Adds answer in the [tbl_Inquiry_master] table.
/// </summary>
public static void AddPostReply(SqlConnection con, SqlTransaction trans, string inquiry_id, string user_id, string priority_type, string status_name, string inquiry_content, int question_id, out string reply_id, out string user_name, out string user_email)
{
using (SqlCommand cmd = new SqlCommand("[dbo].[InquiryPostReply]", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Inquiry_id", SqlDbType.VarChar).Value = inquiry_id;
cmd.Parameters.Add("@User_id", SqlDbType.VarChar).Value = user_id;
cmd.Parameters.Add("@Priority_type", SqlDbType.VarChar).Value = priority_type;
cmd.Parameters.Add("@Status_name", SqlDbType.VarChar).Value = status_name;
cmd.Parameters.Add("@Inquiry_Content", SqlDbType.VarChar).Value = inquiry_content;
cmd.Parameters.Add("@Question_id", SqlDbType.Int).Value = question_id;
cmd.Parameters.Add("@user_name", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@user_email", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@NewId", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
if (trans != null)
trans = con.BeginTransaction();
cmd.ExecuteNonQuery();
trans.Commit();
user_name = Convert.ToString(cmd.Parameters["@user_name"].Value);
user_email = Convert.ToString(cmd.Parameters["@user_email"].Value);
reply_id = Convert.ToString(cmd.Parameters["@NewId"].Value);
}
}
--------------------------------- - 已更新------------------------------------------
該錯誤發生這樣的:
Server Error in '/OmInvestmentStockMarketing_new' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 140: trans = con.BeginTransaction();
Line 141: cmd.ExecuteNonQuery();
Line 142: trans.Commit();
Line 143: user_name = Convert.ToString(cmd.Parameters["@user_name"].Value);
Line 144: user_email = Convert.ToString(cmd.Parameters["@user_email"].Value);
Source File: c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\App_Code\DatabaseHelper.cs Line: 142
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
DatabaseHelper.AddPostReply(SqlConnection con, SqlTransaction trans, String inquiry_id, String user_id, String priority_type, String status_name, String inquiry_content, Int32 question_id, String& reply_id, String& user_name, String& user_email) in c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\App_Code\DatabaseHelper.cs:142
DatabaseHelper.AddPostReply(SqlConnection con, String inquiry_id, String user_id, String priority_type, String status_name, String inquiry_content, Int32 question_id, String& reply_id, String& user_name, String& user_email) in c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\App_Code\DatabaseHelper.cs:120
DatabaseHelper.AddPostReply(String inquiry_id, String user_id, String priority_type, String status_name, String inquiry_content, Int32 question_id, String& reply_id, String& user_name, String& user_email) in c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\App_Code\DatabaseHelper.cs:112
Admin_OWM_Inquiry_Post_Reply.btnsubmit_Click(Object sender, EventArgs e) in c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\Admin\OWM_Inquiry_Post_Reply.aspx.cs:51
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
請幫助我..
對於第三個時間刪除交易和測試。該錯誤在trans.Commit()行上。首先確定問題是否存在於SP中。 – Paparazzi
SP工作正常... –
然後最有可能的反式反應是空的。您還應該在行Commit行上測試null。 – Paparazzi