I have ttried the below code but i m getting error
post solution for this
thanks in advance
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Dec 7, 2011, 5:09 AM
Run the attachments.
Thanks
Rajkumar RPosted Dec 7, 2011, 4:17 AM
Hello Here i m getting error while binding data in the gridview
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Cannot find table 1.
Exception Details: System.IndexOutOfRangeException: Cannot find table 1.
Source Error:
Satyapriya NayakPosted Dec 6, 2011, 7:50 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = GetDataSet().Tables[1];
GridView1.DataBind();
}
}
private DataSet GetDataSet()
{
string query = @"SELECT p.CategoryID,p.ProductID, p.ProductName FROM Products p
SELECT c.CategoryID,c.CategoryName FROM Categories c";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable myTable = new DataTable();
DataColumn productIDColumn = new DataColumn("ProductID");
DataColumn productNameColumn = new DataColumn("ProductName");
myTable.Columns.Add(productIDColumn);
myTable.Columns.Add(productNameColumn);
DataSet ds = new DataSet();
ds = GetDataSet();
int categoryID = 0;
string expression = String.Empty;
if (e.Row.RowType == DataControlRowType.DataRow)
{
categoryID = Int32.Parse(e.Row.Cells[0].Text);
expression = "CategoryID = " + categoryID;
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DataRow[] rows = ds.Tables[0].Select(expression);
foreach (DataRow row in rows)
{
DataRow newRow = myTable.NewRow();
newRow["ProductID"] = row["ProductID"];
newRow["ProductName"] = row["ProductName"];
myTable.Rows.Add(newRow);
}
ddl.DataSource = myTable;
ddl.DataTextField = "ProductName";
ddl.DataValueField = "ProductID";
ddl.DataBind();
}
}
}
Thanks