Bilal Haroon
Bilal Haroon

Reputation: 11

Stored Procedure not updating database asp.net c#

Here is the Stored Procedure in the database.

CREATE PROCEDURE Taskedb_Insert
(

    @nNewID int output,
    @sFromEmail varchar(200)  = NULL,
    @sToEmail varchar(200)  = NULL,
    @sEmailSubject varchar(400)  = NULL,
    @sEmailBody varchar(MAX)  = NULL,
    @dEmailDate datetime  = NULL
)   
AS
 BEGIN

INSERT INTO [Taskedb]
(

    FromEmail, 
    ToEmail, 
    EmailSubject, 
    EmailBody, 
    EmailDate
) VALUES (

    @sFromEmail,
    @sToEmail,
    @sEmailSubject,
    @sEmailBody,
    @dEmailDate
)
SELECT @nNewID = @@IDENTITY
END

And This is the code in ASP.NET C#

string connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=tbd_email_tasks;Integrated Security=True";

SqlConnection connection = new SqlConnection(connectionString);

connection.Open();

SqlCommand cmd = new SqlCommand("Taskedb_Insert", connection);
cmd.CommandType = System.Data.CommandType.StoredProcedure;

SqlParameter p1 = cmd.Parameters.Add("@nNewID", System.Data.SqlDbType.Int);
p1.Direction = System.Data.ParameterDirection.Output;

SqlParameter p2 = cmd.Parameters.Add("@sEmailSubject", System.Data.SqlDbType.VarChar);
p2.Value = subject;

SqlParameter p3 = cmd.Parameters.Add("@sEmailBody", System.Data.SqlDbType.VarChar);
p3.Value = body;

SqlParameter p4 = cmd.Parameters.Add("@sToEmail", System.Data.SqlDbType.VarChar);
p4.Value = to;

SqlParameter p5 = cmd.Parameters.Add("@sFromEmail", System.Data.SqlDbType.VarChar);
p5.Value = from;

SqlParameter p6 = cmd.Parameters.Add("@dEmailDate", System.Data.SqlDbType.DateTime);
p6.Value = date;

//connection.Open();
cmd.ExecuteNonQuery();

It is not updating database. Please help me.

Upvotes: 0

Views: 344

Answers (2)

Bilal Haroon
Bilal Haroon

Reputation: 11

Thanks all for the help

I was getting this error because my table Taskedb had

nvarchar(50)

but I was injecting 67 character strength, which resulted in this error. Changing it to

nvarchar(MAX)

Problem Fixed

Upvotes: 1

Konstantin Smirnov
Konstantin Smirnov

Reputation: 1

You could check if it's probably in a transaction scope or such some . And of course I'd wrap your db connection and command in using block.

Upvotes: 0

Related Questions