Lahib
Lahib

Reputation: 1365

GridView is not visible on page

I'm trying to fill a Gridview with data from a database view. I cant use linq because the view has 200+k rows i have to show. here is my code:

     public partial class _Default : System.Web.UI.Page
{
    private string mobileGateway = "MobileGateway";
    private List<string> addressReport = new List<string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        GetReport(addressReport);
    }

    public void GetReport(List<string> adr)
    {
        string connecntionString = ConfigurationManager.ConnectionStrings[mobileGateway].ConnectionString;

        using (SqlConnection connection = new SqlConnection(connecntionString))
        {
            try
            {
                connection.Open();
                string sqlCmd = "SELECT * from dbo.BarcodeWithLocation";
                using (SqlCommand command = new SqlCommand(sqlCmd, connection))
                {
                    command.CommandType = System.Data.CommandType.Text;
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            adr.Add("" + reader[0]);
                            adr.Add("" + reader[1]);
                            adr.Add("" + reader[2]);
                            adr.Add("" + reader[3]);
                            adr.Add("" + reader[4]);
                            adr.Add("" + reader[5]);
                            adr.Add("" + reader[6]);
                            adr.Add("" + reader[7]);
                            adr.Add("" + reader[8]);
                            adr.Add("" + reader[9]);
                            adr.Add("" + reader[10]);
                        }

                        Grid.DataSource = adr;
                        Grid.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR!!!!: " + ex.Message);
            }
        }
    }
}

}

aspx code:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="AddressReporting._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="Grid" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False">
</asp:GridView>
</asp:Content>

I get a blank page with no gridview at all. what am i doing wrong ?

Upvotes: 0

Views: 713

Answers (4)

Charles Wesley
Charles Wesley

Reputation: 829

Try something like this:

string sqlCmd = "SELECT * from dbo.BarcodeWithLocation";
using (SqlCommand command = new SqlCommand(sqlCmd, connection))
{
     DataTable dataTable = new DataTable();
     command.CommandType = System.Data.CommandType.Text;
     connection.Open();
     SqlDataAdapter dataAdapter = new SqlDataAdapter(command);

     dataAdapter.Fill(dataTable);

     Grid.DataSource = dataTable;
     Grid.DataBind();
}

Upvotes: 1

paparazzo
paparazzo

Reputation: 45106

Try declaring ref

public void GetReport(ref List<string> adr)

ref (C# Reference)

Upvotes: 0

LearningAsp
LearningAsp

Reputation: 361

Check whether you query is fetching data or not as you can't see empty GridView

Upvotes: 0

Richa Saksena
Richa Saksena

Reputation: 21

enter code here

command.CommandType = System.Data.CommandType.Text;
SqlDataReader reader = command.ExecuteReader();
Grid.DataSource = reader;
Grid.DataBind();

try this it will get all the details into reader and bind it to grid directly. No need to loop through all the rows unless you want to do something else there.

Upvotes: 2

Related Questions