468,133 Members | 1,574 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Displaying SQL Query Results

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


Apr 9 '06 #1
5 5149
Brad Baker wrote:
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";

--

Riki
Apr 9 '06 #2
Brad you can also bind it to a control like datagrid,datalist repeater etc.
depending what you need.
Patrick

"Brad Baker" <br**@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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

Apr 9 '06 #3
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" <na********@hotmail.com> wrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
Brad you can also bind it to a control like datagrid,datalist repeater
etc.
depending what you need.
Patrick

"Brad Baker" <br**@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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


Apr 9 '06 #4
Actually a repeater is just what I wanted. Thanks for the help! :-)

Brad
"Brad Baker" <br**@nospam.nospam> wrote in message
news:uj****************@TK2MSFTNGP04.phx.gbl...
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" <na********@hotmail.com> wrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
Brad you can also bind it to a control like datagrid,datalist repeater
etc.
depending what you need.
Patrick

"Brad Baker" <br**@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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



Apr 9 '06 #5
You are welcome brad
Patrick

"Brad Baker" <br**@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Actually a repeater is just what I wanted. Thanks for the help! :-)

Brad
"Brad Baker" <br**@nospam.nospam> wrote in message
news:uj****************@TK2MSFTNGP04.phx.gbl...
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" <na********@hotmail.com> wrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
Brad you can also bind it to a control like datagrid,datalist repeater
etc.
depending what you need.
Patrick

"Brad Baker" <br**@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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




Apr 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by David | last post: by
6 posts views Thread by Francisco | last post: by
2 posts views Thread by jaysonsch | last post: by
10 posts views Thread by Lloyd Harold | last post: by
3 posts views Thread by Nightcrawler | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.