Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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




C#
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();
       
        
       
    }



and in htmlpage i use

XML
<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]Code block extended to cover both methods - OriginalGriff[/edit]
Posted
Updated 20-Jun-11 6:26am
v5

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]
 
Share this answer
 
v2
Comments
Monjurul Habib 19-Jun-11 18:32pm    
nice solution, my 5.
Ahsan Mirza 20-Jun-11 2:27am    
good solution, 5 from me!
SREENATH GANGA 20-Jun-11 12:09pm    
NO FRIEND THIS SOLUTION IS NOT WORKING
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:

XML
<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>
 
Share this answer
 
v3
Comments
SREENATH GANGA 21-Jun-11 12:22pm    
dt contains 3 rows but it is not being filled on data grid
Anupama Roy 22-Jun-11 1:16am    
Add DataGrid1.DataBind(); after you set the datasource
SREENATH GANGA 22-Jun-11 11:43am    
I had already done it
Anupama Roy 22-Jun-11 13:22pm    
ya right,dint notice that.Just had a look at html & seems like you haven't assigned text property.check it out with the changed html & let me know.
SREENATH GANGA 24-Jun-11 11:37am    
thank you it solved my problem very very thankssssssssssssss

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900