Hi, I kind of hit a bump in the road with SQL and C#.

I am sending information through to a stored proc like:

    SqlCommand TestCaseManager = new SqlCommand("TestCaseManager", thisConnection);
    TestCaseManager.CommandType = CommandType.StoredProcedure;
    TestCaseManager.Parameters.Add(new SqlParameter("@TestCaseName", TestCaseName));

    if (c.Controls[2].Text.Equals(string.Empty))
    {TestCaseManager.Parameters.Add(new SqlParameter("@Steps", DBNull.Value));}

    else 
    {TestCaseManager.Parameters.Add(new SqlParameter("@Steps", c.Controls[2].Text));}

    if (UnitTester.GroupBoxCreator.SomeClass.PictureLocation.Count.Equals(0))
    {TestCaseManager.Parameters.Add(new SqlParameter("@Img", DBNull.Value));}

    else 
    {byte[] Img = File.ReadAllBytes(UnitTester.GroupBoxCreator.SomeClass.PictureLocation[r].ToString());
    TestCaseManager.Parameters.Add(new SqlParameter("@Img", Img));}

    thisConnection.Open();
    object ObjTestCaseManager = TestCaseManager.ExecuteScalar();
    thisConnection.Close();

and my stored proc looks like:

    USE [TestCaseManager]
    GO
    /****** Object:  StoredProcedure [dbo].[TestCaseManager]    Script Date: 01/03/2014 10:49:43 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[TestCaseManager]
        @TestCaseName nvarchar(MAX), @Steps nvarchar(MAX), @Img image   
    AS

    DECLARE @SqlCommand nvarchar(MAX)
    SET @SqlCommand = 'INSERT INTO '+@TestCaseName+' ([Steps], [Img]) VALUES (' +@Steps+', '+@Img+')'
    EXEC(@SqlCommand)

The error that I receive for the SQL part is : "The data types nvarchar(max) and image are incompatible in the add operator."

Any help would be appreciated. Thanks

You're trying to save a string into an image type, those two are incompatible. You need to extract a byte array of the image to pass to SQL.

Try specifying the type when you add the parameter

TestCaseManager.Parameters.Add(new SqlParameter("@Img", SqlDbType.Image, Img));

I tried that and now it says "Declare scalar variable "@Img"" something something, I'm not at my PC at the moment and my stupid Internet Explorer did not want to post this message today.

Can you change the data type from image to nvarchar or something ?

That won't help, but it says that it cant convert byte to string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.