Advertise with us
Control Panel
Forum
Hosting Help
ASP.NET 4.5 & SQL 2012 Hosting
Resources
Notice
Login
Members
Shared Hosting
|
Upgrade
|
Advertise
|
Tutorials
|
Home
»
Questions & Answers
»
Example .ASPX
Example .ASPX
28 May, 12
Author:
Matthew Cox
self
Hey guys, could anyone email me an example .ASPX that is accessing a SQL Server 2008 db? Also it would help if its using gridview or details view - just post here please.
Answers .............
Author:
John Cleary
Posted Date: 28 May, 12
The following is a self contained aspx file that will connect to a db and print a table in a gridview
<%@ Page Language=C# Inherits=System.Web.UI.Page %>
<script runat=server>
private string connString = @Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=addressbook;Persist Security Info=True;User ID=Jimmy;Password=*******;;
private string sqlString = @SELECT * FROM addresses;;
protected override void OnLoad(EventArgs e)
{
HtmlForm form = new HtmlForm();
GridView gridView = new GridView();
form.Controls.Add(gridView);
Controls.Add(form);
SqlDataSource ds = new SqlDataSource(connString, sqlString);
ds.DataSourceMode = SqlDataSourceMode.DataReader;
gridView.DataSource = ds.Select(DataSourceSelectArguments.Empty);
gridView.DataKeyNames = new String[] { id }; // Name of primary key
gridView.DataBind();
}
</script>
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html
xmlns=
http://www.w3.org/1999/xhtml>
<head
runat=server>
<title></title>
</head>
<body>
</body>
</html>
Author:
Matthew Cox
self
Posted Date: 28 May, 12
Thanks, could I also see one in VB.NET?
Author:
John Cleary
Posted Date: 28 May, 12
All double quotes have been removed by the question system e.g
connString and sqlString
private string sqlString = @SELECT * FROM addresses;;
should be
private string sqlString = @[double-quote]SELECT * FROM addresses;[double-quote];
also
gridView.DataKeyNames = new String[] { id };
should be
gridView.DataKeyNames = new String[] { [double-quote]id[double-quote] };
Author:
John Cleary
Posted Date: 28 May, 12
The same in VB.NET
<%@ Page Language=VB Inherits=System.Web.UI.Page%>
<script runat=server>
Private Const ConnString As String = Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=addressbook;Persist Security Info=True;User ID=John;Password=apple06;
Private Const SqlString As String = SELECT * FROM AddressBook.dbo.AddressBook;
Protected Overrides Sub OnLoad(e As EventArgs)
Dim gridView As New GridView()
FindControl(form1).Controls.Add(gridView)
Dim ds As New SqlDataSource(ConnString, SqlString)
gridView.DataSource = ds.[Select](DataSourceSelectArguments.Empty)
gridView.DataKeyNames = New [String]() {id}
gridView.DataBind()
End Sub
</script>
<!DOCTYPE html>
<html xmlns=
http://www.w3.org/1999/xhtml>
<head>
> <title>TestDB</title>
</head>
<body>
<form Id=form1 runat=server></form>
</body>
</html>
You must
login
to post answer for this question.
Total
members
:
250643
Average new registrations per day (in last 7 days):
7
New Registration:
Open
Register Now
Talk to Webmaster
Tony John
Facebook
Google+
Twitter
Advertise
Privacy Policy
Terms of use
Contact Us
SpiderWorks Technologies Pvt Ltd.
All Rights Reserved.