Solved

GridView not showing data

Posted on 2006-07-16
5
223 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:
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
Error on link 14 40
System.Speech 2 17
Code works but it's slow 24 45
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

27 Experts available now in Live!

Get 1:1 Help Now