Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems adding an Image to SQL Database in ASP.NET
    text
    copied!<p>I have been working on that for a while now. I am trying to add an Image to an SQL Database. I know it is not the best way but my boss really wants it that way. The table in which I will add the image has and Image column of type <strong>Image</strong>.</p> <p>This is the stored procedure I am using to add it:</p> <pre><code>ALTER PROCEDURE pr_INSRT_Img (@Img image, @name varchar(50)) AS BEGIN UPDATE usr SET Img= @Img WHERE disp_nm = @name END </code></pre> <p>My application is made in ASP.NET 4.0. The it works is that the user chooses and Image with an AJAX AsyncFileUpload and on the UploadComplete function of that control, it calls the StoredProcedure to do the <code>INSERT</code>. The ASynchFileUPload is in an accordion that is in an UpdatePanel that is in a gridview.</p> <p><strong>UploadComplete:</strong></p> <pre><code>protected void OnUpdateComplete(object sender, EventArgs e) { Image ImgUser = new Image(); AsyncFileUpload asyncSender = new AsyncFileUpload(); UpdatePanel pnlinfo = new UpdatePanel(); AsyncFileUpload asyncGv = new AsyncFileUpload(); Accordion accordion = new Accordion(); Label lblName = new Label(); //finding the row of the sender asyncSender = (AsyncFileUpload)sender; foreach (GridViewRow Row in gvData.Rows) { accordion = (Accordion)Row.Cells[0].FindControl("Accordion"); asyncGv = (AsyncFileUpload)accordion.FindControl("AsyncFileUpload"); if (asyncSender.ClientID == asyncGv.ClientID) { ImgUser = (Image)accordion.FindControl("imgUser"); ImgUser.ImageUrl = asyncGv.FileName; lblName = (Label)Row.Cells[0].FindControl("lblPnlName"); bool IsWorking = InsertImage(asyncGv.FileBytes, lblName.Text); pnlinfo = (UpdatePanel)Row.Cells[0].FindControl("pnlInfo"); pnlinfo.Update(); break; } } </code></pre> <p><strong>InsertImage:</strong></p> <pre><code> //pr_INSRT_Img SqlCommand cmd = new SqlCommand(ConfigManager.InsertImage); cmd.CommandType = CommandType.StoredProcedure; //set the parameters cmd.Parameters.Add("@Img", SqlDbType.Image).Value = ImgData; cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = Name; int ifWorks = DBUtils.ExecuteCmdScalar(cmd, ConfigManager.PhonebookSQLConnectionString); if (ifWorks != 0) return true; else return false; </code></pre> <p><strong>ASPX portion of the code where the ASychFileUpload is:</strong></p> <pre><code>&lt;div runat="server" class="divRow" style="text-align:center; width:300px; float:left;"&gt; &lt;asp:Accordion ID="Accordion" runat="server" FadeTransitions="true" FramesPerSecond="40" TransitionDuration="250" AutoSize="None" SelectedIndex="-1" RequireOpenedPane="false" SuppressHeaderPostbacks="true" Height="50px" Width="360px"&gt; &lt;Panes&gt; &lt;asp:AccordionPane ID="AccordionPane" runat="server"&gt; &lt;Header&gt; &lt;asp:Image ID="imgUser" runat="server" ImageAlign="Middle" ImageUrl="~/silhouette.jpg" Width="100px" Height="100px" EnableViewState="true"/&gt; &lt;/Header&gt; &lt;Content&gt; &lt;asp:AsyncFileUpload ID="AsyncFileUpload" runat="server" OnUploadedComplete="OnUpdateComplete" /&gt; &lt;/Content&gt; &lt;/asp:AccordionPane&gt; &lt;/Panes&gt; &lt;/asp:Accordion&gt; &lt;asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Visible="false" /&gt; &lt;/div&gt;&lt;br /&gt;&lt;br /&gt; </code></pre> <p>My problem is that when I upload the image, the <strong>ExecuteScalar()</strong> returns a 0 which means that the Image was not inserted in the Table. I've tried putting a <strong>varbinary(max)</strong> Field instead of an Image type but it did not work. All I can find on the Net tells me to use that code, or variant of that code (other ways to get the byte array, I just took the simplest). </p> <p>Overall, why is the <code>UPDATE</code> failing and how can I make it work.</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload