Solved

GridView not showing data

Posted on 2006-07-16
5
220 Views
Last Modified: 2010-04-16
When I run my aspx page, nothing is showing.   This is running on my Win XP Pro PC to a local SQL Server 2005 DB for testing.

Here's my web.config:

<?xml version="1.0"?>

<configuration>
      <appSettings/>
      <connectionStrings>
            <add name="MyConn"
         connectionString="Data Source=localhost;
           Initial Catalog=URLs;
           Integrated Security=True"
           providerName="System.Data.SqlClient"/>
      </connectionStrings>

      <system.web>

            <authentication mode="Windows"/>

      </system.web>
</configuration>


Here's my code-behind:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using MyDataAccess;  //Class Library I created that contains GetListURLs class

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //create an instance of the GetListURLs class first
        //GetListURLs geturls = new GetListURLs(System.Configuration.ConfigurationManager.AppSettings["MyConn"]);

        GetListURLs geturls = new GetListURLs(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);

        //use the geturls class object to call GetList() which returns a dataset and use that as the DataSource for GridView

        VPPURLList.DataSource = geturls.GetList();
        VPPURLList.DataBind();
    }
}

Here's my aspx page GridView:

        <asp:GridView
            ID="VPPURLList"
            runat="server"
            AllowPaging="True"
            BackColor="White"
            BorderColor="#336666"
            BorderStyle="Double"
            BorderWidth="3px"
            CellPadding="4"
            GridLines="Horizontal"
            Width="397px">
           
            <Columns>
                <asp:TemplateField HeaderText="URL" SortExpression="URLID">
                <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                    <ItemTemplate>
                        <asp:Label runat="server" id="lblURLString" Text='<%# Bind("URLString") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
           
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#DDDDDD" />
            <SelectedRowStyle BackColor="#DDDDDD" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
 
        </asp:GridView>

Here's my class:

    public class GetListURLs
    {

       private string _ConnString;

        public GetListURLs(string connection)
        {
            this._ConnString = connection;
        }

        public DataSet GetList()
        {
            DataSet URLs = new DataSet();
            SqlConnection Conn = new SqlConnection(this._ConnString);
            try
            {

                SqlCommand GetURLsCommand = new SqlCommand("GetURLs", Conn);
                GetURLsCommand.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(GetURLsCommand);
                da.Fill(URLs);
               
                return URLs;
               
            }
            catch
            {
                return null;
            }
            finally
            {
                Conn.Close();
            }
        }
    }
}

I am getting no compile errors whatsoever during the build of any of this.
0
Comment
Question by:dba123
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
mnasman earned 500 total points
Comment Utility
Have you checked if geturls.GetList() return results or not?

also try to change your code to return DataTable instead of DataSet

for example

public DataTable GetList()
        {
            DataTable URLs = new DataTable ();
            SqlConnection Conn = new SqlConnection(this._ConnString);
            try
            {

                SqlCommand GetURLsCommand = new SqlCommand("GetURLs", Conn);
                GetURLsCommand.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(GetURLsCommand);
                da.Fill(URLs);
               
                return URLs;
               
            }
            catch
            {
                return null;
            }
            finally
            {
                Conn.Close();
            }
        }

0
 
LVL 1

Author Comment

by:dba123
Comment Utility
You can't directly fill a datatable without a dataset can you?  It just errored out on me
0
 
LVL 1

Author Comment

by:dba123
Comment Utility
geturls.GetList() is not returning results.  The class is the problem but don't know what I'm missing in it or doing wrong..probably a simple syntax issue but not seeing it.
0
 
LVL 1

Author Comment

by:dba123
Comment Utility
Ok, took out the catch statement in my class method.  

I found out that my Connection couldn't be opened wh ich is the main problem it looks like that was revealed.  I got this error in my class now because the connection isn't liking my domain login.  My domain login in SQL 2005 security/logins has db_owner on the Catalog I'm referencing:

Exception Details: System.Data.SqlClient.SqlException: Cannot open database "dbname" requested by the login. The login failed.
Login failed for user 'domain\myaccount'.

Source Error:

Line 25:             try
Line 26:             {
Line 27:                 Conn.Open();
Line 28:                 SqlCommand GetURLsCommand = new SqlCommand("GetURLs", Conn);
Line 29:                 GetURLsCommand.CommandType = CommandType.StoredProcedure;


So I wonder if maybe I have a malforned connection string in my web.config

My connection string currently is this in my web.config (I used ** for privacy purposes in the post):

      <connectionStrings>
            <add name="****"
         connectionString="Data Source=localhost;
           Initial Catalog=****;
               Trusted_Connection=Yes"
           providerName="System.Data.SqlClient"/>
      </connectionStrings>
0
 
LVL 1

Author Comment

by:dba123
Comment Utility
Finally,  I changed the connection string to use the correct SQL instance.  I didn't have it named as local in SQL 2005 Management Studio, I had it actually as pcname\LOCAL

that fixed it!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to host windows service on Windows Server 2008 3 42
Handling arrays 14 33
Launch Process with NOT as Administrator 15 65
c# code 19 56
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now