Wednesday, May 16, 2012

Deletion of multiple records in GridView using Checkbox in ASP.NET.




deletion of record in gridview using checkbox(multiple row deletion)in aspx.cs page:-

-------------------------------------------------------------------------------------------------------
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.Data;
using System.Collections.Specialized;
public partial class _Default : System.Web.UI.Page
{
   
    SqlCommand cmd;
    SqlDataAdapter da;
    SqlConnection con = new SqlConnection("data source=RAJAT\\SQLEXPRESS;initial catalog= master;integrated security=sspi;");
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }



    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        StringCollection idCollection = new StringCollection();
        string strID = string.Empty;

        for (int i = 0; i < Gridview1.Rows.Count; i++)
        {
            CheckBox Checkbox1 = (CheckBox)
               Gridview1.Rows[i].Cells[0].FindControl("Checkbox1");
            if (Checkbox1 != null)
            {
                if (Checkbox1.Checked)
                {
                    strID = Gridview1.Rows[i].Cells[1].Text;
                    idCollection.Add(strID);
                }
            }
        }
        DeleteMultipleRecords(idCollection);
        Gridview1.DataBind();

    }
    private void DeleteMultipleRecords(StringCollection idCollection)
    {
        //Create sql Connection and Sql Command
        //con.Open();
        SqlCommand cmd = new SqlCommand();
        string IDs = "";

        foreach (string id in idCollection)
        {
            IDs += id.ToString() + ",";
        }
        try
        {
            string strIDs =
             IDs.Substring(0, IDs.LastIndexOf(","));
            foreach (string id in idCollection)
            {
                string strSql = ("Delete from aparna  WHERE id in ('" + id + "')");
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSql;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        catch (SqlException ex)
        {
            string errorMsg = "Error in Deletion";
            errorMsg += ex.Message;
            throw new Exception(errorMsg);
        }
        finally
        {
            con.Close();
        }
    }
}

code in .aspx page :-

---------------------------------------

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>

<asp:GridView ID="Gridview1" runat ="server" AutoGenerateColumns ="false"
        DataSourceID ="sqldatasource1" ShowFooter ="true"
        onselectedindexchanged="Gridview1_SelectedIndexChanged">
<Columns >
<asp:TemplateField >
<ItemTemplate >
<asp:CheckBox ID="Checkbox1" runat ="server" />
</ItemTemplate>
<FooterTemplate >
<asp:Button ID="Button1" runat ="server" Text ="DELETE" OnClick="Button1_Click" OnClientClick ="return DeleteConfirmation();" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField ="id" HeaderText ="ID" />
<asp:BoundField DataField ="name" HeaderText ="NAME" />
<asp:BoundField DataField ="location" HeaderText ="LOCATION" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID ="sqldatasource1" runat ="server" ConnectionString ="Data Source=RAJAT\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" SelectCommand ="select [id],[name],[location] from [aparna]">
</asp:SqlDataSource>
</div>
    </asp:Content>




code in master page(site master page);-

-----------------------------------------------------------------------

<head runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
    <script type="text/javascript" language="javascript">
function DeleteConfirmation()
{
if (confirm("Are you sure you want to delete selected records ?")==true)
   return true;
else
   return false;
}
</script>

</head>

How to convert a Number into words in C#


Convert Number To Words in ASP.Net
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
decimal iAmt = Convert.ToDecimal(Label1.Text);
string strAmount = wordify(iAmt);
Label2.Text = strAmount;
}
static string wordify(decimal v)
{
if (v == 0) return "zero";
var units = "|One|Two|Three|Four|Five|Six|Seven|Eight|Nine".Split('|');
var teens = "|eleven|twelve|thir#|four#|fif#|six#|seven#|eigh#|nine#".Replace("#", "teen").Split('|');
var tens = "|Ten|Twenty|Thirty|Forty|Fifty|Sixty|Seventy|Eighty|Ninety".Split('|');
var thou = "|Thousand|m#|b#|tr#|quadr#|quint#|sex#|sept#|oct#".Replace("#", "illion").Split('|');
var g = (v < style="color: rgb(163, 21, 21);">"minus " : "";
var w = "";
var p = 0;
v = Math.Abs(v);
while (v > 0)
{
int b = (int)(v % 1000);
if (b > 0)
{
var h = (b / 100);
var t = (b - h * 100) / 10;
var u = (b - h * 100 - t * 10);
var s = ((h > 0) ? units[h] + " Hundred" + ((t > 0 | u > 0) ? " and " : "") : "") + ((t > 0) ? (t == 1 && u > 0) ? teens[u] : tens[t] + ((u > 0) ? "-" : "") : "") + ((t != 1) ? units[u] : ""); s = (((v > 1000) && (h == 0) && (p == 0)) ? " and " : (v > 1000) ? ", " : "") + s; w = s + " " + thou[p] + w;
} v = v / 1000; p++;
}
return g + w ;
}
}
OutPut :
Number : 98563 

Number in Words: Ninety-Eight Thousand, Five Hundred and Sixty-Three