Mad coder.
Mad coder.

Reputation: 2175

Uploading an image into database in C#

I created an application in ASP.NET C# and I want to upload an image into MySQL (Binary field) using ASP:FileUpload tool. I could code only the following and couldn't understand the rest. I Googled all the day and couldn't find any relevant. Any help!

ASPX file

<asp:FileUpload ID="FileUpload1" runat="server" />
br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

C# file

protected void Button1_Click(object sender, EventArgs e)
    {
        HttpPostedFile fup = FileUpload1.PostedFile;
        cmd = new OdbcCommand("INSERT into profile(picture) VALUES(?)", MyConnection);
        cmd.Parameters.Add("@picture", OdbcType.Binary) = fup;
        MyConnection.Open();
        cmd.ExecuteNonQuery();
        MyConnection.Close();
    }

Upvotes: 0

Views: 4152

Answers (1)

M4N
M4N

Reputation: 96551

It seems that these lines are wrong:

cmd = new OdbcCommand("INSERT into profile(picture) VALUES(?)", MyConnection);
cmd.Parameters.Add("@picture", OdbcType.Binary) = fup;

I guess this should be like this (or similar - can't test it right now):

cmd = new OdbcCommand("INSERT into profile(picture) VALUES(@picture)", MyConnection);
cmd.Parameters.Add("@picture", OdbcType.Binary).Value = FileUpload1.FileBytes;

Also, see this similar question.

Upvotes: 1

Related Questions