scripting syntax

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a project in VS.net using C
In a code-behind page I am able to create a dataset from an Access database and then use the dataset to populate a datagrid
I am trying to follow some examples in a book and would like to remove the code from the code-behind page and place it in server-side script
The following line runs in code-behind but generates an error when used in the scrip
ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0; " + @"Data Source=" + @"C:\Documents and Settings\Jim McGivney\My Documents\DMORT04\Win0401.mdb;" + ";"

The error I get is: CS1519: Invalid token '=' in class, struct, or interface member declaratio

Any help would be appreciated
Thanks
Ji
 
Hi Jim,

Thank you for posting in the community! My name is Kevin, and I will be
assisting you on this issue.

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you're trying to display data from
the Access database on a datagrid. When moving code from code behind to the
server-side script, an compilation error 'CS1519' occurs. If there is any
misunderstanding, please feel free to let me know.

Based on my research, this error might be caused by assigning value to a
variable which was not declared. Whether we are programming in code behind
or server-side script, we have to declare the type of a variable before
using. You might have declared the string 'ConnStr' in code behind, but
somehow forget to declare it when moving to the server-side script. I have
made some changes to the code. Hope this helps.

<script runat="server">
string ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0; " + @"Data Source=" +
@"C:\Documents and Settings\Jim McGivney\My Documents\DMORT04\Win0401.mdb;"
+ ";";
</script>

Does this answer your question? If anything is unclear, please feel free to
reply to the post. I'm standing by to be of assistance.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thanks for your help, but the problem still remains. Your solution moved the error down two lines. My whole page-behind code is (this code works)
string ConnStr
//Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source="C:\Documents and Settings\Jim McGivney\My Documents\DMORT04\Win0401.mdb";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider="Microsoft.Jet.OLEDB.4.0";Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=Fals
ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0; " + @"Data Source=" + @"C:\Documents and Settings\Jim McGivney\My Documents\DMORT04\Win0401.mdb;" + ";"
OleDbConnection myOleDbConn = new OleDbConnection(ConnStr)
myOleDbConn.Open()
string SQL = "SELECT * FROM Ante"
OleDbCommand myOleDbCmd = new OleDbCommand(SQL, myOleDbConn)
OleDbDataReader myReader = myOleDbCmd.ExecuteReader()
AnteGrid.DataSource = myReader
AnteGrid.DataBind()

Using your suggested code I place the following in the servicer side script
<script runat="server"
Sub Page_Load(object sender, System.EventArgs e)
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + @"C:\Documents and Settings\Jim McGivney\My Documents\DMORT04\Win0401.mdb;"
OleDbConnection myOleDbConn = new OleDbConnection(ConnStr)
myOleDbConn.Open
string SQL = "SELECT * FROM Ante"
OleDbCommand myOleDbCmd = new OleDbCommand(SQL, myOleDbConn)
OleDbDataReader myReader = myOleDbCmd.ExecuteReader()
AnteGrid.DataSource = myReader
AnteGrid.DataBind()
</script>

But I now get the error: Compiler Error Message: CS1519: Invalid token ';' in class, struct, or interface member declaratio
for the line of code: myOleDbConn.Open

Any suggestions are welcomed, I am sure there is a very simple solution
Thanks
Jim
 
Hi Jim,

It is not recommend to use server-side scripts. Code behind the advantage
in ASP.NET than ASP. Also we have to do more check when moving code behind
to server-side script.

According to the code you provided, there are many syntax errors. Here I
listed some of them:

1. The brackets has to be added when calling a method, even it has no
parameters. So myOleDbConn.Open; has to be myOleDbConn.Open();
2. Because you have imported the namespaces System.Data and
System.Data.OleDb in the code behind, you needn't add namespace before a
class name. However, we have to add namespace before class names in server
side scripts. When declaring and OleDbConnection object, it has to be
System.Data.OleDb.OleDbConnection.
3. The declaration of the function is wrong. Sub is used in VB. In C#, we
have to use protected void Page_Load(object sender, System.EventArgs e).
And it doesn't follow with a semicolon.
4. We also have to enable AutoEventWireup in aspx pages. We can modify the
first line of the page script to achieve this. The first line might look
like the following, we just need to set AutoEventWireup="true".
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="true"
Inherits="WebApplication23.WebForm1" %>

Here I have made some modifications on your code:

<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
@"C:\Documents and Settings\Jim McGivney\My Documents\DMORT04\Win0401.mdb;";
System.Data.OleDb.OleDbConnection myOleDbConn = new
System.Data.OleDb.OleDbConnection(ConnStr);
myOleDbConn.Open();
string SQL = "SELECT * FROM Ante";
System.Data.OleDb.OleDbCommand myOleDbCmd = new
System.Data.OleDb.OleDbCommand(SQL, myOleDbConn);
System.Data.OleDb.OleDbDataReader myReader = myOleDbCmd.ExecuteReader();
AnteGrid.DataSource = myReader;
AnteGrid.DataBind();
}
</script>

If the problem still persists, please feel free to let me know. I'm
standing by to be of assistance.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top