Tuesday, 14 August 2012

URL Routing in ASP.net 4.0 with C#

Introduction:
ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users and also it helps for SEO Operation and Security Aspect

Step:1
First Step to start the Routing is to define the Route in Application_Start of Global.asax. Also include namespace System.Web.Routing.


Step:2









Download-Routing-in-RAR-132 KB

Thursday, 26 July 2012

Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List



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

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showgrid();
        }
    }
    public void showgrid()
    {
        using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
        {
            DataTable dt = new DataTable();
            sqlcon.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from employee ", sqlcon);
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        showgrid();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex;
        showgrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        Label lb = (Label)GridView1.Rows[e.RowIndex].FindControl("Label6");
        DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
        RadioButtonList rbl = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1");
        CheckBoxList chb = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("CheckBoxList2");
        TextBox tx1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        TextBox tx2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
        TextBox tx3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
        using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
        {
            sqlcon.Open();
            string sql = "update employee set emp_name='" + tx1.Text + "',emp_address='" + tx2.Text + "',salary='" +
                tx3.Text + "',department='" + ddl.SelectedValue.ToString() + "',maritalstatus='" +
                rbl.SelectedValue.ToString() + "',Active_status='" + chb.SelectedValue.ToString() + "' where emp_id='" +
                lb.Text + "'";
            using (SqlCommand cmd = new SqlCommand(sql))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sqlcon;
                cmd.ExecuteNonQuery();
                GridView1.EditIndex = -1;
                showgrid();
            }


        }

    }
    public DataTable load_department()
    {
        using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sd = new SqlDataAdapter("select DeparmentName,DeparmentId from department", sqlcon);
            sd.Fill(dt);
            return dt;

        }

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList dp = (DropDownList)e.Row.FindControl("ddlOutlet");
                DataTable dt = (DataTable)ViewState["OutletDrpDown"];
                dp.DataSource = dt;
                dp.DataTextField = "DeparmentName";
                dp.DataValueField = " DeparmentId";
                dp.DataBind();
  }
                RadioButtonList rbtnl = (RadioButtonList)e.Row.FindControl("RadioButtonList1");
                rbtnl.SelectedValue = drv[5].ToString();
                CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("CheckBoxList2");
                chkb.SelectedValue = drv[6].ToString();
}
}


            }

        }
    }
}


Output Parameter in stored Procedure(MYSQL) using C# in asp.net



DROP PROCEDURE IF EXISTS SP_OUTLETCREATION;
CREATE PROCEDURE SP_OUTLETCREATION(
IN SP_OutletId  smallint,
IN SP_OutletName varchar(20),
out SP_MSG varchar(50)
)
Begin
 IF NOT EXISTS(select 1 from outletcreation where OutLetName=SP_OutletName) then

if SP_OutletId=0 then
INSERT INTO OUTLETCREATION (OutletName,CreatedDate,ModifiedDate) values(SP_OutletName,CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP());
else
UPDATE OUTLETCREATION SET outletName=SP_OutletName,ModifiedDate=CURDATE() where OutletId=SP_OutletId;
end if;
else
set SP_MSG="OutletName is Already Exist!!!";

End if;
End;

C#:

protected void btnCreateOutlet_Click(object sender, EventArgs e)
    {
        try
        {

            using (MySqlConnection ObjCon = new MySqlConnection(new CommonClass().GetConnectionString()))
            {

                ObjCon.Open();
                using (MySqlCommand ObjCmd = new MySqlCommand("SP_OUTLETCREATION", ObjCon))
                {
                    ObjCmd.CommandType = CommandType.StoredProcedure;
                    ObjCmd.Parameters.Add("SP_OutletId", MySqlDbType.Int64).Value = 0;
                    ObjCmd.Parameters.Add("SP_OutletName", MySqlDbType.VarChar, 20).Value = txtOutletName.Text.Trim();
                    MySqlParameter parm2 = new MySqlParameter("SP_MSG", MySqlDbType.VarChar);
                    parm2.Size = 50;
                    parm2.Direction = ParameterDirection.Output;
                    ObjCmd.Parameters.Add(parm2);
                    ObjCmd.ExecuteNonQuery();
                    lblMsg.Text = ObjCmd.Parameters["SP_MSG"].Value.ToString();

                }

                Bind();
            }
        }

        catch (Exception ex)
        {

            lblMsg.Text=ex.Message;

        }

    }

Monday, 23 July 2012

DropDownList(Bind) Inside The Gridview Using Templatefield in Asp.net


  <asp:GridView ID="GVBeneficiary" runat="server" AutoGenerateColumns="False"
          CellPadding="4" ForeColor="#333333"
          onrowcancelingedit="GVBeneficiary_RowCancelingEdit"
          onrowediting="GVBeneficiary_RowEditing"
          onrowupdating="GVBeneficiary_RowUpdating"
          onrowdatabound="GVBeneficiary_RowDataBound">
         <AlternatingRowStyle BackColor="White" />
         <Columns>
             <asp:CommandField ShowEditButton="True" />
             <asp:TemplateField HeaderText="ID">
             <ItemTemplate>
             <asp:Label runat="server" ID="lblID" Text='<%#Bind("id") %>'  ></asp:Label>
             </ItemTemplate>
             </asp:TemplateField>
              <asp:TemplateField HeaderText="AccountNumber">
             <ItemTemplate>
             <asp:TextBox runat="server" ID="txtAccountNumber" Text='<%#Bind("AccountNo") %>' ></asp:TextBox>
             </ItemTemplate>
             </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
             <ItemTemplate>
             <asp:TextBox runat="server" ID="txtName" Text='<%#Bind("Name") %>' ></asp:TextBox>
             </ItemTemplate>
             </asp:TemplateField>
                 <asp:TemplateField HeaderText="PhoneNumber">
             <ItemTemplate>
             <asp:TextBox runat="server" ID="txtPhoneNumber" Text='<%#Bind("PhoneNumber") %>' ></asp:TextBox>
             </ItemTemplate>
             </asp:TemplateField>
           
                 <asp:TemplateField HeaderText="BankName">
             <ItemTemplate>
             <asp:DropDownList runat="server" ID="ddlBankNme">
                 <asp:ListItem Value="0">Select</asp:ListItem>
                 <asp:ListItem Value="1">State Bank of India</asp:ListItem>
                 <asp:ListItem Value="2">Indian Bank</asp:ListItem>
                 </asp:DropDownList>
             </ItemTemplate>
             </asp:TemplateField>
           <asp:TemplateField HeaderText="BranchNumber">
             <ItemTemplate>
             <asp:TextBox runat="server" ID="txtBranchNumber" Text='<%#Bind("BranchName") %>' ></asp:TextBox>
             </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField Visible="false" >
             <ItemTemplate>
             <asp:HiddenField runat="server" ID="txtBankName" Value='<%#Bind("BankName") %>'  />
         
             </ItemTemplate>
             </asp:TemplateField>
         </Columns>
         <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
         <HeaderStyle BackColor="Red" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
         <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
         <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
     </asp:GridView>



C#:

protected void GVBeneficiary_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HiddenField txthidden = ((HiddenField)e.Row.FindControl("txtBankName"));
        DropDownList ddlhidden = ((DropDownList)e.Row.FindControl("ddlBankNme"));
        ddlhidden.SelectedValue = txthidden.Value;
     
    }
}