How to delete records from the data list in asp.net c#.

In this article, I will explain how to delete the particular image from the data list in asp.net c#.

Data Base (Sql Server)
Design:-



Script:-
USE [uploaddata]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tab](
          [Id] [int] IDENTITY(1,1) NOT NULL,
          [ImageName] [nvarchar](max) NULL,
         
 CONSTRAINT [PK_tab] PRIMARY KEY CLUSTERED
(
          [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
  
Source Code:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist.aspx.cs" Inherits="datalist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
                <tr>
                <td align="right"> Image Upload </td>
                    <td>
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>
                        &nbsp;</td>
                    <td>
                        <asp:Button ID="btnupload" runat="server" Text="Upload"
                            OnClick="btnupload_Click" />
                        </td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
            <asp:DataList ID="dlImages" runat="server" RepeatColumns="4" CellPadding="4"
                            style="font-family: 'Times New Roman', Times, serif" DataKeyField="Id"
                            ondeletecommand="dlImages_DeleteCommand">
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" ImageUrl='<%# Bind("ImageName", "~/img/{0}") %>'
                        Width="182" Height="284" runat="server" />
                        <br />
                         <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete selected image')"></asp:LinkButton>             
                    </ItemTemplate>
                     <ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
                    VerticalAlign="Bottom" />
            </asp:DataList>
               
                        </td>
                </tr>
            </table>
    </div>
    </form>
</body>
</html>


C#:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Data;

public partial class datalist : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=NEERAJ-PC; uid=sa; Pwd=12345678;database=uploaddata");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataList();
        }
    }
    protected void BindDataList()
    {
        con.Open();
        SqlCommand command = new SqlCommand("SELECT Id,ImageName from tab", con);
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dlImages.DataSource = dt;
        dlImages.DataBind();
        con.Close();
    }
    protected void btnupload_Click(object sender, EventArgs e)
    {
        try
        {
            string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.SaveAs(Server.MapPath("img/" + filename));
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert into tab(ImageName) values(@ImageName)", con);


            cmd.Parameters.AddWithValue("@ImageName", filename);
            cmd.ExecuteNonQuery();
            con.Close();
            BindDataList();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Please select the image !!')</script>");
        }

    }  
    protected void dlImages_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int imgId = Convert.ToInt32(dlImages.DataKeys[e.Item.ItemIndex]);

        SqlCommand cm = new SqlCommand("delete from tab where Id=@ID", con);
        cm.Parameters.AddWithValue("@ID",imgId);
        con.Open();
        cm.ExecuteNonQuery();
        con.Close();
        BindDataList();

    }
}

Output:-



How to delete records from the data list in asp.net c#. How to delete records from the data list in asp.net c#. Reviewed by NEERAJ SRIVASTAVA on 9:21:00 PM Rating: 5

No comments:

Powered by Blogger.