Last active
June 27, 2017 15:12
-
-
Save ssbalakumar/a4fc119d78dfde48de1f68b1e381211f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------Conn String | |
<connectionStrings> | |
<add name="DBConnection" connectionString="Data Source=balan-pc;Initial Catalog=Tekardia;Persist Security Info=True;User ID=sa;Password=sqlserver" providerName="System.Data.SqlClient" /> | |
</connectionStrings> | |
--SQL | |
use Tekardia | |
-- Table Department | |
create table tblDepartment ( | |
intDeptId int primary key identity(1,1), | |
varDeptName varchar(40) | |
) | |
go | |
alter procedure spdept | |
as | |
select * from tblDepartment | |
go | |
alter procedure spdept | |
as | |
select intDeptId "Dept ID", varDeptName "Dept Name" from tblDepartment | |
exec spdept | |
insert into tblDepartment values('Development') | |
insert into tblDepartment values('Testing') | |
insert into tblDepartment values('HR') | |
insert into tblDepartment values('Marketing') | |
------UI | |
<h4>Gridview Databinding using SQL Command</h4> | |
<asp:GridView ID="gvNormal" runat="server" CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None"> | |
<AlternatingRowStyle BackColor="White" /> | |
<EditRowStyle BackColor="#7C6F57" /> | |
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> | |
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> | |
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> | |
<RowStyle BackColor="#E3EAEB" /> | |
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> | |
</asp:GridView> | |
<br /> | |
<h4>Gridview Databinding using Stored Procedure</h4> | |
<asp:GridView ID="gvSP" runat="server" CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None"> | |
<AlternatingRowStyle BackColor="White" /> | |
<EditRowStyle BackColor="#7C6F57" /> | |
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> | |
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> | |
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> | |
<RowStyle BackColor="#E3EAEB" /> | |
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> | |
</asp:GridView> | |
------ Code | |
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data; | |
using System.Data.Common; | |
using System.Data.SqlClient; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
namespace GridView | |
{ | |
public partial class index : System.Web.UI.Page | |
{ | |
// Connection - Command - Dataadapter - Fill Datatable - assign dt to grid | |
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString()); | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
if (!Page.IsPostBack) { | |
bindNormal(); | |
bindSP(); | |
} | |
} | |
public void bindNormal() | |
{ | |
DataTable dt = new DataTable(); | |
try | |
{ | |
con.Open(); | |
SqlCommand cmd = new SqlCommand("select intDeptId as ID, varDeptName as DeptName from tblDepartment", con); | |
SqlDataAdapter da = new SqlDataAdapter(cmd); | |
da.Fill(dt); | |
gvNormal.DataSource = dt; | |
gvNormal.DataBind(); | |
} | |
catch (System.Data.SqlClient.SqlException ex) | |
{ | |
string msg = "Fetch Error:"; | |
msg += ex.Message; | |
throw new Exception(msg); | |
} | |
finally | |
{ | |
con.Close(); | |
} | |
} | |
public void bindSP() | |
{ | |
DataTable dt = new DataTable(); | |
try | |
{ | |
con.Open(); | |
SqlCommand cmd = new SqlCommand("spdept", con); | |
cmd.CommandType = CommandType.StoredProcedure; | |
SqlDataAdapter da = new SqlDataAdapter(cmd); | |
da.Fill(dt); | |
gvSP.DataSource = dt; | |
gvSP.DataBind(); | |
} | |
catch (System.Data.SqlClient.SqlException ex) | |
{ | |
string msg = "Fetch Error:"; | |
msg += ex.Message; | |
throw new Exception(msg); | |
} | |
finally | |
{ | |
con.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment