Mono Support Insert Code Block on Page 

Viewed 18185 time(s), 6 post(s), 10/25/2014 6:55:49 PM - by Samtg
10/25/2014 6:55:49 PM
440 Reputation 38 Total posts

Hi,
     I was trying to insert a code block on a page, my wall, but keep getting an error like this:
c:\Users\Sam\Documents\My Web
Sites\EmptySite10\MonoX\Pages\UserProfile\MyWall.aspx(20): error CS1518:
Expected class, delegate, enum, interface, or struct
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath
virtualPath)at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath
virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean
allowBuildInPrecompile, Boolean throwIfNotFound, Boolean
ensureIsUpToDate)at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext
context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath
virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath
virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp)at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)at System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context,
String requestType, String virtualPath, String path)at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Also, when I try to insert it in a different part of the page, it says something like Code block not allowed in this context.

Any help would be helpful.

Samtg

1
10/28/2014 5:53:08 PM
7207 Reputation 956 Total posts

Could you please post exact code you were trying to insert?

2
10/30/2014 12:40:30 AM
440 Reputation 38 Total posts

Yeah, sure.
Here is the C# code:

<%
protected void ExportTextFile(object sender, EventArgs e)
{
string UserId=System.Environment.UserName;

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT @DateCreated AND @NoteContent FROM SnNote"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.Add("@UserId", UserId);
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);

//Build the Text file data.
string txt = string.Empty;

foreach (DataColumn column in dt.Columns)
{
//Add the Header row for Text file.
txt += column.ColumnName + "\t\t";
}

//Add new line.
txt += "\r\n";

foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
txt += row[column.ColumnName].ToString() + "\t\t";
}

//Add new line.
txt += "\r\n";
}

//Download the Text file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=SqlExport.txt");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(txt);
Response.Flush();
Response.End();
}
}
}
}
}%>

I am using this to call it:
<a href="#" runat="server" onclick="<% ExportTextFile();%>" style="height:7px;width:40px;border:outset">PK</a>

Thanks.

3
10/30/2014 4:38:23 PM
7207 Reputation 956 Total posts

For a start, you have a method that is not placed inside a class, and that is what the original error says - you cannot have standalone methods in C#. Other than that, you should really use a codebehind file for your code instead of putting the code inline - see http://www.codeproject.com/Articles/8178/Inline-Single-File-vs-CodeBehind for more details.

4
10/31/2014 8:26:51 AM
2793 Reputation 345 Total posts

Hi all,
you CAN embed methods, but using runat="server" in a script block

see
http://www.808.dk/?code-aspnet-inline
and
http://msdn.microsoft.com/en-us/library/ms178135%28v=vs.100%29.aspx

for examples

5
10/31/2014 1:19:38 PM
7207 Reputation 956 Total posts

I agree that you can do it (but not with old ASP style <% %> tags, but with script runat="server" like you suggest), however I am saying that much better practice is to use the codebehind approach.

6
This is a demo site for MonoX. Please visit Mono Software for more info.