Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
/// <summary>
/// Takes an Error object and saves its values to the database
/// </summary>
/// <param name="e"></param>
public void SaveError(Error e)
    {
    // Save error incident to db
    string connectionString = Properties.Settings.Default.PlatinumDBConnectionString;
    string cmdString = 
"INSERT INTO dbo.Errors(eName,eInstance,eMessage,eModule,eProcedure,eUserId,eUserMessage,ecName,ecCode,ecDescription,ecMessage) VALUES (@val1, @va2, @val3,@val4,@val5,@val6,@val7,@val8,@val9,@val10,@val11)";

    using (SqlConnection conn = new SqlConnection(connectionString))
        {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(cmdString, conn))
            {
            // assign value to parameters 
            cmd.Parameters.AddWithValue("@val1", e.Name);
            cmd.Parameters.AddWithValue("@val2", e.Instance);
            cmd.Parameters.AddWithValue("@val3", e.Message);
            cmd.Parameters.AddWithValue("@val4", e.Module);
            cmd.Parameters.AddWithValue("@val5", e.Procedure);
            cmd.Parameters.AddWithValue("@val6", e.UserId);
            cmd.Parameters.AddWithValue("@val7", e.UserMessage);
            cmd.Parameters.AddWithValue("@val8", e.ErrorCode.Name);
            cmd.Parameters.AddWithValue("@val9", e.ErrorCode.Code);
            cmd.Parameters.AddWithValue("@val10", e.ErrorCode.Description);
            cmd.Parameters.AddWithValue("@val11", e.ErrorCode.Message);
            cmd.ExecuteNonQuery();
            }
        conn.Close();
        }
    }
#endregion
}

I added the transaction command but still no row created

C#
public void SaveError(Error e)
          {
          // Save error incident to db
          string connectionString = Properties.Settings.Default.PlatinumDBConnectionString;
          string cmdString =
     "INSERT INTO dbo.Errors(eName,eInstance,eMessage,eModule,eProcedure,eUserId,eUserMessage,ecName,ecCode,ecDescription,ecMessage) VALUES (@val1, @va2, @val3,@val4,@val5,@val6,@val7,@val8,@val9,@val10,@val11)";

          using (SqlConnection conn = new SqlConnection(connectionString))
              {
              conn.Open();
              SqlTransaction trans = conn.BeginTransaction();
              using (SqlCommand cmd = new SqlCommand(cmdString, conn))
                  {
                  cmd.Transaction = trans;
                  // assign value to parameters
                  cmd.Parameters.AddWithValue("@val1", e.Name);
                  cmd.Parameters.AddWithValue("@val2", e.Instance);
                  cmd.Parameters.AddWithValue("@val3", e.Message);
                  cmd.Parameters.AddWithValue("@val4", e.Module);
                  cmd.Parameters.AddWithValue("@val5", e.Procedure);
                  cmd.Parameters.AddWithValue("@val6", e.UserId);
                  cmd.Parameters.AddWithValue("@val7", e.UserMessage);
                  cmd.Parameters.AddWithValue("@val8", e.ErrorCode.Name);
                  cmd.Parameters.AddWithValue("@val9", e.ErrorCode.Code);
                  cmd.Parameters.AddWithValue("@val10", e.ErrorCode.Description);
                  cmd.Parameters.AddWithValue("@val11", e.ErrorCode.Message);
                  cmd.ExecuteNonQuery();
                  trans.Commit();
                  }
              conn.Close();
              }
          }


Here is the server dump from the table

SQL
USE [PlatinumDB]
GO

/****** Object:  Table [dbo].[Errors]    Script Date: 13/03/2015 12:33:17 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Errors](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [eName] [nchar](50) NULL,
    [eInstance] [nchar](50) NULL,
    [eMessage] [nvarchar](max) NULL,
    [eModule] [nvarchar](50) NULL,
    [eProcedure] [nvarchar](50) NULL,
    [eUserId] [nvarchar](50) NULL,
    [eUserMessage] [nvarchar](max) NULL,
    [ecName] [nchar](50) NULL,
    [ecCode] [nchar](50) NULL,
    [ecDescription] [nvarchar](max) NULL,
    [ecMessage] [nvarchar](max) NULL,
 CONSTRAINT [PK_Errors] PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
Posted
Updated 12-Mar-15 16:36pm
v7
Comments
[no name] 12-Mar-15 20:42pm    
What's the error message?
Ozzie Mozzie 12-Mar-15 20:55pm    
I get no error message in the application but it seems to return on cmd.ExecuteNonQuery();
[no name] 12-Mar-15 21:02pm    
In which way does it fail? No record gets saved? What about a Transaction (Rollback?)
Ozzie Mozzie 12-Mar-15 21:06pm    
No row created and no indication of failure from the server
Ozzie Mozzie 12-Mar-15 22:01pm    
Please help me with an example

Two issues were causing the Insert to fail.
Firstly the typo in the SQL (Thank You manchanx for alerting me to this )

Secondly I needed to have the CommandType set to Text;
C#
using (SqlCommand cmd = new SqlCommand(cmdString, conn))
                   {
                   cmd.CommandType = CommandType.Text;

Thanks to aarif moh shaikh for raising this issue.

I appreciate the time taken by you guys to assist me. :)
 
Share this answer
 
Comments
aarif moh shaikh 14-Mar-15 1:01am    
oh......Thanks & Welcome...
You're using no transaction there. Your INSERT gets rolled back once the connection closes. I assume you know how to use a transaction and just forgot it here? I tried to find a quality example to provide you a link but all examples I found have some quirks, even on MSDN. If you need an example, please leave a comment and I write down an example here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900