2016-04-05 23 views
0

リストビューの編集テンプレートでファイルアップローダーを使用して、リストビューを更新しています。 私はリストビューの更新を処理するためにアイテムの更新を使用しています。しかし、それはcmd.executenonquery()でエラーをスローしています。この点で誰も助けてくれますか?リストビューのテンプレート編集のファイルアップローダー

<EditItemTemplate> 
       <tr style="background-color:#008A8C;color: #FFFFFF;"> 
        <td> 
         <asp:Button ID="UpdateButton" runat="server" Text="Update" CommandName="Update"/> 
         <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" /> 
        </td> 

        <td> 
         <asp:Label ID="productidLabel1" runat="server" Text='<%# Eval("productid") %>' /> 
        </td> 
        <td> 
         <asp:TextBox ID="productnameTextBox" runat="server" Text='<%# Bind("productname") %>' /> 
        </td> 
        <td> 
         <asp:TextBox ID="productdescTextBox" runat="server" Text='<%# Bind("productdesc") %>' /> 
        </td> 
        <td> 

         <asp:TextBox runat="server" ID="productpriceTextBox" Text='<%# Bind("productprice") %>'> </asp:TextBox> 

        </td> 
        <td> 
         <asp:DropDownList SelectedValue='<%# Bind("productcateg") %>' runat="server" ID="dropdownlist" style="overflow:auto" CssClass="dropdown"> 
           <asp:ListItem Enabled="true" Text="Select Category" Value="-1"></asp:ListItem> 
           <asp:ListItem Text="Watch" Value="Watch"> </asp:ListItem> 
           <asp:ListItem Text="Hand Bags" Value="handbag"></asp:ListItem> 
           <asp:ListItem Text="Television" Value="television"></asp:ListItem> 
           <asp:ListItem Text="Books" Value="book"></asp:ListItem> 
           <asp:ListItem Text="Accessories" Value="accessories"></asp:ListItem> 
           <asp:ListItem Text="Cars" Value="car"></asp:ListItem> 
           <asp:ListItem Value="bike"></asp:ListItem> 
           <asp:ListItem Text="Bikes" value="shoe"></asp:ListItem> 
           <asp:ListItem Text="Shoes" Value="garment"> </asp:ListItem> 
           <asp:ListItem Text="Garments" Value="cellphone"></asp:ListItem> 
           <asp:ListItem Text="Laptops" Value="laptop"></asp:ListItem> 
           <asp:ListItem Text="Home & Appliances" Value="homeappliance"></asp:ListItem> 
           <asp:ListItem Text="Perfumes" Value="perfume"></asp:ListItem> 
           <asp:ListItem Text="Sports" Value="sports"></asp:ListItem> 
    </asp:DropDownList> 
        </td> 
        <td> 
         <asp:FileUpload ID="FileUpload2" runat="server" /> 
         <asp:Button runat="server" ID="btnupload" OnClick="btnupload_Click" Text="Upload" /> 
        </td> 
       </tr> 
      </EditItemTemplate> 







protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e) 
    { 
     DropDownList dropdownlist = ListView1.Items[e.ItemIndex].FindControl("dropdownlist") as DropDownList; 
     string category = dropdownlist.SelectedValue; 
     FileUpload fileUpload1 = ListView1.Items[e.ItemIndex].FindControl("FileUpload2") as FileUpload; 

     System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(fileUpload1.FileContent); 
     System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 200); 

     var stream = new System.IO.MemoryStream(); 
     objImage.Save(stream, ImageFormat.Png); 
     stream.Position = 0; 

     BinaryReader br = new BinaryReader(stream); 
     byte[] imagebytes = br.ReadBytes((Int32)stream.Length); 
     string base64String = Convert.ToBase64String(imagebytes, 0, imagebytes.Length); 
     string url = "data:image/png;base64," + base64String; 


     TextBox productname = ListView1.Items[e.ItemIndex].FindControl("productnameTextBox") as TextBox; 
     TextBox productprice = ListView1.Items[e.ItemIndex].FindControl("productpriceTextBox") as TextBox; 
     TextBox productdesc = ListView1.Items[e.ItemIndex].FindControl("productdescTextBox") as TextBox; 
     Label productid = ListView1.Items[e.ItemIndex].FindControl("productidLabel1") as Label; 


     con.Open(); 
     string query = "UPDATE products SET productname=" + productname.Text + "," + "productdesc=" + productdesc.Text + "," + "productprice=" + productprice.Text + "," + "productcateg=" + category + "," + "productimage=" + url + "WHERE productid=" + productid.Text; 
     cmd = new SqlCommand("UPDATE products SET productname =" + productname.Text + "," + "productdesc =" + productdesc.Text + "," + "productprice =" + productprice.Text + "," + "productcateg =" + category + "," + "productimage =" + url + "WHERE productid =" + productid.Text,con); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 

    } 
    public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight) //Image Resize 
    { 
     var ratio = (double)maxHeight/image.Height; 

     var newWidth = (int)(image.Width * ratio); 
     var newHeight = (int)(image.Height * ratio); 

     var newImage = new Bitmap(newWidth, newHeight); 
     using (var g = Graphics.FromImage(newImage)) 
     { 
      g.DrawImage(image, 0, 0, newWidth, newHeight); 
     } 
     return newImage; 
    } 

答えて

0

OK私は、リストビューに反映されるだけでe.NewValues.Add(「ColumnNameに」、URL)と更新を追加する方法を見出した

関連する問題