Johnny Doe
Johnny Doe

Reputation: 193

Why won't C# show the table

I'm using Visual Web Developer 2010 Express and SQL Server 2008 R2 Management Studio Express

Hey guys,

Pretty new at C# here. I'm trying to follow this C# ADO.NET tutorial (currently on step 2), and I'm stumped. I'm following all the steps, and everything makes sense to me in it, but whenever I try to debug, it does not show anything (in the sense of the c# methods not printing out a table from Northwind database onto my webpage) in WebApplication1's Default.aspx page.

For a while, I thought it was my connection string, conn, and I wasn't naming the "Data Source" attribute, which from my understanding is the name of the server I'm trying to connect to. It is all on a local machine, and I'm putting the correct server name.. I think. Server name is AZUES-221\JDOESQLSERVER

I'm properly escaping the backward slash, but I still don't know. Is there something in my coding that's flawed? Please help!

C# code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace WebApplication1
{
    public partial class SqlConnectionDemo : System.Web.UI.Page
    {


        protected void Main(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=AZUES-221\\JDOESQLSERVER; Initial Catalog=Northwind; Integrated Security=SSPI");

            SqlDataReader rdr = null;

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn); //passed the connection

                rdr = cmd.ExecuteReader(); // get query results

                while (rdr.Read()) //prints out whatever was
                { Console.WriteLine(rdr[0]); }//selected in the table
            }

            finally
            {
                if (rdr != null)// closes
                { rdr.Close(); }// the reader

                if (conn != null)//closes
                { conn.Close(); }// the connection

            }
        }
    }
}

Thanks in advance

Upvotes: 2

Views: 311

Answers (3)

Kash
Kash

Reputation: 9039

Create a Console application instead of the Web Application you have created. Otherwise you will run into similar issues considering you are new to C# (or Visual Studio in general) AND considering the rest of the tutorial uses Console.WriteLine heavily.

Then you can use the same code as shown in the tutorial.

Console Application from Visual Studio 2008

Additonally if you are concerned about the slash in the database server (it is a database server instance), you may wanna try this:

    SqlConnection conn = new SqlConnection(@"Server=AZUES-221\JDOESQLSERVER;Database=Northwind;Trusted_Connection=True;");

Source: Connection Strings Reference

Upvotes: 1

Sarim Sidd
Sarim Sidd

Reputation: 2176

why would console.writeline show anything. you are not working on console.

in case just to see your output. use Response.writeline(rdr[0]);

Upvotes: 0

Pilgerstorfer Franz
Pilgerstorfer Franz

Reputation: 8359

As your example seems to be a WebProject try to put your code within Page_Load eventHandler. Afterwards you should try to print your data to the Debug window or to a control within your webPage.

using System;
using System.Data;
// and all the others ...

namespace WebApplication1
{
  public partial class SqlConnectionDemo : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Data Source=AZUES-221\\JDOESQLSERVER; Initial Catalog=Northwind; Integrated Security=SSPI");
        SqlDataReader rdr = null;

        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn); 
            rdr = cmd.ExecuteReader(); // get query results

            while (rdr.Read()) //prints out whatever was
            { 
                System.Diagnostics.Debug.WriteLine(rdr[0]); // or on the other hand
                lblOutput.Text += rdr[0];   // as a "quick and dirty" solution!
            }
        }

        finally
        {
            if (rdr != null)// closes
            { rdr.Close(); }// the reader
            if (conn != null)//closes
            { conn.Close(); }// the connection
        }
    }
  }
}

You may it find very useful to have a look at databound controls or just use another type of project (eg winForm, console, ...)

Upvotes: 3

Related Questions