Solved

GridView not showing data

Posted on 2006-07-16
5
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 500 total points
ID: 17119931
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
ID: 17122707
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
ID: 17122713
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
ID: 17123023
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
ID: 17123891
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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