Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a asp gridview on a page
VB
<asp:GridView ID="WebDataGrid1" runat="server" CellPadding="4" AutoGenerateColumns="false" ></asp:GridView>


I am trying to bind it to a datasource in code behind

VB
OracleDataAdapter da = new OracleDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            WebDataGrid1.DataSource = dt;
            WebDataGrid1.DataBind();


But on the line
C#
WebDataGrid1.DataSource = dt;

it gives the error "Object reference not set to an instance of an object".

while debugging the dt has values (10 records). But I can't understand why it is giving me this error.

Even though i have a gridView on my aspx page, on the code behind it shows "null" at runtime.- This is the cause of the problem.

What could be the solution?

please help.
Posted
Updated 16-Jul-13 3:07am
v2

hey pre check that your data table has records or not then assign to gridview then it will not work

F#
OracleDataAdapter da = new OracleDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            
        if(dt.Rows.Count>0)
               {
                     WebDataGrid1.DataSource = dt;
                     WebDataGrid1.DataBind();
               }
          else
               {
                     WebDataGrid1.DataSource = null;
                     WebDataGrid1.DataBind();

               }
 
Share this answer
 
v2
Comments
maverick12131 16-Jul-13 8:59am    
dt has 10 records. I already checked that
maverick12131 16-Jul-13 9:03am    
also the WebDataGrid1 is showing "null" while debugging.
Hi Pleas try this,

OracleDataAdapter da = new OracleDataAdapter(cmd);
            Dataset ds = new Dataset();
            da.Fill(ds);
 
            WebDataGrid1.DataSource = ds;
            WebDataGrid1.DataBind();
 
Share this answer
 
Comments
maverick12131 16-Jul-13 9:16am    
what difference will it make anyway

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