MonoX support board

Start the conversation, ask questions and share tips and solutions with fellow developers.

Non-registered users can only browse through our support boards. Please register now if you want to post your questions. It takes a second and it is completely free. Alternatively, you can log in without registration using your credentials at major sites such as Google, Microsoft Live, OpenId, Facebook, LinkedIn or Yahoo.

Insert Code Block on Page  (Mono Support )

Viewed 9400 time(s), 6 post(s) 10/25/2014 6:55:49 PMby Samtg
Samtg

Samtg

10/25/2014 6:55:49 PM
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
This content has not been rated yet. 
440 Reputation 38 Total posts
denis

denis

10/28/2014 5:53:08 PM
Could you please post exact code you were trying to insert?
This content has not been rated yet. 
7207 Reputation 956 Total posts
Samtg

Samtg

10/30/2014 12:40:30 AM
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.

This content has not been rated yet. 
440 Reputation 38 Total posts
denis

denis

10/30/2014 4:38:23 PM
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.
This content has not been rated yet. 
7207 Reputation 956 Total posts
Zoomicon

Zoomicon

10/31/2014 8:26:51 AM
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
This content has not been rated yet. 
2793 Reputation 345 Total posts
denis

denis

10/31/2014 1:19:38 PM
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.
This content has not been rated yet. 
7207 Reputation 956 Total posts