473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 5460
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David | last post by:
I am retreiving information from a database and it works ok, but its rather a lot and I like to limit each web page to 10 rows of data, then put the next ten on a new web page, probably with a...
7
by: Robert | last post by:
I have a php/mysql query working like so: $Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%' " All I want to do now is sort them alphabetically. By using the above...
6
by: Francisco | last post by:
I have this question: I have a simple search to a db, something like: "select description from games where year = '1990'" suppose I get 300 results, I would like to display this in pages of 30...
2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
7
by: Joe | last post by:
I am using Access 2003 and are linking to an Oracle 9i ODBC datasource (using Oracle ODBC drivers). After linking the tables in Access, I inspect the data contained in the linked tables. For...
2
by: NasirMunir | last post by:
I have created a table in access (copied from excel). Then I created a form which contains a text field and a list box. The text field is actually a look-up field, where a user can enter a searchable...
10
by: Lloyd Harold | last post by:
I'm very new to PHP and attempting to put together a simple script for retrieving MySQL data of personal records. The MySQL table I'm using consists of: 0: id 1: name 2: location (an integer...
2
by: chazzy69 | last post by:
I having trouble displaying information i have retrieved from an mysql database, i succesfully execute the SELECT query as shown below- $sql = "SELECT PCODE,LOCALITY,STATE FROM `postcode` WHERE...
3
by: Nightcrawler | last post by:
I have a website that does the following: 1. it accepts a keyword through a textbox in the UI 2. once the submit button is clicked it goes out and spiders a few websites using the keyword...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.