Newbie: Connecting to a sql database

M

Mike Walker

Hi,
Im using the asp.net for dummies and im trying to connect to a sql database.
Here is my code:-
<%@Page Explicit="True" Language="VB" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
<head></head>
<script runat="server">
Dim Connect as SqlConnection
Dim SQLCmd as SqlCommand
Dim Adapter as SqlDataAdapter
Dim dsTable as DataSet

Connect = New SqlConnection
SQLCmd = New SqlCommand
Adapter = New SqlAdapter
dsTable = New DataSet

ConnectString="Provider=SQLOLEDB;UID=sa;PWD=;Name=TestData;Data
Source=MyServer;initial Catalog=TestDatabase;"
Connect.ConnectionString=ConnectString

SQLCmd.CommandText="Select * from NameTable"
SQLCmd.Connection=Connect

Adapter.SelectCommand = SQLCmd

Adapter.SelectCommand.Connection.Open

Adapter.Fill(dsTable,"MyData")

<asp:datagrid id="MainGrid" runat="server" />

MainGrid.DataSource=dsTable.Tables("MyData")
MainGrid.DataBind

</script>
</html>

However im getting the following error:-

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.
Compiler Error Message: BC30188: Declaration expected.
Source Error:
Line 11: Dim dsTable as DataSet
Line 12:
Line 13: Connect = New SqlConnection
Line 14: SQLCmd = New SqlCommand
Line 15: Adapter = New SqlAdapter

Source File: c:\inetpub\wwwroot\testweb\test.aspx Line: 13

what am i doing wrong ?

Note:-
I know im using the long way round DIM, i know I can use :-
Dim Connect as SqlConnection = New New SqlConnection
But I thought I would use the long way until i understand how its working,
or is this where im going wrong ?

Thanks
Mike

(Please do not reply to this email address)
 
M

Mike Walker

Many thanks for the answer, and I have changed the line of code
however the problem is still there.
The error is shown thus:-
Line 13 is : - Connect = New SqlConnection
with the note:- BC30188: Declaration expected.
but it is declared above as :- Dim Connect as SqlConnection

Any idears ?

Thanks
Mike.



The error
 
M

Mike Walker

I have found the answer here:-

http://ryangregg.com/archive/2004/04/22/792.aspx

My code now looks like:-
<script runat="server">
Dim Connect as SqlConnection
Dim SQLCmd as SqlCommand
Dim Adapter as SqlDataAdapter
Dim dsTable as DataSet
</script>
<%
Connect = New SqlConnection
SQLCmd = New SqlCommand
. . . rest of code . . .
%>

But i now have a new problem:-
Compiler Error Message: BC30636: '>' expected.
Source Error:
Line 37:
Line 38:
Line 39: <asp:datagrid id="MainGrid" runat="server" />
Line 40:
Line 41:
Source File: c:\inetpub\wwwroot\jnyweb\test.aspx Line: 39

But again I can not find out why ?
 
P

Patrick

<script runat="server">

Adapter.Fill(dsTable,"MyData")

<asp:datagrid id="MainGrid" runat="server" />

MainGrid.DataSource=dsTable.Tables("MyData")
MainGrid.DataBind

</script>
</html>

You can't have an <asp:datagrid> tag inside of a server script block like
this.

You need to do this AFTER your </script> tag:

<form runat=server>
<asp:datagrid id="MainGrid" runat="server" />
</form>

- Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top