---
title: "How to write program to data adapter?"  
description: "How to write program to data adapter?"  
author: "Mark Devid"  
published: 2016-07-23  
updated: 2020-09-16  
canonical: https://www.mindstick.com/interview/23032/how-to-write-program-to-data-adapter  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# 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**

## Answers

### Answer by Mark Devid

**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**


---

Original Source: https://www.mindstick.com/interview/23032/how-to-write-program-to-data-adapter

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
