?
Solved

GridView not showing data

Posted on 2006-07-16
5
Medium Priority
?
230 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

839 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