Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
in my application i am retrive data from sql.in database contain 6 record but when i run page it display only 1 st record and rest 5 record not display. below i m write code .

protected void Page_Load(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection("Data Source=Aarambh;Initial   Catalog=rebuild_technology;Integrated Security=True");
       SqlCommand cmd = new SqlCommand("select * from content_managment where page_id = 1 ", con);
       DataSet ds = new DataSet("temp");
       SqlDataAdapter ad = new SqlDataAdapter();

       ad.SelectCommand = cmd;
       ad.Fill(ds);
       DataRow dr = ds.Tables[0].Rows[0];
       Session["divhtml_heading1"] = dr["divhtml_heading"].ToString();
       Session["divhtml_heading2"] = dr["divhtml_content"].ToString();

   }
Posted

You've told it to only retrieve 1 record in your SQL statement...(unless you have 6 records with a page id of 1?)

SQL
select * from content_managment where page_id = 1 


Also, you are only taking the first row from the results and doing something with that...

DataRow dr = ds.Tables[0].Rows[0];

You would need to iterate over the Rows collection, or bind to a grid - depends what you are trying to do really.

e.g.

C#
foreach(DataRow row in ds.Tables[0].Rows)
{
    // do something with each row in the results...
} 
 
Share this answer
 
v2
Comments
Manohar Khillare 14-Jun-12 4:30am    
can anyone write proper code to retrive all data from sql
[no name] 14-Jun-12 4:51am    
Try now.. I improved the answer. A little mistake was there. :)
select * from content_managment
 
Share this answer
 
Check your code here
C#
DataRow dr = ds.Tables[0].Rows[0];

You are retrieving only 1st row from the dataset. Please loop through the dataset like below:

C#
if(ds.Tables[0].Rows.Count > 0)
{
    foreach(DataRow dr in ds.Tables[0])
    {
        string strHeading = Convert.ToString(dr["divhtml_heading"]);
        string strContent = Convert.ToString(dr["divhtml_content"]);
    }
}


Also check the query you are running in the database if it really returns 6 records. I am asking this as you have mentioned a condition in your query,

SQL
select * from content_managment where page_id = 1
 
Share this answer
 

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