简体   繁体   中英

File Uploader in Edit Template in List View

I am using File uploader in edit template in list view to update the list view. I am using on item updating to handle the update of list view. However it is throwing error at cmd.executenonquery(). Can anyone help me in this regard.

 <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;
    }

好的,我发现了仅添加e.NewValues.Add(“ columnname”,url)和更新的方式就会反映在列表视图中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM