DataBinding to DropDownList in GridView Error
-
Hi, As my requirement is i want to bind data to the dropdownlist in the gridview control. when iam doing this iam getting an error. "Cannot implicit convert Type 'string' to 'int'", "Only assignment, call, increment, decrement, and new object expressions can be used as a statement",and "Cannot apply indexing with [] to an expression of type 'method group'". Please help me...My code is as follows.. using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GridView1.DataSource = GetDataSet().Tables[0]; GridView1.DataBind(); } private DataSet GetDataSet() { string connectionString = "user id=sa;password=victory;database=Northwind;server=INDUS-SERVER"; string query = "SELECT p.CategoryID, p.ProductID, p.ProductName, c.CategoryID, c.CategoryName from Products p,Categories c where p.CategoryID=c.CategoryID and c.CategoryID<2"; SqlConnection newConnection = new SqlConnection(connectionString); SqlDataAdapter adapter = new SqlDataAdapter(query, newConnection); DataSet ds = new DataSet(); adapter.Fill(ds); return ds; } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { DataTable myDataTable = new DataTable(); DataColumn productIDColumn = new DataColumn("ProductID"); DataColumn productNameColumn = new DataColumn("ProductName"); myDataTable.Columns.Add(productIDColumn); myDataTable.Columns.Add(productNameColumn); DataSet ds = new DataSet(); ds = GetDataSet(); int categoryID = 0; string exp = string.Empty; if (e.Row.RowType == DataControlRowType.DataRow) { categoryID = Int32.Parse(e.Row.Cells[0].Text); exp = "CategoryID=" + categoryID; DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1"); DataRow[] rows = ds.Tables[0].Select(exp); foreach(DataRow roe in rows) { DataRow newRow = myDataTable.NewRow(); newRow["ProductID"] = rows["ProductID"]; newRow["ProductName"] = rows["ProductName"]; myDataTable.R
-
Hi, As my requirement is i want to bind data to the dropdownlist in the gridview control. when iam doing this iam getting an error. "Cannot implicit convert Type 'string' to 'int'", "Only assignment, call, increment, decrement, and new object expressions can be used as a statement",and "Cannot apply indexing with [] to an expression of type 'method group'". Please help me...My code is as follows.. using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GridView1.DataSource = GetDataSet().Tables[0]; GridView1.DataBind(); } private DataSet GetDataSet() { string connectionString = "user id=sa;password=victory;database=Northwind;server=INDUS-SERVER"; string query = "SELECT p.CategoryID, p.ProductID, p.ProductName, c.CategoryID, c.CategoryName from Products p,Categories c where p.CategoryID=c.CategoryID and c.CategoryID<2"; SqlConnection newConnection = new SqlConnection(connectionString); SqlDataAdapter adapter = new SqlDataAdapter(query, newConnection); DataSet ds = new DataSet(); adapter.Fill(ds); return ds; } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { DataTable myDataTable = new DataTable(); DataColumn productIDColumn = new DataColumn("ProductID"); DataColumn productNameColumn = new DataColumn("ProductName"); myDataTable.Columns.Add(productIDColumn); myDataTable.Columns.Add(productNameColumn); DataSet ds = new DataSet(); ds = GetDataSet(); int categoryID = 0; string exp = string.Empty; if (e.Row.RowType == DataControlRowType.DataRow) { categoryID = Int32.Parse(e.Row.Cells[0].Text); exp = "CategoryID=" + categoryID; DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1"); DataRow[] rows = ds.Tables[0].Select(exp); foreach(DataRow roe in rows) { DataRow newRow = myDataTable.NewRow(); newRow["ProductID"] = rows["ProductID"]; newRow["ProductName"] = rows["ProductName"]; myDataTable.R
Can you tell me on which line your getting the Error ?
Parwej Ahamad R & D with IIS 5.0/6.0
-
Can you tell me on which line your getting the Error ?
Parwej Ahamad R & D with IIS 5.0/6.0
-
Iam getting the error with the following lines... DataRow newRow = myDataTable.NewRow(); newRow["ProductID"] = rows["ProductID"]; newRow["ProductName"] = rows["ProductName"]; myDataTable.Rows.Add[newRow];
I noticed that the Column "ProductID" (DataColumn productIDColumn = new DataColumn("ProductID") did not specify the Data type. I think your data base returning ProductID as int type. So you can resolve this prob lem in two way (1) newRow["ProductID"] = rows["ProductID"].ToString(); OR (2)Also specify the Data type as int, When you are creating the column DataColumn productIDColumn = new DataColumn("ProductID")
Parwej Ahamad R & D with IIS 5.0/6.0
-
I noticed that the Column "ProductID" (DataColumn productIDColumn = new DataColumn("ProductID") did not specify the Data type. I think your data base returning ProductID as int type. So you can resolve this prob lem in two way (1) newRow["ProductID"] = rows["ProductID"].ToString(); OR (2)Also specify the Data type as int, When you are creating the column DataColumn productIDColumn = new DataColumn("ProductID")
Parwej Ahamad R & D with IIS 5.0/6.0
-
I noticed that the Column "ProductID" (DataColumn productIDColumn = new DataColumn("ProductID") did not specify the Data type. I think your data base returning ProductID as int type. So you can resolve this prob lem in two way (1) newRow["ProductID"] = rows["ProductID"].ToString(); OR (2)Also specify the Data type as int, When you are creating the column DataColumn productIDColumn = new DataColumn("ProductID")
Parwej Ahamad R & D with IIS 5.0/6.0
-
Even i tried your solution then also iam getting the same error..Please any further suggestions...
Can you post here modified code which i have suggested to you ? Mark as bold where you are still getting the problem and also post error messages.
Parwej Ahamad R & D with IIS 5.0/6.0