B
Brad Baker
I'm completely new to ASP.NET programming, so please accept my apologies in
advance for asking what is probably an obvious question.
I'm trying to write a page which will display the contents of an SQL record.
I've added the following code to a file called default.aspx:
<html>
<head>
<title>My sample application </title>
</head>
<body>
<p> Output a database record:</p>
<%@ Page Language="C#" Debug="true" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
SqlConnection objConn = new SqlConnection("Server=servername;
Database=database; UId=username; Pwd=password");
objConn.Open();
string strQuery = "select * from tablename where field =
'value'";
SqlCommand objCmd = new SqlCommand(strQuery, objConn);
objCmd.CommandType = CommandType.Text;
SqlDataReader objDR = objCmd.ExecuteReader();
objDR.Read();
Response.Write(objDR["field"] + "<br>");
objConn.Close();
}
</script>
<p>Thus ends my query. </p>
</body>
</html>
The code executes properly and produces the expected results, however its
printing those results at the top of the page before any of the html. (Even
though the code is sandwiched between html code)
What am I doing wrong?
Thank You,
Brad
advance for asking what is probably an obvious question.
I'm trying to write a page which will display the contents of an SQL record.
I've added the following code to a file called default.aspx:
<html>
<head>
<title>My sample application </title>
</head>
<body>
<p> Output a database record:</p>
<%@ Page Language="C#" Debug="true" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
SqlConnection objConn = new SqlConnection("Server=servername;
Database=database; UId=username; Pwd=password");
objConn.Open();
string strQuery = "select * from tablename where field =
'value'";
SqlCommand objCmd = new SqlCommand(strQuery, objConn);
objCmd.CommandType = CommandType.Text;
SqlDataReader objDR = objCmd.ExecuteReader();
objDR.Read();
Response.Write(objDR["field"] + "<br>");
objConn.Close();
}
</script>
<p>Thus ends my query. </p>
</body>
</html>
The code executes properly and produces the expected results, however its
printing those results at the top of the page before any of the html. (Even
though the code is sandwiched between html code)
What am I doing wrong?
Thank You,
Brad