Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

aspx page using c#

$
0
0

Hi,

I'm a novice at this, not sure if this is the most appropriate forum so apologies if not.

I am trying to build a string in a web page on sharepoint 2010 using output from a query made against an sql database. I have the following code below,  I have set up a trace on the sql database and I can see the login happens and the right query executes.  What doesn't seem to be happening is the data doesn't seem to load into my dataTable.  I've tried altering the code  as described here http://stackoverflow.com/questions/7809320/trouble-loading-sql-data-reader-data-into-datatable using an sqlAdapter but I get an error [].

Can anyone point out to me where i am going wrong?

<%@ page language="C#" %><%@ Import Namespace="System.Data.SqlClient" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System" %><script runat="server"> //Create a data table object DataTable dataTable = new DataTable(); public class ParamDemo { public static void Main() { // create connection and reader variables SqlConnection conn = null; SqlDataReader reader = null; //Create a data table object DataTable dataTable = new DataTable(); try { // instantiate and open connection conn = new SqlConnection("Server=myServer;Database=myDB;User Id=User;Password=Pass"); conn.Open(); // 1. declare command object SqlCommand cmd = new SqlCommand("select top 2 dateSid, fullDate from myDB.dbo.dim_Date where dateName = 'Jan 1 2013'", conn); // get data stream reader = cmd.ExecuteReader(); //Record datastream in datatable dataTable.Load(reader); } finally { // close reader if (reader != null) { reader.Close(); } // close connection if (conn != null) { conn.Close(); } } } }</script><% ParamDemo.Main(); foreach (DataColumn column in dataTable.Columns){ Response.Write("It has worked"); } %> SomeOtherHTMLOutput

Thanks



Viewing all articles
Browse latest Browse all 31927

Trending Articles