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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 6, 2011, 2:50 AM EST
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
Rajkumar R175 reputationPosted Dec 6, 2011, 11:17 PM EST
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:
Line 29: BindGrid(); Line 30: // Line 31: GridView2.DataSource = GetDataSet().Tables[1]; Line 32: GridView2.DataBind(); Line 33: //Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 7, 2011, 12:09 AM EST
Run the attachments.
Thanks