How to display my c# code on an asp page?

Hi, I am trying to test my server and have the results display on a test page:

test.aspx
<asp:Literal ID="includeLiteral" runat="server" />

test.aspx.cs
includeLiteral.Text = Main();

But its not working. Can someone help me display c# code in my webpage?

http://auroriella.com/test.aspx


test.aspx
test.aspx.cs
FairyBusinessAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You are trying to display the return value from a function whose return type is void--meaning there is no return type. Why are you defining a console application inside of your web application to begin with? What is the overall goal?
0
FairyBusinessAuthor Commented:
Oh, I didnt realize I was lol.  How can you tell its a console application?  I was following a tutorial to test the server connection to my database.  I am trying to out put information from my Users table in my sql server database.

SqlCommand cmd = new SqlCommand("select * from Users", conn);
0
käµfm³d 👽Commented:
How can you tell its a console application?
Two indicators:  1) static void Main is usually used for console applications  2) You call Console.WriteLine.

Try this: Replace the contents of your .aspx.cs file with this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _27498948_2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 1. Instantiate the connection
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);

        SqlDataReader rdr = null;

        try
        {
            // 2. Open the connection
            conn.Open();

            // 3. Pass the connection to a command object
            SqlCommand cmd = new SqlCommand("select * from Users", conn);

            //
            // 4. Use the connection
            //

            // get query results
            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record
            while (rdr.Read())
            {
                includeLiteral.Text = rdr[0] as string;
            }
        }
        finally
        {
            // close the reader
            if (rdr != null)
            {
                rdr.Close();
            }

            // 5. Close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}

Open in new window


You will need to change the name of the namespace in line 8 to match your namespace.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

FairyBusinessAuthor Commented:
I don't have a name space currently. Does it matter what I choose to name it?
0
FairyBusinessAuthor Commented:
Why is my id coming up not recognized?
includeLiteral.png
0
FairyBusinessAuthor Commented:
I defined includeLiteral as an id in my test.aspx file, shouldn't that be enough???
0
käµfm³d 👽Commented:
This goes to what I mentioned in your other question. Try right-clicking on the .aspx file in Solution Explorer and see if you have a "Convert To Web Application" option. If so, click that, and you should get a new .aspx.designer.cs file. This file will hold the declarations for all the controls you add to your page.
0
FairyBusinessAuthor Commented:
I don't see that option:
app.png
0
käµfm³d 👽Commented:
Sorry, please disregard the last suggestion. You made a web site, not a web application.

Your new error is my fault, any way. I gave you a class name of "_Default", but that is not correct. Change "class _Default" to "class test".
0
FairyBusinessAuthor Commented:
ok I renamed the class but its still has a red underline and my webpage still doe not work.

Is it better to make a web application versus a website?


test.png
0
FairyBusinessAuthor Commented:
so the class name has to match the aspx file name?
0
käµfm³d 👽Commented:
Is it better to make a web application versus a website?
I think it would be a matter of preference. If you use web site, then your source code goes into the App_Code folder, and it is compiled on the fly by IIS. If you use web application, then you have to build your project first, which results in your code files being placed into a DLL, and you deploy that DLL with the markup pages to the server.

In your .aspx page, modify the Inherits attribute of the Page directive from:

Inherits="test"

Open in new window


to:

Inherits="test.test"

Open in new window


...since you put the test class inside of the test namespace in your code-behind file.
0
käµfm³d 👽Commented:
so the class name has to match the aspx file name?
AFAIK, no. It just needs to match what you put in the Inherits attribute of the Page directive.
0
FairyBusinessAuthor Commented:
1. Is a namespace really necessary?  Without it could I just have Inherits="test"  ?

2. I am not getting any errors in visual studio, but when I try to run the page in the browser it does not work still.  Any ideas as to why?

http://auroriella.com/test.aspx
0
käµfm³d 👽Commented:
Is a namespace really necessary?
No. Namespaces give a structure to your code. Think about it like this:  How many men are there in the world named "John"? Too many to count, right? If you had two friends named John, how would you refer to them when talking to other people? You would use their last names in much the same way you use a namespace to talk about different classes.

Let's say you had two different, but identically named classes called Error:

class Error  // this class handles file access errors
{
    public string FileName { get; set; }
}

Open in new window


class Error  // this class handles invalid input errors in the GUI
{
    public string ErrorMessage { get; set; }
}

Open in new window


 Now let's say you wanted to use both of these classes within the same block of code:

if (string.IsNullOrEmpty(this.textBox1.Text))
{
    Error e = new Error();

    e.ErrorMessage = "Filename cannot be blank!";
}
else if (!System.IO.File.Exists(this.textBox1.Text))
{
   Error e = new Error();

   e.FileName = this.textBox1.Text;
}

Open in new window


How does the C# compiler know which Error version to use in lines 3 and 9? You might say, "the compiler can look at the following line to see which property is being used." To that I would say, "How about this, then:"

if (anErrorDidOccur)
{
    Error e = new Error();
    Error e1 = new Error();

    e.ErrorMessage = "Filename cannot be blank!";
    e1.FileName = this.textBox1.Text;
}

Open in new window


This is where namespaces help us. If we redefine our classes to be located within namespaces, such as:

namespace FileStuff
{    class Error  // this class handles file access errors
    {
        public string FileName { get; set; }
    }
}

Open in new window


namespace GuiStuff
{
    class Error  // this class handles invalid input errors in the GUI
    {
        public string ErrorMessage { get; set; }
    }
}

Open in new window


Now we can use both classes in the same block without ambiguity:

if (anErrorDidOccur)
{
    GuiStuff.Error e = new GuiStuff.Error();
    FileStuff.Error e1 = new FileStuff.Error();

    e.ErrorMessage = "Filename cannot be blank!";
    e1.FileName = this.textBox1.Text;
}

Open in new window


In other words, instead of talking about John, we are now talking about John Smith.
0
käµfm³d 👽Commented:
when I try to run the page in the browser it does not work still.  Any ideas as to why?
What does the page say is the error when you view it in the browser?
0
käµfm³d 👽Commented:
How does the C# compiler know which Error version to use in lines 3 and 9? You might say, "the compiler can look at the following line to see which property is being used." To that I would say, "How about this, then:"

if (anErrorDidOccur)
{
    Error e = new Error();
    Error e1 = new Error();

    e.ErrorMessage = "Filename cannot be blank!";
    e1.FileName = this.textBox1.Text;
}

That example doesn't quite clarify. Let's change it to:

if (anErrorDidOccur)
{
    Error e = new Error();
    Error e1 = new Error();
}

Open in new window

0
FairyBusinessAuthor Commented:
I really like your examples of the namespaces.  I remember back in php I would want to have two functions named the same things but do different things and be used in different situations but I couldn't! I think this will come in handy :)

Here's what it says in the browser:

http://auroriella.com/test.aspx

it has a problem with line 30 but I don't know why.. .
line30.png
0
käµfm³d 👽Commented:
Did you add a connection string to your web.config file? The structure should be similar to:

<configuration>
    <connectionStrings>
      <add name="connection"
           connectionString="your connection string here"
           providerName="System.Data.SqlClient" /> <!-- the provider may change if you're not using SQL Server -->

    </connectionStrings>
</configuration>

Open in new window

0
FairyBusinessAuthor Commented:
0
käµfm³d 👽Commented:
You have two "Data Source" attributes in your connection string--there should only be one.
0
FairyBusinessAuthor Commented:
ok, I took away the first one entirely but still the same error message in the browser :(
0
FairyBusinessAuthor Commented:
0
käµfm³d 👽Commented:
How is your database set up? In the "Data Source" attribute, the parts are:

Server Name\SQL Server Instance Name

By your config file, this means you have a server (physical machine) named "MSI_K9MM_V". On this server, you have Sql Server installed, and the name you gave to the instance was "Tanya". Within this instance, you created a database named "auroriella". Is this all correct?
0
FairyBusinessAuthor Commented:
yep its all correct, that's why I'm stumped
0
FairyBusinessAuthor Commented:
0
käµfm³d 👽Commented:
Does you database server use Windows authentication or SQL Server authentication?
0
FairyBusinessAuthor Commented:
Windows authentication
0
nishant joshiTechnology Development ConsultantCommented:
might having problem with connection to the server create a good connection string

using,
http://www.connectionstrings.com/


and check for your authentication on demo application...
and if work then implement it on website..

Regards,
nishant
0
FairyBusinessAuthor Commented:
I had to end up getting information from my hosting site.  Besides that your info was great :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.