Search This Blog

Wednesday, March 21, 2012

Confirm on Delete in Gridview using Javascript ?

When You Press on Delete Button in the Gridview At that time confimation is there For Delete Particular Row from the Database .


Press Delete

At that time confirmation block will be generated.

On aspx Page Write Javascript as shown Below.

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function onconfirm() {
        return confirm('Are You Sure ???')
        }
    </script>
</head>

And In the Body Part code inside the gridview :

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
Width="754px" AutoGenerateColumns="False" DataKeyNames="country_id" onrowcancelingedit="GridView1_RowCancelingEdit" onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting"  onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"  onsorting="GridView1_Sorting">
   <Columns>
   </asp:TemplateField>                                                                                                       
<ItemTemplate>
   <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return onconfirm();" CommandArgument='<%# Bind("country_id") %>' CommandName="Delete">Delete</asp:LinkButton>
 </ItemTemplate>
   </asp:TemplateField>                                                               
  </Columns>                                                          
 </asp:GridView>

IN aspx.cs File

DAL_Country d1 = new DAL_Country();
BAL_Country b1 = new BAL_Country();
 public void ShowData()
    {
        DataSet ds = new DataSet();
        ds = b1.SelectData();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        b1.DeleteData(id);
        ShowData();
    }
public DataSet SelectData()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tbl_country",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
BAL_Country.cs File
    public void DeleteData(int id)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from tbl_country where country_id='"+id+"'",con);
        cmd.ExecuteNonQuery();
        con.Close();
    }

Thursday, March 15, 2012

Upload Product Information



Aspx File Source Code
<table class="style1">
                                                <tr>
                                                    <td class="style2">
                                                        Product Category
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:DropDownList ID="Ddl_Category" runat="server" AutoPostBack="True" ForeColor="#009933"
                                                            OnSelectedIndexChanged="Ddl_Category_SelectedIndexChanged">
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product SubCategory
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:DropDownList ID="Ddl_SubCategory" runat="server" AutoPostBack="True" ForeColor="#009933">
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Name
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductName" runat="server" Font-Bold="True" ForeColor="#009933"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Price
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductPrice" runat="server" Font-Bold="True" ForeColor="#009933"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Image
                                                    </td>
                                                    <td class="style3">
                                                        &nbsp;
                                                    </td>
                                                    <td>
                                                        <asp:FileUpload ID="FileUpload1" runat="server" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Color
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:DropDownList ID="Ddl_Color" runat="server" AutoPostBack="True" ForeColor="#009900">
                                                            <asp:ListItem Value="0">--select color--</asp:ListItem>
                                                            <asp:ListItem Value="White">White</asp:ListItem>
                                                            <asp:ListItem>Red</asp:ListItem>
                                                            <asp:ListItem>Green</asp:ListItem>
                                                            <asp:ListItem>Yellow</asp:ListItem>
                                                            <asp:ListItem>Black</asp:ListItem>
                                                            <asp:ListItem>Blue</asp:ListItem>
                                                            <asp:ListItem>Gray</asp:ListItem>
                                                            <asp:ListItem>Orange</asp:ListItem>
                                                            <asp:ListItem>Pink</asp:ListItem>
                                                        </asp:DropDownList>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Features
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductFeatures" runat="server" Font-Bold="True" ForeColor="#009933"
                                                            TextMode="MultiLine" Height="46px" Width="250px"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Specification
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductSpecification" runat="server" Font-Bold="True" ForeColor="#009933"
                                                            TextMode="MultiLine" Height="45px" Width="250px"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Product Description
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductDescription" runat="server" Font-Bold="True" ForeColor="#009933"
                                                            TextMode="MultiLine" Height="46px" Width="250px"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style5">
                                                        Product Warranty
                                                    </td>
                                                    <td class="style6">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td class="style7">
                                                        <asp:TextBox ID="Txt_ProductWarranty" runat="server" Font-Bold="True" ForeColor="#009933"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        Available Quantity
                                                    </td>
                                                    <td class="style3">
                                                        <strong>:-</strong>
                                                    </td>
                                                    <td>
                                                        <asp:TextBox ID="Txt_ProductQuantity" runat="server" Font-Bold="True" ForeColor="#009933"></asp:TextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="style2">
                                                        &nbsp;
                                                    </td>
                                                    <td class="style3">
                                                        &nbsp;
                                                    </td>
                                                    <td>
                                                        <asp:Button ID="Btn_Upload" runat="server" Text="Upload" OnClick="Btn_Upload_Click" />
                                                    </td>
                                                </tr>
                                            </table>


In aspx .cs File the Code To upload all the Information of Product

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

public partial class ProductUpload : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=TOPS21;Initial Catalog=MyExample;Integrated Security=True;Pooling=False");
    static string path = "";
    static string filename = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
        if (!IsPostBack)
        {
            Fill_DdlCategory();
        }
    }
    public void Fill_DdlCategory()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tbl_category",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Ddl_Category.DataSource = ds;
        Ddl_Category.DataValueField = "category_id";
        Ddl_Category.DataTextField = "category_name";
        Ddl_Category.DataBind();
        Ddl_Category.Items.Insert(0,new ListItem("--select category--","0"));
        Ddl_SubCategory.Items.Insert(0, new ListItem("--select subcategory--", "0"));
    }
    protected void Ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from tbl_subcategory where category_id='"+Ddl_Category.SelectedValue+"'", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Ddl_SubCategory.DataSource = ds;
        Ddl_SubCategory.DataValueField = "subcategory_id";
        Ddl_SubCategory.DataTextField = "subcategory_name";
        Ddl_SubCategory.DataBind();
        Ddl_SubCategory.Items.Insert(0, new ListItem("--select subcategory--", "0"));   
    }
    protected void Btn_Upload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            filename = FileUpload1.FileName;
            path = "~//images//" + filename;
            FileUpload1.SaveAs(Server.MapPath(path));
            SqlCommand cmd = new SqlCommand("insert into tbl_product values('" + Ddl_Category.SelectedValue + "','" + Ddl_SubCategory.SelectedValue + "','" + Txt_ProductName.Text + "','" + Txt_ProductPrice.Text + "','" + path + "','" + Ddl_Color.SelectedValue + "','" + Txt_ProductFeatures.Text + "','" + Txt_ProductSpecification.Text + "','" + Txt_ProductDescription.Text + "','" + Txt_ProductWarranty.Text + "','" + Txt_ProductQuantity.Text + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Text = "Product Uploaded...";
        }
        else
        {
            lblmsg.ForeColor = System.Drawing.Color.Red;
            lblmsg.Font.Bold.Equals(true);
            lblmsg.Text = "Please select the Product Image First.";
        }
    }
}

Output is

For that Make table in the database