C#-Web: Using CodeFiles

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nicodemas
    Recognized Expert New Member
    • Nov 2007
    • 164

    #1

    C#-Web: Using CodeFiles

    Hi .NET experts. I'm tired of Classic ASP. Well, not really, but I figured there's a time to put stubborness aside. I have decided to begin learning .NET. I have some experience, but as you will soon find out, not much. I hand wrote all this, so forgive subsequent errors.

    From the two code snippets, can you tell me how to correctly setup the code behind? I'm getting a compilation error.

    Exercise1.aspx

    Code:
    <%@ Page Language="C#" Debug="True" CodeFile="exercise1.aspx.cs" Inherits="System" %>
    <html>
    <head><title>.NET</title></head>
    <body>
       <form id="frmExercise" runat="server">
          <div><asp:label id="lblMessage" runat="server" /></div>
          <label id="lblUsername" for="username">Username:</label>
          <asp:textbox id="username" runat="server" /><asp:RequiredFieldValidator ControlToValidate="username" Text="The username field is required!" runat="server" /><br />
          <label id="lblPassword" for="txtPassword">Password:</label><asp:textbox id="pword" textmode="password" runat="server" /><asp:RequiredFieldValidator ControlToValidate="pword" Text="The password field is required!" runat="server" /><br />
          <asp:button text="submit" onclick="GoForm" runat="server" />
       </form>
    </body>
    </html>
    Exercise1.aspx. cs

    Code:
    using System.Data;
      using System.Data.SqlClient;
    
      class ArbitraryClassName {  
    	  protected void GoForm(object Sender, EventArgs e){
    		 string sUser = username.Text;
    		 string sPass = pword.Text;
    
    		 string SQL = "SELECT admAccessLevel FROM adminTable WHERE admUsername = '"+ sUser +"'";
    		 lblMessage.Text = SQL;
    
    		 SqlConnection conn = new SqlConnection("Data Source=my.sql.server.com;Initial Catalog=nico;User ID=user;Password=pass");
    
    		 try {
    			conn.Open();
    
    			SqlCommand cmd = new SqlCommand(SQL, conn);
    			int AdminAccessLevel = cmd.ExecuteScalar();
    			lblMessage.Text += "<br />Access Level is: " + AdminAccessLevel;
    		 }
    		 finally {
    			conn.Close();
    		 }
    	  }
      }
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Could you post the error? exception message?

    Comment

    Working...