Skip to content
Advertisement

“Column name or number of supplied values does not match table definition” when trying to upload data to my DB

this is the C# code of the query (trying to make it so I can upload images to my DB table):

protected void ButtonInsert_Click(object sender, EventArgs e)
    {
        insert();
    }

    public void insert()
    {
        if (FileUpload1.PostedFile.FileName != "")
        {
            Byte[] image;
            Stream s = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(s);
            image = br.ReadBytes((Int32)s.Length);

            string connectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UserskfirmDesktopKEY_web_v1KEY_web_v1App_DataDatabase1.mdf;Integrated Security=True";

            SqlConnection conn = new SqlConnection(connectionString);
            //SqlConnection conn = new SqlConnection("data source=.\sqlexpress; initial catalog=SlideShow; integrated security=true");//ORIGINAL - "data source=.\sqlexpress; initial catalog=SlideShow; integrated security=true" //I think he's just making a dataset, I can edit later if it doesnt work or add this to the global.asax
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;

            comm.CommandText = "INSERT into Portfolio values(@ImageData)"; //I changed stuff!
            comm.Parameters.AddWithValue("@ImageData", image); //I changed stuff!

            conn.Open();
            int row = comm.ExecuteNonQuery();
            conn.Close();

            if (row > 0)
            {
                LabelError.Text = "success";
            }

        }
        else LabelError.Text = "please upload an image";
    }

this is the HTML code:

<form  name="AddSiteToPortfolio" action="AddSiteToPortfolio.aspx" method="post" runat="server">
    <table>
        <tr>
            <td>ImageData: </td>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button ID="ButtonInsert" runat="server" Text="Upload Image" 
    onclick="ButtonInsert_Click" />
            <asp:Label ID="LabelError" runat="server" Text=""></asp:Label>
            </tr>
     </table>
</form>

and this is my tables code:

CREATE TABLE [dbo].[Portfolio] (
[Image]       NVARCHAR (50) NOT NULL,
[Description] NVARCHAR (50) NOT NULL,
[ImageData] VARBINARY (max) NULL,
[id]          INT           IDENTITY (1, 1) NOT NULL,
PRIMARY KEY CLUSTERED ([id] ASC)
);

when I try to update (uploading the image’s binary code) the database, I get this error:

Column name or number of supplied values does not match table definition.

and the source error is the line in bold:

conn.Open();
**int n = comm.ExecuteNonQuery();**
conn.Close();

what’s wrong?

Advertisement

Answer

If you indeed wish to INSERT data then you need to pass real values to all columns that are mark NOT NULL in the table definition–except autogenerated columns like “ID”.

So your insert query should look in this:

comm.CommandText = "INSERT INTO Portfolio (Image, Description, ImageData) VALUES(@Image, @Description, @ImageData)";

And the parameters:

comm.Parameters.AddWithValue("@Image", “myImage”);
comm.Parameters.AddWithValue("@Description", “WhateverYouNeed”);
comm.Parameters.AddWithValue("@ImageData", image);



OR FOR UPDATE
If you want to update an existing value you will need an identifier to locate the row and field you wish to UPDATE. In this case, you would need the “ID” of the image you wish to update.

comm.CommandText = “UPDATE [Portfolio] SET [ImageData] = @Image WHERE [ID] = @ID”;

And the parameters:

comm.Parameters.AddWithValue("@Image", “myImage”);
comm.Parameters.AddWithValue(“@ID”, 123); // or whatever the ID
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement