Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

What's the issue with this piece of code? The data table is not filling up.... why?

C#
SqlConnection con = new SqlConnection(Application["con"].ToString());
        con.Open();
        SqlDataAdapter ad = new SqlDataAdapter("SELECT  *   FROM    Students", con);
        DataTable dtbl = new DataTable();
        ad.Fill(dtbl);
        gdvStudents.DataSource = dtbl;
        gdvStudents.DataBind();
        con.Close();

Thanks in advance
Posted
Comments
S.P.Tiwari 18-May-12 5:18am    
is you connection is ok..?
aswathy.s.88 18-May-12 5:19am    
Yea ... Connection works fine ...
Adersh Ram 18-May-12 5:20am    
Is your query is ok
S.P.Tiwari 18-May-12 5:28am    
check you table name. if its ok then be sure you are not referring the data table(clear or null) anywhere else.if its not solve your issue then put your whole page code.(if possible)

Your code seems to be correct. Cannot tell you why without debugging it line by line

Here.. Try this. It works for me

C#
//Connection string to the server
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = "Password=sasasasas;Persist Security Info=True;User ID=sa;Initial Catalog=sasasas;Data Source=sasasas;";
        //This doe sthe task of fetching data from sql server
        SqlDataAdapter adp = new SqlDataAdapter();
        //Create the command object
        SqlCommand command=new SqlCommand();
        command.CommandText="SELECT * FROM Students";
        //Inline query or stored procedure
        command.CommandType = CommandType.Text;
        command.Connection = connection;
        adp.SelectCommand = command;
        //Open the connedction to sql server
        connection.Open();

        //Fill to datatable
        DataTable dt=new DataTable();
        adp.Fill(dt);
        connection.Close();

        GridView1.DataSource = dt;
        GridView1.DataBind();
 
Share this answer
 
v2
Comments
aswathy.s.88 18-May-12 5:28am    
@Birajdar

Me too don't know what's happening ... The code u pasted here also not working ... Same error I get that Data Table is Null .....
sjelen 18-May-12 5:43am    
Are you sure you're looking at the right place? You have here:
DataTable dt=new DataTable();
so DataTable is not null. Do you have another DataTable elsewhere in code?
Error message usually contains a line number where the error happen.
bbirajdar 18-May-12 7:23am    
Did you debug it line by line? What is the error you are getting? Also check that the students table contains data.
C#
connection.ConnectionString = "Password=sasasasas;Persist Security Info=True;User ID=sa;Initial Catalog=sasasas;Data Source=sasasas;";
maybe you can,t connecting SQL
 
Share this answer
 
v2
Comments
S.P.Tiwari 18-May-12 5:39am    
heee heee. see the comment he already told that connection is working fine.:)
aswathy.s.88 18-May-12 5:41am    
What goes wrong is I can populate the grid with AutoGenerate = "True" Columns ... But I can't use the Template Field .... There I get error
sjelen 18-May-12 5:47am    
in that case you probably got the column name wrong. Can you put your TemplateField markup here?
bbirajdar 18-May-12 7:37am    
But your question and the code pasted was for the issue regarding the datatable not being filled. Make sure you are asking the right question.
Problem was with %#DataBinder.Eval(Container.DataItem, "Student_Name")%>

I missed ".DataItem" in my code (%#DataBinder.Eval(Container, "Student_Name")%> )
Now it works fine ...

Thanks all of you for the help....
 
Share this answer
 
Comments
S.P.Tiwari 18-May-12 6:01am    
:8
bbirajdar 18-May-12 7:38am    
But your question and the code pasted was for the issue regarding the datatable not being filled. Next time make sure you are asking the right question.

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