Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday, 5 October 2013

Object Datasource in Asp.Net

--Add a Class
















--add code in that class

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;
using System.Configuration;

namespace WebApplication3
{
    public class employee
    {

         public string name { get; set; }
         public int age { get; set; }
         public string gender { get; set; }
         public string email { get; set; }
    
    }
    
    
    public class employeelayer
    {
        public List<employee> getallemployees()
        {
            List<employee> listemployees = new List<employee>();
            string cscon = ConfigurationManager.ConnectionStrings["ConnectionStringdb"].ConnectionString;

            using (SqlConnection con = new SqlConnection(cscon))
            {

                SqlCommand cmd = new SqlCommand("SearchEmployee", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {

                    employee emp = new employee();
                    emp.age = Convert.ToInt32(rdr["age"]);
                    emp.email = rdr["email"].ToString();
                    emp.name = rdr["email"].ToString();
                    emp.gender = rdr["gender"].ToString();

                    listemployees.Add(emp);

                }

                return listemployees;
            }
        
        
        }

    }



}

-------drag and drop object data source 























--Build if its not populating in dropdownlist







































































<asp:GridView ID="GridView2" runat="server" BackColor="#DEBA84" 
            BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            CellSpacing="2" AutoGenerateColumns="False" 
            DataSourceID="ObjectDataSource1">
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <Columns>
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
            <asp:BoundField DataField="gender" HeaderText="gender" 
                SortExpression="gender" />
            <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
        </Columns>
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="getallemployees" TypeName="WebApplication3.employeelayer">
        </asp:ObjectDataSource>



--Thats it we are done.

Run the application




























--back end stored procedure is as below


alter proc SearchEmployee-- '','','',''
@Name varchar(50)=null,
@Age int=null
as
begin

declare @sql varchar(max),@sqlwhere varchar(max)
  
set @sql='select * from employee' 

if ((@Name is not null) and @Name <> '')
begin
set @sqlwhere=' Name='+ '''' + @Name + ''''
print 'sqlwhere '
end

 if ((@Age is not null) and @Age <> '')
begin
 print 'i m in age block '
    set @sqlwhere =isnull(@sqlwhere + ' and ', '') + ' Age = ''' + cast(@Age as varchar(50)) + ''' ';
print 'sqlwhere ' + cast(@sqlwhere as varchar(50))
end



if (@sqlwhere is not null)
begin
set @sql=@sql+' where ' + @sqlwhere;
end
else
begin
set @sql=@sql;
end

print @sql
exec (@sql) 

end


--exec SearchEmployee
















---Thank you . Hope you learned it :)

No comments:

Post a Comment