0

I have a SQL stored procedure which uses openrowset command and fetches values from an excel sheet and inserts it into the database.

I have created a C# application which will call the procedure and execute it.

PROBLEM!

When I execute the procedure from SQL management studio, there are no errors. It happens perfectly. But when I execute it through the C# application I get an error: "Conversion failed when converting date and/or time from character string."

Code

SQL Query (only the insert part)

insert into  tbl_item ([Item code],[Dt Created])

select[Item code] ,

case when [Dt Created] is null or [Dt Created]='' then null when ISDATE(CONVERT(nvarchar,CONVERT(datetime, [Dt Created],103))) =1 then CONVERT(datetime, [Dt Created],103) else null end as [Dt Created]    

FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0; Database=C:\Upload\Report.xlsx;HDR=YES;IMEX=1;',
                    'select * from [Sheet1$]')

C# Code

public int updateItem()
{
        SqlCommand cmd; cmd = new SqlCommand("usp_updateItem", conn); 
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter returnParameter = cmd.Parameters.Add("RetVal", SqlDbType.Int);
        returnParameter.Direction = ParameterDirection.ReturnValue;
        try
        {
            if (conn.State.Equals(ConnectionState.Closed))
                conn.Open();
            cmd.ExecuteNonQuery();
            ret = Convert.ToInt32(returnParameter.Value);
        }
        catch (Exception e)
        {
            err = "Error: " + e.Message;
            return -1;
        }
        finally
        {
            conn.Close();
        }
        return ret;
    }
2
  • I think you forgot to paste your CODE :D :P Commented Nov 18, 2015 at 6:11
  • The C# code is just a simple ExecuteNonQuery ... Just a call to the SQL procedure.. So didn't bother posting it :) Commented Nov 18, 2015 at 6:14

3 Answers 3

2

What is the format you are having in the [Dt Created] variable.

the convert statement you have in the case will convert only the following types below

YYYY-MM-DD

YYYY-DD-MM

DD-MM-YYYY

The error you are getting is since you have a date in the format of "MM-DD-YYYY" something like '12-24-2015'. Due to this you are getting the conversion error.

Sign up to request clarification or add additional context in comments.

Comments

1

Excuse me I want to stop you here. Your problem has resolved now but whatever Karthik Venkatraman had said is correct. Somehow you got solution but for learning purpose i recommended to investigate little bit more. This is not belongs to the whatever you have said but damm sure this belongs to date-format.

**

One trick

Create one DateTimeVariable and once its initialized then just parse it using DateTimeParse class according to the records exist in database.

I am sure you will get solution.. Thanks :)

Comments

0

This is how I finally solved it...

The SQL error message 'Failed Conversion' was absolutely a wrong pointer. It had no connection to the issue at hand. [If only I knew this before :( ]

The actual problem was that I had called another procedure within the main procedure I had posted above. This setup ran perfectly in SQL management studio which was running under my credentials. Now in the C# application, I had created another SQL login user ID to run it. And this user ID did not have execute permission to run the sub procedure. And ironically, SQL gave me a misleading conversion error. Once I gave the right permission it worked perfectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.