Displaying SQL Query Results

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
 
R

Riki

Brad said:
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? :)

Don't use Response.Write in Page_Load. As a matter of fact, don't
use Response.Write at all. This is old style ASP programming.

In ASP.NET, you should put a label control on the page, and use
Label1.Text="whatever";
 
P

Patrick.O.Ige

Brad you can also bind it to a control like datagrid,datalist repeater etc.
depending what you need.
Patrick
 
B

Brad Baker

Hmm... thanks for the suggestions. Although I don't really want to output
the results as a button label.

I don't think I want a data grid or a data list repeater either? (Although
I'm not completely sure what either of those are)

All I want to do is output the contents of one record and put it in a table.
For instance:

<table>
<tr><td>Announcement</td></tr>
<tr><td> <% PRINT MY RECORD HERE %> </td></tr>
<table>

Would I want to do that with a data list repeater even though I am sure
there there will only be one record?

Thanks Again,
Brad



Patrick.O.Ige said:
Brad you can also bind it to a control like datagrid,datalist repeater
etc.
depending what you need.
Patrick



Brad Baker said:
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
 
B

Brad Baker

Actually a repeater is just what I wanted. Thanks for the help! :)

Brad


Brad Baker said:
Hmm... thanks for the suggestions. Although I don't really want to output
the results as a button label.

I don't think I want a data grid or a data list repeater either? (Although
I'm not completely sure what either of those are)

All I want to do is output the contents of one record and put it in a
table. For instance:

<table>
<tr><td>Announcement</td></tr>
<tr><td> <% PRINT MY RECORD HERE %> </td></tr>
<table>

Would I want to do that with a data list repeater even though I am sure
there there will only be one record?

Thanks Again,
Brad



Patrick.O.Ige said:
Brad you can also bind it to a control like datagrid,datalist repeater
etc.
depending what you need.
Patrick



Brad Baker said:
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
 
P

Patrick.O.Ige

You are welcome brad
Patrick

Brad Baker said:
Actually a repeater is just what I wanted. Thanks for the help! :)

Brad


Brad Baker said:
Hmm... thanks for the suggestions. Although I don't really want to output
the results as a button label.

I don't think I want a data grid or a data list repeater either?
(Although I'm not completely sure what either of those are)

All I want to do is output the contents of one record and put it in a
table. For instance:

<table>
<tr><td>Announcement</td></tr>
<tr><td> <% PRINT MY RECORD HERE %> </td></tr>
<table>

Would I want to do that with a data list repeater even though I am sure
there there will only be one record?

Thanks Again,
Brad



Patrick.O.Ige said:
Brad you can also bind it to a control like datagrid,datalist repeater
etc.
depending what you need.
Patrick



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top