Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlCommand cmd = new SqlCommand("Select _name from Computers", con);
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
DA.Fill(dt);
for (int i = 0; i <dt.rows.count;>
{
   GridView2.Rows[0].Cells[1].Text = dt.Rows[1]["_name"].ToString();
}

below is the error(Gridview is throwing the error) -----
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index 

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.Collections.ArrayList.get_Item(Int32 index) +7492824
   System.Web.UI.WebControls.GridViewRowCollection.get_Item(Int32 index) +13
   _Default.Butto   n3_Click(Object sender, EventArgs e) in c:\Users\vn73662\Desktop\MyTest\Dashboard.aspx.   cs:78
   System.Web.UI.WebControls.Button.On    Click(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13      System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Posted
Updated 13-Sep-14 13:49pm
v2

Just because the data table has rows and cells, why do you assume that the grid view also does?
GridView2.Rows[0].Cells[1].Text = dt.Rows[1]["_name"].ToString();
 
Share this answer
 
try this::
C#
SqlCommand cmd = new SqlCommand("Select _name from Computers", con);
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
DA.Fill(dt);

GridView2.Datasource = dt;
GridView2.Databind();
 
Share this answer
 
v2

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