0

I am trying to upload a .mdb file from my upload event from c# using stored procedure. but that mdb file is not loading data in tables of my sql server . when i debug the whole code i got above exception. My Stored Procedure parameter is:-(from stored procedure I m showing only its starting as it is sensitive)

  USE [Demo]
    GO

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[UploadToDatabase] 
        @UserId bigint,
        @ClientMachineIP nvarchar(15),
        @LoadType nvarchar(25) ='Upload',
        @InstId bigint = null, 
        @bIsIgnoreErrors bit = 'False'
AS
BEGIN
    SET NOCOUNT ON;

    .
            declare @srcConnection varchar(MAX) 
        set @srcConnection = 'Data Source=PAth Of my .mdb file;Provider=Microsoft.Jet.OLEDB.4.0;'


            declare @ssispath varchar(1000)
            declare @cmd varchar(1000)
            set @ssispath = 'Package Path\Package.dtsx'
            set @cmd = @cmd + 'Package.Variables["SourceConnectionString"].Value = @srcConnection' 
            set @cmd = 'dtexec /F "' + @ssispath + '"' 
            print @cmd
            exec master..xp_cmdshell @cmd

It is after that loading data into my tables that code i m not showing at here.

I am using Linq to SQL for accessing sql server in the code hence for the stored procedure my designer.cs file code is

[Function(Name="dbo.UploadToDatabase")]
        public int UploadToDatabase([Parameter(Name="UserId", DbType="BigInt")] System.Nullable<long> userId, [Parameter(Name="ClientMachineIP", DbType="NVarChar(15)")] string clientMachineIP, [Parameter(Name="LoadType", DbType="NVarChar(25)")] string loadType, [Parameter(Name="InstId", DbType="BigInt")] System.Nullable<long> instId, [Parameter(DbType="Bit")] System.Nullable<bool> bIsIgnoreErrors, [Parameter(DbType="Int")] ref System.Nullable<int> rcout)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), userId, clientMachineIP, loadType, instId, bIsIgnoreErrors, rcout);
            rcout = ((System.Nullable<int>)(result.GetParameterValue(5)));
            return ((int)(result.ReturnValue));
        }

As I have layered architecture stored procedure calling on upload button is as-

DAL -Access.cs file

Public int? RunDTSxProcess(long userID, string clientMachineIP, string loadType, long instID, bool ignoreErrors)
        {
            try
            {
                int? result = 0;
                var run = Demo.UploadToDatabase(userID, clientMachineIP, loadType, instID, ignoreErrors,ref result);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }   

Logic.cs

 public int? ExcuteDTSxProcess(long userID, string clientMachineIP, string loadType, long instID, bool ignoreErrors)
    {
        try

        {
            return new DAL.access().RunDTSxProcess( userID,  clientMachineIP,  loadType,  instID,  ignoreErrors);
        }
        catch (Exception)
        {
            throw;
        }
    }

On button click :-

protected void btnUpload_Click(object sender, EventArgs e)
        {
            pnlUploadData.Style.Value = "display:none;";
            pnlModify.Style.Value = "display:none;";
            if (fileUpload1.HasFile)
            {
                try
                {


                    string savePath = string.Empty;
                    savePath = ConfigurationManager.AppSettings["UploadDBFilePath"];



                    Server.ScriptTimeout = 100000000;

                    fileUpload1.SaveAs(Server.MapPath(savePath + fileUpload1.FileName));

                    try
                    {
                        //Call Upload procedure
                        **int? result = this.UploadModifyDatabase(savePath +
                              fileUploadEspace.FileName, "Upload");**
                        if(result == 0)
                            lblUploadSuccess.Text = "Database Upload successful";
                        else
                            lblUploadSuccess.Text = "Error while uploading the Database. Please try again.";

                    }
                    catch (Exception)
                    {
                        lblUploadSuccess.Text = "Error in append process. Please try again.";

                    }

                    lblUploadSuccess.Text = "File uploaded successfully.";
 catch (Exception ex)
                {
                    lblUploadSuccess.Text = "ERROR: " + ex.Message.ToString();
                }
            }
            else
            {
                lblUploadSuccess.Text = "You have not specified a file.";
            }

UploadModifyDatabase mehtod is:-

   protected int? UploadModifyDatabase(string fileName, string loadType)
            {
                try
                {
                    long userID = 0;
                    if (null != Session["UserID"])
                        userID = long.Parse(Session["UserID"].ToString());
                    long instID = 0;
                    if (null != Session["InstID"])
                        instID = long.Parse(Session["InstID"].ToString());
                    string clientMachineIP = null;

                    bool ignoreErrors = ckhIgnoreErrors.Checked;

                    int? result =  new BLL.logic().ExcuteDTSxProcess(userID, clientMachineIP, loadType, instID, ignoreErrors);
                    return result;

                }
                catch (Exception)
                {

                    throw;
                }
            }

Please tell me what is going wrong in the code. As per some reffered post I tried to add a a rcount variable in stored procedure but issue still not resolved.

  • 1
    Possible duplicate of [Procedure or function !!! has too many arguments specified](https://stackoverflow.com/questions/17292705/procedure-or-function-has-too-many-arguments-specified) – VDWWD Feb 28 '18 at 09:56
  • 1
    `@bIsIgnoreErrors bit = 'False'` - illegal in TSQL. `BIT`s are 0 or 1. I surmise you failed altering this SP and it's still without "new" argument. – Ivan Starostin Feb 28 '18 at 09:57
  • is `ETL_IMPORT_09_01` the same thing as `UploadToDatabase` ? – Marc Gravell Feb 28 '18 at 10:07
  • yes `UploadToDatabase` is calling the same –  Feb 28 '18 at 10:12
  • I changed `@bIsIgnoreErrors bit=0` still result is same –  Feb 28 '18 at 10:20

1 Answers1

1

Your Procedure UploadToDatabase has 5 parameter defined,but in designer.cs you have 6 parameter including rcout. You can change your procedure to have rcout as a parameter or output parameter if you are using it as output parameter.

 ALTER PROCEDURE [dbo].[UploadToDatabase] 
        @UserId bigint,
        @ClientMachineIP nvarchar(15),
        @LoadType nvarchar(25) ='Upload',
        @InstId bigint = null, 
        @bIsIgnoreErrors bit = 'False',
        @rcout int OUTPUT
AS
BEGIN
    SET NOCOUNT ON;
    declare @srcConnection varchar(MAX) 
    set @srcConnection = 'Data Source=PAth Of my .mdb file;Provider=Microsoft.Jet.OLEDB.4.0;'
    declare @ssispath varchar(1000)
    declare @cmd varchar(1000)
    set @ssispath = 'Package Path\Package.dtsx'
    set @cmd = @cmd + 'Package.Variables["SourceConnectionString"].Value = @srcConnection' 
    set @cmd = 'dtexec /F "' + @ssispath + '"' 
    print @cmd
    exec master..xp_cmdshell @cmd
    Select @rcout= @@ROWCOUNT
Rojalin Sahoo
  • 1,025
  • 1
  • 7
  • 18
  • I edited my post and added portion of my stored procedure. in stored procedure I m not returning any output parameter so if i declare `rcout` as a output parameter then i need to return it through the stored procedure as result after executing –  Feb 28 '18 at 10:27
  • what is the rcout value resulting from **designer.cs**. `rcout = ((System.Nullable)(result.GetParameterValue(5)));` – Rojalin Sahoo Feb 28 '18 at 10:30
  • @swati_9385623 you are mistaken. Rojalin is absolutely correct. If you wish to have `rcout` as an output (dataset, or "result" value ) of SP - you must not declare it as an argument. – Ivan Starostin Feb 28 '18 at 10:30
  • so can I Add that last line in my stored procedure as @Rojalin said –  Feb 28 '18 at 10:34
  • Shown part of SP is not giving any sense to what this `rcout` is supposed to be. – Ivan Starostin Feb 28 '18 at 10:36
  • as when I am running my stored procedure through the sqlserver that time it is giving me the expected result i.e data is loading into my tables and also it returning 0 –  Feb 28 '18 at 10:44
  • as when I added `rcout int ` in my stored porcedure after running the code it giving anothe exception like ` the formal parameter \"@rcout\" was not declared as an output parameter, but the actual parameter passed in requested output.` –  Feb 28 '18 at 11:05
  • Procedures `RETURN` value is not an argument of it. Neither output nor input parameter. If you still need it's value you must access it in somewhat else way. – Ivan Starostin Feb 28 '18 at 11:22
  • @swati_9385623 , Let me update the code as you required. What I was telling is like as per the code the SP should change or code should be change as per the SP. As both has different parameter length. As per your code **rcout** is ouput parameter form SP. Below link will give you a better idea. https://weblogs.asp.net/zeeshanhirani/iexecuteresult-to-return-data-from-stored-procedures – Rojalin Sahoo Feb 28 '18 at 11:53
  • @RojalinSahoo I suppose OP needs something like that: https://stackoverflow.com/questions/6210027/calling-stored-procedure-with-return-value – Ivan Starostin Feb 28 '18 at 12:07
  • @RojalinSahoo I think your answer is right mam as I updated my stored procedure now my c# code calling stored procedure but the problem is it showing me timeout error. after solving it i will sure about that it works for me or not. –  Feb 28 '18 at 12:22
  • @IvanStarostin yes this can be implemented as well. – Rojalin Sahoo Feb 28 '18 at 12:23
  • @RojalinSahoo rcout result in 3601 rowcount. so mam in reult variable it should pass 3601 a as value ?? –  Mar 01 '18 at 14:02
  • on button click i mention if clause as - `if(result==0)` so this condition also need to modify or not. I am confuse in that. –  Mar 01 '18 at 14:04
  • @swati_9385623, As per my knowledge `@@ROWCOUNT` will return the affected row. so you will get `result` value as 0 when no row affected.you could change your condition as per the business rule. – Rojalin Sahoo Mar 02 '18 at 04:46
  • Hello mam I am facing timeout errror exception i set Coomand timeout Period for my mehtod but after the period it again showing me same timeout exception as ` $exception {"Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.\r\ndtexec /F \"C:\DTSX\DTSXPackage\DTSXPackage\Package.dtsx\""} System.Data.SqlClient.SqlException` –  Mar 02 '18 at 09:39