Sunday, 29 July 2012

3 tier insert,update,delete gridview using c#

BAL:

 public int insert()
    {
        try
        {
            SqlParameter[] p = new SqlParameter[3];
            // p[0]=new  SqlParameter("Id",id);
            p[0] = new SqlParameter("UserName", username);
            p[1] = new SqlParameter("Email", email);
            p[2] = new SqlParameter("Image", image);
            int result = DAL.executeNonQuery_sam("insselect", p);
            return result;
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
            return 0;
        }
    }
    public DataSet load()
    {
        string query = ("select * from dbo.tbl_select");
        DataSet ds = DAL.load(query);
        return ds;
    }
   
    public int update()
    {
        try
        {
            SqlParameter[] p = new SqlParameter[3];
            p[0] = new SqlParameter("@Id", id);
            p[1] = new SqlParameter("@UserName", username);
            p[2] = new SqlParameter("@Email", email);
            int result = DAL.executeNonQueryupdate("Updateselect", p);
            return result;
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
            return 0;
        }
    }
    
    public int delete()
    {
        SqlParameter[] p=new SqlParameter[1];
        p[0]=new SqlParameter("Id",id);

        int ds = DAL.executeNonQuery_sam("Delete_grid", p);
        return ds;
     }
   

}

DAL:

 static SqlConnection con;
    static SqlCommand cmd;
public static string connection = (ConfigurationManager.ConnectionStrings["constr"].ConnectionString);


    public static int executeNonQuery_sam(String CommandText, SqlParameter[] Parameters)
    {
        try
        {
            con = new SqlConnection(connection);
            cmd = new SqlCommand(CommandText, con);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter p in Parameters)
            {
                if (p.Value == null)
                {

                }
                cmd.Parameters.Add(p);
            }
            con.Open();
            return cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new ArgumentException(ex.Message);
        }

        finally
        {
            con.Close();
        }
    }
    public static DataSet load(String CommandText)
    {

        con = new SqlConnection(connection);
        cmd = new SqlCommand(CommandText, con);
        con.Open();
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter adb=new SqlDataAdapter(cmd);
        DataSet ds=new DataSet();
        adb.Fill(ds);
        con.Close();
        return ds;
    }
    
    
    public static int executeNonQueryupdate(String CommandText, SqlParameter[] Parameters)
    {
        try
        {
            con = new SqlConnection(connection);
            cmd = new SqlCommand(CommandText, con);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter p in Parameters)
            {
               
                cmd.Parameters.Add(p);
            }
            con.Open();
            return cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new ArgumentException(ex.Message);
        }

        finally
        {
            con.Close();
        }
    }
   
}
Default.aspx:

int id6;
    string name1, email2;
    BAL b = new BAL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {  
            load();
        }
    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
        try
        {

            b._Username = TextBox1.Text;
            b._Email = TextBox2.Text;
            int result = b.insert();
            load();
            Label1.Text = "save";

        }
        catch (Exception ex)
        {
            ex.Message.ToString();

        }
       
    }

    public void load()
    {
        DataSet ds = b.load();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        
         Button btn = sender as Button;
        TextBox id = (TextBox)btn.FindControl("TextBox4");
        TextBox name = (TextBox)btn.FindControl("TextBox5");
        TextBox email = (TextBox)btn.FindControl("TextBox6");
        b._Id = Convert.ToInt32(id.Text.ToString());
        b._Username = name.Text;
        b._Email = email.Text;
        int result = b.update();
        DataSet ds = new DataSet();
        GridView1.DataBind();
        GridView1.DataSource = ds;
      
        load();
    }

    protected void Button3_Click(object sender, EventArgs e)//sample update code 
    {
        int a = Convert.ToInt32(TextBox3.Text.ToString());
        b._Id = a;
       
        b._Username = TextBox1.Text;
        b._Email = TextBox2.Text;
        int result=b.update();
        load();

    }
    
    protected void btndelete_Click(object sender, EventArgs e)//gridview
    {
        Button btn = (Button)sender; //find control
        Label ltn = (Label)btn.FindControl("Label1");
        b._Id = Convert.ToInt32(ltn.Text.ToString());
        int result = b.delete();
        load();
    }
}


Design template:

<asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns="false">       
        <Columns>
       
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" runat="server" onclick="ChangecheckboxStateToTrue()" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("Id") %>' Visible="false"></asp:Label>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%#Eval("Id") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="UserName">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("UserName") %>' Visible="false"></asp:Label>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%#Eval("UserName") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%#Eval("Email") %>' Visible="false"></asp:Label>
                        <asp:TextBox ID="TextBox6" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Update">
                <ItemTemplate>
                    <asp:Button ID="btnupdate" runat="server" Text="update" 
                        onclick="btnupdate_Click"/>
                </ItemTemplate>
                </asp:TemplateField>
               <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btndelete" runat="server" onclick="btndelete_Click" 
                            Text="Delete" />
                    </ItemTemplate>
                </asp:TemplateField>
               
            </Columns>
        </asp:GridView>

No comments:

Post a Comment