Thursday, September 17, 2020

Search and Display Data In GridView From Database Table In Asp.Net Using C#.Net

    This article will show you how you can Search and Display Data In GridView From Database Table In Asp.Net Using C#.Net. In this you will perform search operation on the bases of added textbox value.

So for this article first we will create a new sql table in which we will add some data.

                                     

Now add some value in table.           


Here is the table creation query.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserDetail](
      [Id] [int] NULL,
      [Name] [varchar](50) NULL,
      [EmailId] [varchar](50) NULL,
      [Address] [varchar](100) NULL,
      [Status] [int] NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

Now in this we will create a new asp.net application and add the below code into the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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>Search and Display Data In GridView From Database Table In Asp.Net Using C#.Net
    </title>
    <style type="text/css">
        .style1
        {
            height: 41px;
        }
        .style2
        {
            height: 41px;
            width: 142px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%" cellpadding="5" cellspacing="5">
            <tr>
                <td align="right" class="style1">
                    Search</td>
                <td align="left" class="style2">
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                <td align="left" class="style1">
                    <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click"
                        Text="Search" />
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:GridView ID="GridView1" runat="server" Width="100%">
                    </asp:GridView>
                </td>
              
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Now on button click add the below code.

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

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView("");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGridView(TextBox1.Text);
        }
        private void BindGridView(string searchVal)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {
                DataTable objdt = new DataTable();
                string query = searchVal == "" ? "select * from UserDetail;" : "select * from UserDetail where name like '%" + searchVal + "%';";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = objdt;
                    GridView1.DataBind();
                }
            }
            catch
            {
                con.Close();
            }
        }
    }
}

In code I have created a new function and in this function I have passed the search value on the bases of search value query will get prepared and data will displayed on the grid view.

Now add the connection string in your web.config file.

<connectionStrings>
              <add name="con" connectionString="Data Source=ADMIN-PC;Initial Catalog=Test;uid=sa;pwd=ajay;"
          providerName="System.Data.SqlClient" />
 </connectionStrings>

Now we have done run the application and check the output.
         

Now add some value ad click on search.
                             



                                                                                                                                                                    



Monday, August 3, 2020

Why We Use Trigger in SQL Server Database ?


A trigger is a special type of stored procedure that automatically runs when an event occurs in the database server. DML triggers run when a user tries to modify data through a data manipulation language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view.


Types of Triggers

Data Definition Language (DDL) triggers
Data Manipulation Language (DML) triggers
Logon triggers

DML

Instead of Trigger: An Instead of trigger is fired instead of the triggering action such as an insert, update, or delete
After Trigger: An After trigger executes following the triggering action, such as an insert, update or delete


DDL Trigger

This type of trigger is fired against DDL statements like Drop Table, Create Table or Alter Table. DDL Triggers are always After Triggers.

Friday, May 8, 2020

What is the best way to develop a web application in .NET?


Microsoft’s dot net is one of the most popular frameworks for developing web apps in different ways. Dot net is an open-source and high-performance framework used for building web apps.
The web apps are modern, cloud-based and internet-based and are built using various tools and services.
In this blog, we will spotlight some of the adjoining tools and technologies that can be utilized by dot net developers to build robust web apps.

Saturday, April 18, 2020

How do I get started developing .NET Application?

 . Net is the platform


MVC and web forms are design patterns
C# is the language you develop application with.
Web, mobile and windows are different targets you can develop for.
You need to know what the platform is and understand the construction of the platform. Choose the design patters. Web forms for web development is easier to begin with. The depth of understanding of C# OR other programming language is directly proportional to how flexible and faster you can solve a programming problem.
Install visual studio community, watch basic C# tutorials for beginners. Start solving small math problems with code. I'd suggest a C# book would give you good head start. Once you're comfortable with web forms start with MVC.

Search and Display Data In GridView From Database Table In Asp.Net Using C#.Net

    This article will show you how you can Search and Display Data In GridView From Database Table In Asp.Net Using C#.Net. In this you will...