473,395 Members | 1,383 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.

Read column data from MSSQL and output on page

I think I'm making a assumption about C#, that is works similar to VBscript. I'm new to C# and I believe I'm going about this bit of code the wrong way. If some one can take a look and maybe point in the right direction that would be great. I'm trying to read data from a MSSQL database/table and display one of the columns on the page. Of course, I have removed the names for security.


Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="somepage.aspx.cs" Inherits="xxx" %>
  2.  
  3. <%@ Import Namespace="System.Data" %>
  4. <%@ Import Namespace="System.Data.SqlClient" %>
  5. <%@ Import Namespace="System.Configuration" %>
  6. <%@ Import Namespace="System.Xml" %>
  7.  
  8.  
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10.  
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head runat="server">
  13.     <title></title>
  14. </head>
  15. <%
  16.     SqlConnection con = null;
  17.     SqlCommand cmd = null;
  18.     SqlDataReader rd = null;
  19.  
  20.     con = new SqlConnection("server=xxx.xxx.xxx.xxx; database=namehere;user=userid;password=password");
  21.     cmd = new SqlCommand("SELECT * FROM tablename", con);
  22.  
  23.     con.Open();
  24.     rd = cmd.ExecuteReader();
  25.     rd.Read();
  26. %>
  27.  
  28.     <table><tr><td>Name:<%Response.Write(rd.GetString(1));%></td></tr></table>
  29.  
  30.     <%
  31.         rd.Close();
  32.         con.Close();  
  33.     %>
  34.  
The error I'm getting is "The name 'rd' does not exist in the current context". If I put the code together within the same tags it works fine. Ca you not break up the C# code? Should I be using VB to code this page instead? I think I just answered by own question.

Thanks

Dean
Mar 13 '10 #1
5 5114
tlhintoq
3,525 Expert 2GB
Database How-to parts 1 and 2
Database tutorial Part 1
Database tutorial Part 2
Mar 13 '10 #2
Thanks, I can follow the tutorials and like I said I can get it to work fine if all of the C# code is put together. I guess my question is can you inter-mix standard html code with C#? Like what is done with VBscript. I don't seem to see that explained in the tutorials.

Thanks
Mar 13 '10 #3
Frinavale
9,735 Expert Mod 8TB
You can but it gets messy.

There's a lot to be said about keeping your C# code in it's own file, separate from the UI component (the ASPX page). I'm not going to get into it right now.

What exactly are you trying to do?
Do you want to add dynamic JavaScript to your page?
If so check out How to use JavaScript in ASP.NET

If you trying to do database related stuff then check out the links previously posted.

-Frinny
Mar 16 '10 #4
I'm building a timesheet app and I need to fill in textboxes with the table data so the user can update it. I'd like to display the data in a table, hence the html. Is there a different avenue I should pursue? I would include all the code together and build the table with "Response.Write", but there are dropdown controls in the table that are being filled with XML files.

Thanks for the help
Mar 17 '10 #5
Frinavale
9,735 Expert Mod 8TB
Don't use Response.Write (especially do not use it in your C# code). I understand why people who come from a classic ASP background are so attached to it but in ASP.NET it is rarely needed.

What you're trying to do is pretty straight forward. Use something like the GridView control which will bind to data that you retrieve from you database and automatically creates the grid/table (it's actually rendered as an HTML table) for you.

The GridView has a lot of features that will help you accomplish your task. Like editing, paging, etc. It also lets you specify how your data should be displayed so that if you want to use a RadioButtonList or a DropDownList in the table to display the data you can (you would use a TemplateField for this task) .

It takes some getting used to. I recommend using MSDN Library to search for any questions you have on the topic before asking here.

Cheers!

-Frinny
Mar 17 '10 #6

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

Similar topics

3
by: chunman | last post by:
When I restart the machine, the sql 2000 database files become "Read-Only". In the Enterprise Manager this DB is marked as "suspect". After I remove "read only" attribute form the files, and...
1
by: SS | last post by:
I had a databases running on SQL Server (EVALUATION VERSION). I moved it to another system running: -Windows 2000 Server -IIS 5.0 -BlueDragon Server -MSDE I applied all of the latest...
6
by: python1 | last post by:
I apologize if this is redundant. How would one fill an empty column with a sequence of numbers? The column exists in a table with aproximately 1000000 rows of data. I believe in oracle the...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
2
by: Danko Greiner | last post by:
I'm trying to update every table in DataSet, but getting inserted "id_user" gets me an exception: column "id_user" is read only if(_dataSet.HasChanges()) { SqlDataAdapter _adapter;...
10
by: D. Shane Fowlkes | last post by:
I have a function that is called in page_load and the purpose of this function is to look up basic data in a MSSQL table and return it in the form of a datatable. The page_load will read the data...
8
by: eugenio | last post by:
Hi...not sure if this is the right group for this posting, but i'm don't know where else to post. I've got a simple problem...I have a linux box running apache 2.0 and php5. I'm trying to use the...
14
by: guswebb | last post by:
Hi. I'm a newbie to PHP and am having a few problems as follows... I have installed PHP successfully on server 1 which is running IIS 6 (W2k3) and hosting multiple sites, some of which connect to...
4
by: pks83 | last post by:
In MSSQL I am using datetime as the column type and trying to execute the below mentioned query select time from tickets where ticket_id = '1'; when i execute the query on sql manager window it...
0
by: JRough | last post by:
Hi, I have a page with a form that is included into a php page. It has some php variables which are headers based on the user choices. In the form I will send some mysql data columns but right...
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.