ASP.NET

Moderators: None (Apply to moderate this forum)
Number of threads: 1727
Number of posts: 3292

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with accessing databases with ASP.NET and SQL Server 2000 Posted by joejoe on 10 Feb 2004 at 10:05 PM

Can someone tell me why I cannot get the data from the Northwind database in MSQL Server 2000. I am trying to place the data in the data gride and disply it.

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebWelcome.WebForm1"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>

<HTML>
<script language="VB" runat="server">

Sub Page_Load(Scr As Object, E AS EventArgs)
Dim myDataGrid as new DataGrid()
Dim myConn As SqlConnection = New _
SqlConnection("server=localhost;uid=sa;pwd=156977;database=Northwind")

Dim myCommand As SQLCommand = New SQLCommand("select * form Customers", MyConn)

myConn.Open()

Dim DS AS SQLDataReader=myCommand.ExecuteReader()

myDataGrid.DataSource= DS
myDataGrid.DataBind()

myConn.Close()
End Sub
</script>

<body >

<h3><font face="Verdana">Northwind Customers</font></h3>
<asp:DataGrid ID="myDataGrid"
width="70"
BackColor="#FFFFFF"
BorderColor="black"
SHowFooter="False"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#cfcfcf"
EnableViewState="False"
</asp:DataGrid>

</body>
</HTML>

Report
Re: Help with accessing databases with ASP.NET and SQL Server 2000 Posted by iwilld0it on 11 Feb 2004 at 6:28 AM
:
: Can someone tell me why I cannot get the data from the Northwind database in MSQL Server 2000. I am trying to place the data in the data gride and disply it.
:
: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebWelcome.WebForm1"%>
: <%@ Import Namespace="System.Data"%>
: <%@ Import Namespace="System.Data.SqlClient"%>
:
: <HTML>
: <script language="VB" runat="server">
:
: Sub Page_Load(Scr As Object, E AS EventArgs)
: Dim myDataGrid as new DataGrid()
: Dim myConn As SqlConnection = New _
: SqlConnection("server=localhost;uid=sa;pwd=156977;database=Northwind")
:
: Dim myCommand As SQLCommand = New SQLCommand("select * form Customers", MyConn)
:
: myConn.Open()
:
: Dim DS AS SQLDataReader=myCommand.ExecuteReader()
:
: myDataGrid.DataSource= DS
: myDataGrid.DataBind()
:
: myConn.Close()
: End Sub
: </script>
:
: <body >
:
: <h3><font face="Verdana">Northwind Customers</font></h3>
: <asp:DataGrid ID="myDataGrid"
: width="70"
: BackColor="#FFFFFF"
: BorderColor="black"
: SHowFooter="False"
: CellPadding=3
: CellSpacing="0"
: Font-Name="Verdana"
: Font-Size="8pt"
: HeaderStyle-BackColor="#cfcfcf"
: EnableViewState="False"
: </asp:DataGrid>
:
: </body>
: </HTML>
:
:

Try removing this line from your code ...

Dim myDataGrid as new DataGrid() 


Basically the .NET framework reads the <asp: tags, parses it out and creates an instance of the control for you. So "myDataGrid" will be a legitimate reference to a datagrid object as soon as the page load event occurs. Creating a new reference as shown above is effectively overwriting the reference that the .NET framework worked so hard to create and that is why it appears that your data-binding is not working.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.