HOW TO WRITE PROGRAM TO DATA ADAPTER?

Mark Devid

Total Post:9

Points:45
Posted by  Mark Devid
C# 
C#
 420  View(s)
Ratings:
Rate this:
  1. Mark Devid

    Post:9

    Points:45
    How to write program to data adapter?

    DataAdapter


    DataAdapter.aspx

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

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
             <div>             <h1>Registration Details
                </h1>
                <br />
                <asp:GridView ID="grdBind" runat="server" AutoGenerateColumns="false" ForeColor="purple" Font-Bold="true" Font-Names="Tahoma" Font-Italic="false">
                    <Columns>
                        <asp:BoundField DataField="id" HeaderText="Id" />
                        <asp:BoundField DataField="FIRSTNAME" HeaderText="FIRST NAME" />
                        <asp:BoundField DataField="LASTNAME" HeaderText="LAST NAME" />
                        <asp:BoundField DataField="DOB" HeaderText="DOB" />
                        <asp:BoundField DataField="ADDRESS" HeaderText="ADDRESS" />
                        <asp:BoundField DataField="GENDER" HeaderText="GENDER" />
                        <asp:BoundField DataField="USERNAME" HeaderText="USER NAME" />

                    </Columns>
                    <AlternatingRowStyle BackColor="yellow" />
                    <EmptyDataTemplate>
                        No Record Found!!
                    </EmptyDataTemplate>
                </asp:GridView>
                 </div>
        </div>
        </form>
    </body>
    </html>

     DataAdapter.aspx.cs


    
    
    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 DataAdapter : System.Web.UI.Page
    {
        SqlConnection cn = null;
        SqlDataAdapter da = null;
        SqlCommand cmd = null;
        string str = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {         if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }
        void BindGrid()
        {
            cn = new SqlConnection("Data Source=.;uid=sa;pwd=mindstick;database=NewForm");
            str = "select id, FIRSTNAME,LASTNAME,CONVERT(varchar, DOB,103)DOB, ADDRESS,GENDER,USERNAME from REGISTRATION where USERNAME <> '' ";
            if (cn.State!= ConnectionState.Open)
                cn.Open();
            SqlDataAdapter da = new SqlDataAdapter(str, cn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (da != null)
            {
                grdBind.DataSource = ds;
                grdBind.DataBind();
            }
            else
            {
                Response.Write("No Record Found");
            }

        }

    }

     


    Thanks

      Modified On Mar-28-2018 06:41:37 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!