473,396 Members | 1,766 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,396 software developers and data experts.

display SqlDataReader values bound to labels MSSQL C#

3
I have very limited knowledge on asp .net using c#. I am trying to fetch data from my mssql database and display the value on my page. I have succeeded to display it using datagrid. But I want it to diplay attached to labels or text boxes.

Below is the code that I have made but it is throwing error

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="c#" %>
  2. <%@ import Namespace="System.Data" %>
  3. <%@ import Namespace="System.Data.SqlClient" %>
  4. <script runat="server">
  5.  
  6.     void Page_Load()
  7.     {
  8.             string connectionStr =
  9. "server=63.45.234.23;uid=MSSqldbdbdb;pwd=password;database=MSSqldbdbdb";
  10.             string queryStr = "SELECT * FROM dibs_test";
  11.  
  12.             SqlConnection connectObj = new
  13. SqlConnection(connectionStr);
  14.             SqlCommand commandObj = new
  15. SqlCommand(queryStr,connectObj);
  16.  
  17.             SqlDataReader readerObj;
  18.  
  19.             connectObj.Open();
  20.             readerObj=commandObj.ExecuteReader();
  21.  
  22.             // display datareader contents into html table
  23.            // first open the table and set up the table headers
  24.            html += "<table cellspacing=1 class='data' width=90%>";
  25.            html += "<tr>";
  26.            html += "<th>Customer ID</th>";
  27.            html += "<th>Company Name</th>";
  28.            html += "</tr>";
  29.  
  30.            // loop thru the reader
  31.            while ( readerObj.Read ( ) ) {
  32.               html += "<tr>";
  33.               html += "<td>" + readerObj.GetInt ( 0 ) + "</td>";
  34.               html += "<td>" + readerObj.GetString ( 1 ) + "</td>";
  35.               html += "</tr>";
  36.            }
  37.  
  38.            // close the table
  39.            html += "</table>";
  40.     }
  41.  
  42. </script>
  43. <html>
  44. <head>
  45.     <title>Record</title>
  46. </head>
  47. <body>
  48.     <h2>Use DataReader to display table with variables not using datagrid
  49.     </h2>
  50.     <%=html%>
  51. </body>
  52. </html>
  53.  
  54. </html>
  55.  
  56.  
Apr 15 '10 #1
3 6199
Frinavale
9,735 Expert Mod 8TB
It would really help if you stated what the error message was saying ;)
I thought you said you were using a "DataGrid" to display your data? But looking at your code it's clear that you are not using a "DataGrid" at all! You're dynamically creating an HTML table based on the data retrieved from the database.

There is an easier (better?) way of doing this.
Use the GridView control instead.

So, after you have retrieved the data from the database, instead of dynamically creating the table, set the GridView's DataSource property to the table retrieved and use the GridView.DataBind() method to bind the data to the grid.

In the GridView, you can specify how the data is displayed.
You do this using TemplateFields and other types of "columns".
You can use labels in the template to make sure that the data is displayed exactly the way you want it to.

-Frinny
Apr 15 '10 #2
dibwas
3
Hii,

Thanks for replying.

I have used datagrid view... and that works correct..

I didn't knew I could design a datagrid.. so I was trying to generate HTML code instead.

Below is the code with datagrid that worked.

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="c#" %>
  2. <%@ import Namespace="System.Data" %>
  3. <%@ import Namespace="System.Data.SqlClient" %>
  4. <script runat="server">
  5.  
  6.     void Page_Load()
  7.     {
  8.             string connectionStr =
  9. "server=63.45.234.23;uid=MSSqldbdbdb;pwd=password;database=MSSqldbdbdb";
  10.             string queryStr = "SELECT * FROM dibs_test";
  11.  
  12.             SqlConnection connectObj = new
  13. SqlConnection(connectionStr);
  14.             SqlCommand commandObj = new
  15. SqlCommand(queryStr,connectObj);
  16.  
  17.             SqlDataReader readerObj;
  18.  
  19.             connectObj.Open();
  20.             readerObj=commandObj.ExecuteReader();
  21.  
  22.             dataGridControl.DataSource = readerObj;
  23.  
  24.             dataGridControl.DataBind();
  25.  
  26.     }
  27.  
  28. </script>
  29. <script language="javascript">
  30. alert("dibs");
  31. </script>
  32. <html>
  33. <head>
  34.     <title>Visualizzo i record</title>
  35. </head>
  36. <body>
  37.     <h2>Uso DataReader per visualizzare il contenuto di una tabella 
  38.     </h2>
  39.     <asp:DataGrid id="dataGridControl" runat="server"></asp:DataGrid>
  40. </body>
  41. </html>
  42.  
Can you provide some simple steps to display image, links, designed text ... etc with data fetched from database and displayed into datagrid.


Thanks

Dibs
Apr 15 '10 #3
Frinavale
9,735 Expert Mod 8TB
Instead of relying on the DataGrid to determine what columns to display you manually add the columns. To do this you need to use the TemplateField class this lets you specify controls that are bound to your data source so that you can display the data the way you want to.

It has been a very long time since I've used a DataGrid so I'm not even sure if the TemplateField works with this control.

Why are you using the DataGrid when the GridView is the updated, new-and-improved version that has been around for many years now?

-Frinny
Apr 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
1
by: Adam | last post by:
I am having difficulty retrieving images from a SQL database. Here is the code I use to UPLOAD the image to the database: <form id="Form1" method="post" enctype="multipart/form-data"...
1
by: Gary Frank | last post by:
I added a couple of labels and a button to a Datalist control called "dlSignIn" by adding them to the Item template. The labels are bound to a dataset column. Program code gets executed in the...
3
by: Galen Somerville | last post by:
In two different Form modules FFTcls.Currfrm = Me In FFTcls class module Dim CurrForm As System.Windows.Forms.Form Public WriteOnly Property Currfrm() As System.Windows.Forms.Form...
3
by: Fendi Baba | last post by:
I have a table with a field called ProjectPhaseID, and another which shows the ProjectPhaseID and ProjectPhaseName. On the data enty form, I want to display the ProjectPhaseName as radio button...
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
3
by: rn5a | last post by:
A SqlDataReader is populated with the records from a SQL Server 2005 DB table. The records retrieved depends upon 2 conditions (the conditions depend on what a user selects in an ASPX page). If...
1
by: geckobtz | last post by:
Hi Guys, I am trying to build a page in ASP/MSsql with the aid of dreamweaver, the page contains total of 6 Questions, each question has 3 choices, of which one of the choice contains the right...
4
by: RB | last post by:
Hi there, I'm pretty new to ASP.NET, and have a "best practice" sort-of question. I'm programming for .NET 2.0, using VB.Net in Visual Studio 2002. Basically, I've got a web-page which is...
2
by: COHENMARVIN | last post by:
I have a formview control, with labels arranged in a table. The labels are bound to fields from a query. But I also want to sum up the values in the table and put them in a row of totals at the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.