asp.net中的数据网格不显示数据库中的任何内容 [英] Data grid in asp.net doesnt show anything from database

查看:78
本文介绍了asp.net中的数据网格不显示数据库中的任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格,并希望将其填充在onpageload()上,我使用了下面的代码,但
什么也不返回.我是初学者,不知道发生什么错误,请帮忙




I had a datagrid and want it to be filled on onpageload() I used the code below but
return nothing .I am a beginner and doesn''t know whats error please help




protected void Page_Load(object sender, EventArgs e)
   {
       databind1();
   }
protected void databind1()
    {
         String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Sree\\Documents\\study1.accdb;Persist Security Info=True";
        String query = "Select * from employetable";
        OleDbConnection ole = new OleDbConnection(connString);
        DataTable dt = new DataTable();
        ole.Open();
        OleDbDataAdapter ad = new OleDbDataAdapter(query, ole);
        ad.Fill(dt);
       
       
        
        DataGrid1.DataSource = dt;
        DataGrid1.DataBind();
       
        
       
    }




在htmlpage中,我使用




and in htmlpage i use

<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"

            CellPadding="4" ForeColor="#333333" GridLines="None" Width="863px" >
            <AlternatingItemStyle BackColor="White" />
            <Columns>
                <asp:BoundColumn HeaderText="Id"></asp:BoundColumn>
                <asp:TemplateColumn HeaderText="Emp ID">
                    <EditItemTemplate>
                        <asp:TextBox runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:BoundColumn HeaderText="L-Name"></asp:BoundColumn>
                <asp:BoundColumn HeaderText="City"></asp:BoundColumn>
                <asp:BoundColumn HeaderText="Email"></asp:BoundColumn>
                <asp:BoundColumn HeaderText="Emp_joining"></asp:BoundColumn>
            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        </asp:DataGrid>




[edit]代码块扩展为涵盖了这两种方法-OriginalGriff [/edit]




[edit]Code block extended to cover both methods - OriginalGriff[/edit]

推荐答案

如果您查看DbDataAdapter.Fill方法(数据集,字符串) [
只需从Fill方法调用中删除字符串参数,它就可能起作用.

[edit]
请注意,我也不能不认为这有点奇怪:
If you look at the OleDbDataAdaptor.Fill[^] method, for a DataSet and a string, you will see this: DbDataAdapter.Fill Method (DataSet, String)[^] which says that the string parameter is table name to use for data source mapping. Since your query only returns rows based on the "employeetable" and you are using the "product" table for your source, I would expect no rows!

Just remove the string parameter from the Fill method call, and it will probably work.

[edit]
Mind you, I can''t help thinking there is something a bit odd about this bit as well:
DataGrid1.DataSource = ds.Tables.Add().DefaultView;

您是否考虑过只填充单个数据表而不是数据集,并直接使用它?

Have you considered filling just a single datatable instead of a dataset, and using that directly?

DataTable dt = new DataTable();
ole.Open();
OleDbDataAdapter ad = new OleDbDataAdapter(query,ole);
ad.Fill(dt);
DataGrid1.DataSource = dt;


[/edit]


[/edit]


希望这会有所帮助!

1.填写记录后检查dt是否包含任何行(调试时请使用快速观察以查看该行)
2.在此内部调用您的方法
如果(!Page.IsPostBack)
{
databind1();
}

看起来您的html中缺少数据绑定标记.通过添加Text属性来更改html.bind标记内的名称应与您在dt中的列名称匹配.

例如:

Hope this helps!

1.Check if dt contains any rows after you fill the records(Use quick watch to see that while debugging)
2. call your method inside this
if (!Page.IsPostBack)
{
databind1();
}

Looks like databinding tag is missing in your html.Change html this way by adding Text property.Name inside bind tag should match with your column name in dt.

eg:

<asp:TemplateColumn HeaderText="Emp ID">
                    <EditItemTemplate>
                        <asp:TextBox runat="server" Text='<%# Bind("Emp ID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%# Bind("Emp ID") %>'></asp:Label>
                    </ItemTemplate>
                   </asp:TemplateColumn>


这篇关于asp.net中的数据网格不显示数据库中的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆