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
Exercise1.aspx. cs
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>
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();
}
}
}
Comment