ged125
asked on
C# Accessing SQL DB From Within the Page
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.
THanks!
I'm a newbie, be gentle.
THanks!
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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");
db1.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select * from table", db1);
using (System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.read())
{
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Unless you need to do something really special you should use a gridview, datalist, or datarepeater to display the contents of the table.
ASKER
Can you provide specific code samples?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!