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.
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
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="somepage.aspx.cs" Inherits="xxx" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<%
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader rd = null;
con = new SqlConnection("server=xxx.xxx.xxx.xxx; database=namehere;user=userid;password=password");
cmd = new SqlCommand("SELECT * FROM tablename", con);
con.Open();
rd = cmd.ExecuteReader();
rd.Read();
%>
<table><tr><td>Name:<%Response.Write(rd.GetString(1));%></td></tr></table>
<%
rd.Close();
con.Close();
%>
Thanks
Dean
Comment