Solved

GridView not showing data

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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