Sunday, 30 November 2014

Difference Between DataSet and DataTable


Friday, 14 November 2014

How to Insert New Row Using GridView Footer in Asp.Net.


Insert New Row Using  GridView Footer




A GridView Bind to Data Table. But Add GridView Footer  Row Directly.



                         DEMO






Insert Data Using GridView Footer Row 









Html Coding


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="False"
            OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" ShowFooter="True">
             <Columns>
           <asp:TemplateField HeaderText="Action">
             <FooterTemplate>
             <asp:LinkButton ID="LinkButton1" runat="server"
                  CommandName="Select">Insert</asp:LinkButton>
             </FooterTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Name">
             <FooterTemplate>
             <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
             </FooterTemplate>
             <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
             </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Email ID">
             <FooterTemplate>
            <asp:TextBox ID="txtEmailID" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Eval("email") %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City">
            <FooterTemplate>
           <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
          </FooterTemplate>
          <ItemTemplate>
         <asp:Label ID="Label3" runat="server" Text='<%# Eval("city") %>'></asp:Label>
         </ItemTemplate>
        </asp:TemplateField>
             </Columns>
        </asp:gridview>
       
    </div>
    </form>
</body>
</html>




C# Coding



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 Grid_Insert : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;

 
    public void dbcon()
    {
        string connn = (System.Configuration.ConfigurationManager.
ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind11();
        }
    }


    protected void bind11()
    {
        dbcon();
        query = "select * from grid";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
        {
            Gridview1.DataSource = ds;
            Gridview1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            Gridview1.DataSource = ds;
            Gridview1.DataBind();
            int columncount = Gridview1.Rows[0].Cells.Count;
            Gridview1.Rows[0].Cells.Clear();
           // GridView1.FooterRow.Cells.Clear();
            Gridview1.Rows[0].Cells.Add(new TableCell());
            Gridview1.Rows[0].Cells[0].ColumnSpan
 = columncount;
            Gridview1.Rows[0].Cells[0].Text = "No Records Found";
        }
    }


    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {

        TextBox txtNamee = (TextBox)Gridview1.FooterRow.
FindControl("txtName");
        TextBox txtEmaill = (TextBox)Gridview1.FooterRow.
FindControl("txtEmailID");
        TextBox txtCity = (TextBox)Gridview1.FooterRow.
FindControl("txtCity");

        dbcon();
  query = "insert into grid (name,email,city)values('"+txtNamee.Text+"',
'"+txtEmaill.Text+"','"+txtCity.Text+"')";
        cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();
        con.Close();

        bind11();
    }

}






















































































OUTPUT























                         
                                                   

Thursday, 13 November 2014

Search Suggestion Box from MULTIPLE TABLE(Like Google Search) Using Ajax AutoCompleteExtender in Asp.Net C#

Search Suggestion Box from Multiple Table Using AJAX


The script will use a TextBox to search through a  instantly to return results Alphabetically without reloading the page. Every typed letter will update the results.

 Auto Suggestion Box Master Page
 Auto Suggestion Box Multiple Table Search
 Auto Suggestion Ajax AutoCompleteExtender
 Auto Complete Search For WINDOW APPLICATION


            Show Result From "Multiple Tables" 


             Download - Ajax .DLL File From Below Link
     
 "http://ajaxcontroltoolkit.codeplex.com/downloads/get/768265"

  


                         DEMO







 Html Coding



<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
          
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
   
         <asp:TextBox ID="txtCourse" runat="server"></asp:TextBox>
            <asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server"
             TargetControlID="txtCourse"
             ServicePath="Course.asmx"
             ServiceMethod ="CourseDetail"
             MinimumPrefixLength="1"
             CompletionSetCount="20"
             CompletionInterval="0"
             EnableCaching="true">
            </asp:AutoCompleteExtender>  

    </div>
    </form>
    <p>
        &nbsp;</p>
</body>

</html>




C# Coding


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for Course
/// </summary>

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[System.Web.Script.Services.ScriptService]

public class Course : System.Web.Services.WebService
{

    public Course()
    {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public List<string> CourseDetail(string prefixText)
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection
            (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());

        con.Open();

        System.Data.SqlClient.SqlCommand cmd1 = new System.Data.SqlClient.SqlCommand
            ("select * from coursename where name like @name+'%'", con);

        System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand
    ("select * from reg where name like @name+'%'", con);

        System.Data.SqlClient.SqlCommand cmd3 = new System.Data.SqlClient.SqlCommand
    ("select * from register where name like @name+'%'", con);

        cmd1.Parameters.AddWithValue("@name", prefixText);

        cmd2.Parameters.AddWithValue("@name", prefixText);

        cmd3.Parameters.AddWithValue("@name", prefixText);

        System.Data.SqlClient.SqlDataAdapter da1 = new System.Data.SqlClient.SqlDataAdapter(cmd1);

        System.Data.SqlClient.SqlDataAdapter da2 = new System.Data.SqlClient.SqlDataAdapter(cmd2);

        System.Data.SqlClient.SqlDataAdapter da3 = new System.Data.SqlClient.SqlDataAdapter(cmd3);


        System.Data.DataTable dt1 = new System.Data.DataTable();

        System.Data.DataTable dt2 = new System.Data.DataTable();

        System.Data.DataTable dt3 = new System.Data.DataTable();

        da1.Fill(dt1);
        da2.Fill(dt2);
        da3.Fill(dt3);

        List<string> Course_names = new List<string>();

        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            Course_names.Add(dt1.Rows[i][2].ToString());
        }
        for (int j = 0; j < dt2.Rows.Count; j++)
        {
            Course_names.Add(dt2.Rows[j][1].ToString());
        }
        for (int k = 0; k < dt3.Rows.Count; k++)
        {
            Course_names.Add(dt3.Rows[k][0].ToString());
        }

        return Course_names;
    }


}

First  - Add  the New Web Form Name - Suggest.aspx