无法从数据库保存图片 [英] Cannot save Picture from database

查看:115
本文介绍了无法从数据库保存图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,程序员在这里遇到了一些问题.如您所见,我想将图片保存在数据库中,但是我无法导致使用断点,这表明我的文件上传没有图片,但是当我检查它时,我已经选择了图片.因此,它只是在我的if语句上循环? Updatepanel是这里的问题吗?
我的代码
客户端

Hello programmers got a few problem here.. As you see I want to save picture on my database but I cannot cause using breakpoints it show that my fileupload doesnt have an image,but when I check it I already choose picture. So it just loop on my if statement? Is an Updatepanel an issue here?
My code
client side

<asp:Panel ID="PanelSelling" Visible ="false" runat="server" Height="850px" Width="700px" >
               <br />
   <br />
   <br />
        
   <asp:Label ID="Label13" runat="server" Text="Browse Picture:" Width="212px"></asp:Label>
   <asp:FileUpload ID="imgUpload" runat="server" /><br />
   <br />
         <asp:Label ID="Label14" runat="server" Text="Add Title:"
       Width="180px"></asp:Label>
   <asp:TextBox ID="txtTitle" runat="server"  Font-Names="verdana"
                       Font-Size="X-Small" Width="300px"></asp:TextBox><br />
       <br />
         <asp:Label ID="Label15" runat="server" Text="Description:"
       Width="180px"></asp:Label><br />
   <br />
                                                                                    
   <asp:TextBox ID="txtDescription" runat="server" Height="49px" TextMode="MultiLine"  Font-Names="verdana"
                       Font-Size="X-Small" Width="300px"></asp:TextBox><br />
   <br />
        
   <asp:Label ID="Label16" runat="server" Text="Price" Width="180px"></asp:Label>
   <asp:TextBox ID="txtPrice" runat="server" Font-Names="verdana"
                       Font-Size="X-Small" Width="300px"></asp:TextBox><br />
   <br />
        
   <asp:Label ID="Label17" runat="server" Text="Quantity" Width="180px"></asp:Label>
   <asp:TextBox ID="txtQuantity" runat="server" Font-Names="verdana"
                       Font-Size="X-Small" Width="300px"></asp:TextBox><br />
   <br />
          
   <asp:Label ID="Label18" runat="server" Text="Category" Width="180px"></asp:Label>
   <asp:DropDownList ID="drpdwnCategory" runat="server" CssClass="p1"
                       Font-Names="Verdana" Font-Size="XX-Small" Height="16px" Width="303px">
                   </asp:DropDownList>
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
                           
   <asp:Button ID="btnSelltheProduct" runat="server" Text="Sell Product" Width="200px" CssClass="button2" />
       
   <br />
          
   <br />
   <br />
               </asp:Panel>


它在更新面板中
在我的服务器端,这是我的代码


it is inside a update panel
and on my server side here is my code

Dim userID As String = Session("UserID")
        'get data from userID
        _userDAL = New UserDAL
        Dim user As New User
        user = _userDAL.SelectByUserID(userID)
        If user IsNot Nothing Then
            Dim account As String = user.AccountNo
            Dim stringmode As String = 1
            Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ECommerceConnectionString").ConnectionString)
            cn.Open()
            'If imgUpload.PostedFile IsNot Nothing AndAlso imgUpload.PostedFile.FileName <> "" Then
            '    Dim imageSize As Byte() = New Byte(imgUpload.PostedFile.ContentLength - 1) {}
            '    Dim uploadedImage__1 As HttpPostedFile = imgUpload.PostedFile
            '    uploadedImage__1.InputStream.Read(imageSize, 0, CInt(imgUpload.PostedFile.ContentLength))


            'End If
            Try
                Dim img As FileUpload = DirectCast(FindControl("imgUpload"), FileUpload)
                Dim imgByte As [Byte]() = Nothing
                If img.HasFile AndAlso img.PostedFile IsNot Nothing Then
                    Dim File As HttpPostedFile = imgUpload.PostedFile
                    imgByte = New [Byte](File.ContentLength - 1) {}
                    File.InputStream.Read(imgByte, 0, File.ContentLength)
                End If
                Dim sql As String = "INSERT INTO [Selling](Image,Title,Description,UserID,Quantity,AccountNo,Price,CategoryID,Mode) VALUES(@Image,@Title,@Description,@UserID,@Quantity,@AccountNo,@Price,@CategoryID,@Mode)"
                Dim cmd As SqlCommand = New SqlCommand(sql, cn)
                cmd.Parameters.AddWithValue("@Image", imgByte)
                cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 120).Value = txtTitle.Text
                cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 500).Value = txtDescription.Text
                cmd.Parameters.Add("@UserID", SqlDbType.NVarChar, 12).Value = userID
                cmd.Parameters.Add("@Quantity", SqlDbType.NVarChar, 12).Value = txtQuantity.Text
                cmd.Parameters.Add("@AccountNo", SqlDbType.NVarChar, 12).Value = account
                cmd.Parameters.Add("@Price", SqlDbType.NVarChar, 12).Value = txtPrice.Text
                cmd.Parameters.Add("@CategoryID", SqlDbType.NVarChar, 12).Value = drpdwnCategory.SelectedValue
                cmd.Parameters.Add("@Mode", SqlDbType.NVarChar, 12).Value = stringmode
                cmd.ExecuteNonQuery()
                SetFocus(MsgBox("Saved"))
                InitiateClear()

                'SetFocus(MsgBox("Error Occured while saving"))
            Finally
                cn.Close()
            End Try
        End If



基本上我是在打开评论,但仍然无法正常工作.有任何想法吗 ?对不起,我的英语

我的问题是先生,我无法使用该代码将图片保存到数据库中.关于我的代码是否存在任何问题?



Basically I am switching on the comment but still it doesnt work. Any Ideas ? Sorry for my english

My question is sir I cant save picture to my data base using that code.. Is there any problem regarding my codes?

推荐答案

UpdatePanel与FileUpload控件不兼容.有一些解决方法或替代方法,例如

http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AsyncFileUpload/AsyncFileUpload.aspx [ ^ ]

简单的AJAX文件上传 [
The UpdatePanel is not compatible with FileUpload controls. There are some work-arounds or alternatives such as these

http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AsyncFileUpload/AsyncFileUpload.aspx[^]

Simple AJAX File Upload[^]


这篇关于无法从数据库保存图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆