Wednesday, 17 January 2018

Part-26 Sql Data Reader in asp.net with Try and Catch exception





back end code :



using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



namespace ADO1

{

    public partial class ado2 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("Data Source=FACULTY-35;Initial Catalog=ADODOTNET_LEARNING;User ID=SA;Password=aptech");

            try

            {

                SqlCommand cmd = new SqlCommand("SELECT * FROM TBL_PRODUCT", con);

                con.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                GridView1.DataSource = rdr;

                GridView1.DataBind();

            }

            catch (Exception ex)

            {



                Response.Write("Error is " + ex);

            }

            finally

            {

                con.Close();

            }







        }

    }

}

Front end code :



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



<!DOCTYPE html>



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">

            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />

            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />

            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

            <SortedAscendingCellStyle BackColor="#FFF1D4" />

            <SortedAscendingHeaderStyle BackColor="#B95C30" />

            <SortedDescendingCellStyle BackColor="#F1E5CE" />

            <SortedDescendingHeaderStyle BackColor="#93451F" />

        </asp:GridView>

    </div>

    </form>

</body>

</html>

web.config code :

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  
    <connectionStrings>
      <add name="testdbms" connectionString="Data Source=FACULTY-35;Initial Catalog=ADODOTNET_LEARNING;User ID=SA;Password=aptech" providerName="System.Data.SqlClient"/>
    </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>

No comments:

Post a Comment

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...