Monday 27 March 2017

insert and bind data in gridview using 3 tier architecture in dotnet

3tier architecture example to insert and bind data in gridview
table 







Presentation(User Interface)

Source Code for above design
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <div class="auto-style2">
            <strong>3Tier Example</strong></div>
        <table class="auto-style1">
            <tr>
                <td>Name</td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>City</td>
                <td>
                    <asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Email</td>
                <td>
                    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <asp:Button ID="btnsave" runat="server" OnClick="btnsave_Click" Text="save" />
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    
                </td>
            </tr>
        </table>
    
    </div>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>

</html>


DAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class Class1
    {
    }
    public class ClsDataLayer
    {
        SqlConnection con = new SqlConnection("data source=ravi-PC;database='3tierexample';integrated security='true';");
        public void InsertData(string _name,String _city,String _email)
        {
            SqlDataAdapter adp = new SqlDataAdapter("insert into Usermst values('"+_name+ "','" + _city + "','" + _email + "')",con);
            DataTable dt = new DataTable();
            adp.Fill(dt);

        }
        public object SelectData()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select *from Usermst", con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            return dt;
        }
    }

}

BLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using BLL;

namespace BLL
{
    
    public class Class1
    {
    }
    public class CLSBussLayer
    {
        ClsDataLayer objDAL = new ClsDataLayer();
        public void InsertUser(String _name,String _city,string _email)
        {
            objDAL.InsertData(_name, _city, _email);
        }
        public object SelectUser()
        {
            return objDAL.SelectData();
        }
    }
}

Code for Button_Click

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnsave_Click(object sender, EventArgs e)
    {
        CLSBussLayer objBLL = new CLSBussLayer();
        objBLL.InsertUser(txtname.Text, txtcity.Text, txtemail.Text);
        GridView1.DataSource = objBLL.SelectUser();
        GridView1.DataBind();
    }
}



i will give you clear explanation
follow step by step

Tuesday 7 March 2017

code for gridview insert update and delete

gridview insert update and delete code 





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

<!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>
    
        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
            AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="id" 
            onrowcancelingedit="gv_RowCancelingEdit" onrowdeleting="gv_RowDeleting" 
            onrowediting="gv_RowEditing" onrowupdating="gv_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="productname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("productname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("productname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="productprice">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("productprice") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("productprice") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="insert" />
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox4" runat="server" Width="73px"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox5" runat="server" Width="73px"></asp:TextBox>
    </form>
</body>
</html>


Code

Insert update and delete in gridview
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 gridview : System.Web.UI.Page
{
    string constr = "data source=ravi;database=ravi;integrated security=yes";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
        }
    }
    private void binddata()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(constr))
        {
            
            SqlDataAdapter adp = new SqlDataAdapter("select *from service", con);
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                gv.DataSource = dt;
                gv.DataBind();
            }
        }
    }

    protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv.EditIndex = -1;
        binddata();

    }

    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id=int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
        deleteservice(id);
        binddata();
    }

    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv.EditIndex = e.NewEditIndex;
    }

    protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
        TextBox textbox2 = (TextBox)gv.Rows[e.RowIndex].FindControl("textbox2");
        TextBox textbox3 = (TextBox)gv.Rows[e.RowIndex].FindControl("textbox3");
        updateservice(id,textbox2.Text,int.Parse(textbox3.Text));
        gv.EditIndex=-1;
        binddata();
     
    }
    private void updateservice(int id,string productname,int productprice)
    {
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query="update service set productname='"+productname+"',productprice="+productprice+" where id="+id+"";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            cmd.ExecuteNonQuery();
        }

    }
    private void deleteservice(int id)
    {
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "delete from service where id=" + id + "";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            cmd.ExecuteNonQuery();
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string productname = TextBox4.Text;
        decimal productprice =decimal.Parse(TextBox5.Text);

        using (SqlConnection con = new SqlConnection(constr))
        {
            string query="insert into service values('"+TextBox4.Text+"',"+TextBox5.Text+")";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            cmd.ExecuteNonQuery();
            gv.DataBind();
            con.Close();
            binddata();
        }
    }
}

Thursday 2 March 2017

palindrome string programme in c#

Palindrome string program in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication78
{
    class Program
    {
        static void Main(string[] args)
        {
            string Str, Revstr = "";
            int i;
            Console.Write("Enter A String : ");
            Str = Console.ReadLine();

            for (i = Str.Length - 1; i >= 0; i--)
            {

                Revstr = Revstr + Str[i];
            }
            if (Revstr == Str)
            {

                Console.WriteLine("entered String  Is palindrome");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("entered  String  Is not palindrome ");
                Console.ReadLine();
            }
        }
    }
}
Output screenshot: