C# GridView在页面上不可见
我试图用数据库视图中的数据填充Gridview。我不能使用linq,因为视图中有200+k行我必须显示。这是我的密码:C# GridView在页面上不可见,c#,asp.net,.net,C#,Asp.net,.net,我试图用数据库视图中的数据填充Gridview。我不能使用linq,因为视图中有200+k行我必须显示。这是我的密码: public partial class _Default : System.Web.UI.Page { private string mobileGateway = "MobileGateway"; private List<string> addressReport = new List<string>(); pr
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);
}
}
}
}
public分部类\u默认值:System.Web.UI.Page
{
专用字符串mobileGateway=“mobileGateway”;
私有列表地址报告=新列表();
受保护的无效页面加载(对象发送方、事件参数e)
{
GetReport(addressReport);
}
公共报告(清单adr)
{
字符串connecntionString=ConfigurationManager.ConnectionString[mobileGateway].ConnectionString;
使用(SqlConnection连接=新的SqlConnection(connecntionString))
{
尝试
{
connection.Open();
string sqlCmd=“SELECT*from dbo.BarcodeWithLocation”;
使用(SqlCommand=newsqlcommand(sqlCmd,connection))
{
command.CommandType=System.Data.CommandType.Text;
使用(SqlDataReader=command.ExecuteReader())
{
while(reader.Read())
{
adr.添加(“+reader[0]);
adr.添加(“+reader[1]);
adr.添加(“+读取器[2]);
adr.添加(“+读取器[3]);
adr.添加(“+reader[4]);
adr.添加(“+reader[5]);
adr.添加(“+reader[6]);
adr.添加(“+reader[7]);
adr.添加(“+reader[8]);
adr.添加(“+reader[9]);
adr.添加(“+reader[10]);
}
Grid.DataSource=adr;
Grid.DataBind();
}
}
}
捕获(例外情况除外)
{
Console.WriteLine(“错误!!!!:”+ex.Message);
}
}
}
}
}
aspx代码:
<%@ 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>
我得到一个完全没有gridview的空白页面。我做错了什么
enter code here
command.CommandType = System.Data.CommandType.Text;
SqlDataReader reader = command.ExecuteReader();
Grid.DataSource = reader;
Grid.DataBind();
试试这个,它将把所有细节都放到阅读器中,并直接绑定到网格中。不需要在所有行中循环,除非您想在那里执行其他操作
试试这个,它将把所有细节都放到阅读器中,并直接绑定到网格中。除非您想在所有行中执行其他操作,否则无需循环。检查您的查询是否正在获取数据,因为您看不到空的GridView检查您的查询是否正在获取数据,因为您看不到空的GridView请尝试声明ref
public void GetReport(ref List<string> adr)
public void GetReport(参考列表adr)
尝试声明ref
public void GetReport(ref List<string> adr)
public void GetReport(参考列表adr)
试试这样的方法:
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();
}
试着这样做:
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();
}
你也可以发布aspx页面吗?顺便说一句,try&catch的块是完全无用的,因此我会为
reader
类使用更安全的方法。您应该使用GetString(index)/GetInt32(index)/…(基于您正在读取的类型),而不是仅使用索引。因此,您的代码应该是:reader.GetString(0)
。我会试试看,但我不认为这能解决我的问题。你可以缓存布局吗?你能同时发布aspx页面吗?顺便说一句,try&catch的块是完全无用的,因此我会为reader
类使用更安全的方法。您应该使用GetString(index)/GetInt32(index)/…(基于您正在读取的类型),而不是仅使用索引。因此,您的代码应该是:reader.GetString(0)
。我会尝试,但不认为这会解决我的问题。你是否可以缓存布局?谢谢你的建议,但这只会优化我的代码,而不会解决我的问题:)谢谢你的建议,但这只会优化我的代码,而不会解决我的问题:)