Sunday, 25 January 2015

How to bind and implement search gridview records in asp.net | Use of If Else If in Sql server||Search into Textbox and Bind Gridview

How to bind and implement search gridview records in asp.net | Use of If Else If in Sql server



  • In the web.config file create the connection string to connect the asp.net web application with the Sql server database as:
·         <connectionStrings>
·             <add name="dataConfig" connectionString="Data Source=SKYWORLD-PC\SQLEXPRESS;Initial Catalog=registerdata;Integrated Security=True"/>
·           </connectionStrings>
Note: Replace the Data source and Initial Catalog (i.e. Database Name) as per your application.

Source Code:
  • In the design page(.aspx) design the page as:


<div align="Center">
        <br />
        <br /><br />
            <fieldset style="width:415px;">
    <legend>Bind and Search records example in gridview</legend> 
        <asp:DropDownList ID="ddlsearchby" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlsearchby_SelectedIndexChanged">

            <asp:ListItem>All</asp:ListItem>
            <asp:ListItem>EmpName</asp:ListItem>
            <asp:ListItem>Salary</asp:ListItem>
            <asp:ListItem>City</asp:ListItem>
        </asp:DropDownList> &nbsp;&nbsp;
        <asp:TextBox ID="txtuser" runat="server" ></asp:TextBox>&nbsp;&nbsp;
                <asp:Button ID="btnsearch" runat="server" Text="Search" OnClick="btnsearch_Click"  />


                 <br />
        <br /><br />
        <asp:GridView ID="grddatalist" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
            <Columns>
                <asp:TemplateField HeaderText="EmpId">
                    <ItemTemplate>
                        <asp:Label ID="lblempid" runat="server" Text='<%#Bind("Empid") %>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblempname" runat="server" Text='<%#Bind("EmpName") %>'></asp:Label>

                    </ItemTemplate>


                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Age">
                    <ItemTemplate>
                        <asp:Label ID="lblempage" runat="server" Text='<%#Bind("Empage") %>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Salary">
                    <ItemTemplate>
                        <asp:Label ID="lblempsalary" runat="server" Text='<%#Bind("Salary") %>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>
                 <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblempcity" runat="server" Text='<%#Bind("city") %>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:Label ID="lblempaddress" runat="server" Text='<%#Bind("addressdata") %>'></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />

        </asp:GridView>
    </fieldset>
    </div>

C#.Net Code to bind and implement searching in GridView
  • In the code behind file (.aspx.cs) write the code as:
First include the following required namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

string configdata = ConfigurationManager.ConnectionStrings["dataConfig"].ToString();
        SqlConnection sqlcon;
        SqlCommand sqlcmd;
        SqlDataAdapter sqldap;
        DataSet ds;
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {



            if (!Page.IsPostBack == true)
            {
                BindEmpGrid();
                txtuser.Enabled = false;

            }
        }



        private void BindEmpGrid()
        {
            sqlcon = new SqlConnection(configdata);
            sqldap = new SqlDataAdapter();
            dt = new DataTable();
            try
            {
                sqldap = new SqlDataAdapter("BindEmpGrid_Sp", sqlcon);
                sqldap.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    grddatalist.DataSource = dt;
                    grddatalist.DataBind();
                }
                else
                {
                    grddatalist.DataSource = null;
                    grddatalist.DataBind();

                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                dt.Clear();
                dt.Dispose();
                sqlcon.Close();
            }


        }

        protected void ddlsearchby_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlsearchby.SelectedItem.Text == "All")
            {
                txtuser.Text = string.Empty;
                txtuser.Enabled = false;

            }
            else
            {

                txtuser.Enabled = true;
                txtuser.Text = string.Empty;
                txtuser.Focus();

            }
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {

            DataTable dt2 = new DataTable();
            sqlcmd = new SqlCommand();
            sqldap = new SqlDataAdapter();
            try
            {
                if (ddlsearchby.SelectedItem.Text == "EmpName")
                {
                    getEmpRecords(ddlsearchby.SelectedItem.Text, txtuser.Text.Trim());
                }
                else if (ddlsearchby.SelectedItem.Text == "Salary")
                {
                    getEmpRecords(ddlsearchby.SelectedItem.Text, txtuser.Text.Trim());
                }
                else if (ddlsearchby.SelectedItem.Text == "city")
                {
                    getEmpRecords(ddlsearchby.SelectedItem.Text, txtuser.Text.Trim());
                }

            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
        }

        private void getEmpRecords(string searchBy, string searchVal)
        {
            sqlcon = new SqlConnection(configdata);
            DataTable dt = new DataTable();
            sqlcmd = new SqlCommand();
            sqldap = new SqlDataAdapter();
            try
            {
                sqlcmd = new SqlCommand("SearchEmpRecords_sp", sqlcon);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.AddWithValue("@SearchBy", searchBy);
                sqlcmd.Parameters.AddWithValue("@SearchVal", searchVal);
                sqldap = new SqlDataAdapter(sqlcmd);
                sqldap.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    grddatalist.DataSource = dt;
                    grddatalist.DataBind();
                }
                else
                {
                    grddatalist.DataSource = dt;
                    grddatalist.DataBind();
                }
            }

            catch (Exception ex)
            {

            }
            finally
            {
                dt.Clear();
                dt.Dispose();
                sqlcon.Close();
            }


        }

    }
}
 

Saturday, 24 January 2015

Contact Page



 


Hai Friend This is Sharath Kumar Software Engineering professional with over 2+ 

years of experience developing Web and Windows based applications using 

Microsoft Technologies like  (C#, ASP.NET, ADO.NET,Ajax,Jquery).

 

For any queries please contact my Facebook page :

 

https://www.facebook.com/dotnetpprograms  

 

 


 




Friday, 23 January 2015

Types of Join in SQL Server||Different Types of SQL Joins||Join in SQL - Inner, Outer, Left, Right Join


Joins in Sql Server

Table 1:











 Table 2













Inner Join :-

The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.

select * from emp1
JOIN
emp2
on emp1.eid=emp2.eid






Left Outer Join:-
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

Query for Left Outer Join:-
select * from emp1
left outer join
emp2
on emp1.eid=emp2.eid








Right Outer Join:-
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

Query for Right Outer Join:-













SQL FULL OUTER JOIN
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

Query for FULL OUTER JOIN :-

select * from emp1
FULL OUTER JOIN
emp2
on emp1.eid=emp2.eid




Cross join:-
SQL CROSS JOIN will return all records where each row from the first table is combined with each row from the second table. Which also mean CROSS JOIN returns the Cartesian product of the sets of rows from the joined tables.


SELECT * FROM emp1 CROSS JOIN emp2





Write a query to display from table1 and table2 records?
Table 1



Table2



Sql query to join two tables are:-
select emp1.eid,emp1.ename,emp1.esal,emp2.eadd,emp2.ephone from emp1 join emp2 on

emp1.eid=emp2.eid