troubleshooting Question

C# Accessing SQL DB From Within the Page

Avatar of ged125
ged125Flag for United States of America asked on
Microsoft SQL Server.NET ProgrammingC#
4 Comments1 Solution287 ViewsLast Modified:
In regards to the page listed below, I have written a script in C# to open a connection to my database.  How do I put the contents of the table that I am accessing in the body of the ASP page?  When I get into <Body> section, I have no access to the "rdr" pr "db1".

I'm a newbie, be gentle.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
    public void Test(object sender)
        System.Data.SqlClient.SqlConnection db1 = new System.Data.SqlClient.SqlConnection("Data Source=SQL;Initial Catalog=Web;User ID=WebUser;Password=password");
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select * from table", db1);
        using (System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader())
            while (
<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros