Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. DataBinding to DropDownList in GridView Error

DataBinding to DropDownList in GridView Error

Scheduled Pinned Locked Moved ASP.NET
databasehelpdesignsysadminsecurity
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Samiullah
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • S Samiullah

      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

      P Offline
      P Offline
      Parwej Ahamad
      wrote on last edited by
      #2

      Can you tell me on which line your getting the Error ?

      Parwej Ahamad R & D with IIS 5.0/6.0

      S 1 Reply Last reply
      0
      • P Parwej Ahamad

        Can you tell me on which line your getting the Error ?

        Parwej Ahamad R & D with IIS 5.0/6.0

        S Offline
        S Offline
        Samiullah
        wrote on last edited by
        #3

        Iam getting the error with the following lines... DataRow newRow = myDataTable.NewRow(); newRow["ProductID"] = rows["ProductID"]; newRow["ProductName"] = rows["ProductName"]; myDataTable.Rows.Add[newRow];

        P 1 Reply Last reply
        0
        • S Samiullah

          Iam getting the error with the following lines... DataRow newRow = myDataTable.NewRow(); newRow["ProductID"] = rows["ProductID"]; newRow["ProductName"] = rows["ProductName"]; myDataTable.Rows.Add[newRow];

          P Offline
          P Offline
          Parwej Ahamad
          wrote on last edited by
          #4

          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

          S 2 Replies Last reply
          0
          • P Parwej Ahamad

            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

            S Offline
            S Offline
            Samiullah
            wrote on last edited by
            #5

            Still iam getting the same error even if i do the things whatever you said..please any further suggestions..

            1 Reply Last reply
            0
            • P Parwej Ahamad

              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

              S Offline
              S Offline
              Samiullah
              wrote on last edited by
              #6

              Even i tried your solution then also iam getting the same error..Please any further suggestions...

              P 1 Reply Last reply
              0
              • S Samiullah

                Even i tried your solution then also iam getting the same error..Please any further suggestions...

                P Offline
                P Offline
                Parwej Ahamad
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups