Home > DeveloperSection > Interviews > How to write program to data adapter?

Posted on    July-23-2016 6:49 AM

 C# C# 
Ratings:
 1 Answer(s)
  162  View(s)
Rate this:

Mark Devid
Mark Devid

Total Post:9

Points:45
Posted on    July-23-2016 6:49 AM

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 Jul-23-2016 08:16:17 AM

Don't want to miss updates? Please click the below button!

Follow MindStick