Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to get previous value in dropdownlist when i click on edit button in gridview.

What I have tried:

if (e.Row.RowType == DataControlRowType.DataRow && gvcallRegister.EditIndex == e.Row.RowIndex)
      {

           if ((e.Row.RowState & DataControlRowState.Edit) > 0)
           {
          using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
          {
              DropDownList ddlprod = (DropDownList)e.Row.FindControl("DropDownList1");
              con.Open();

              SqlCommand cmd = new SqlCommand("select name from registeruser where dept='Sales'", con);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataSet ds = new DataSet();
              da.Fill(ds);
              ddlprod.DataSource = ds;
              ddlprod.DataTextField = "name";
              ddlprod.DataValueField = "name";
              ddlprod.DataBind();

              DataRowView dr = e.Row.DataItem as DataRowView;
              ddlprod.SelectedValue = dr["name"].ToString();// default selected value

                  }
              }

          }
      }
Posted
Updated 18-May-18 19:51pm

1 solution

Look at the error message:
Quote:
Name is neither a datacolumn nor a datarelation for table
Then look at your code:
C#
              SqlCommand cmd = new SqlCommand("select name from registeruser where dept='Sales'", con);
...
              ddlprod.DataTextField = "name";
              ddlprod.DataValueField = "name";
...
              ddlprod.SelectedValue = dr["name"].ToString();
Now check your DB: does it have a column called "name" in the table "registeruser"? If it does, use the debugger to check the DataRowView and see exactly what cells it contains. Is "name" there?

The error message says one of them doesn't.
 
Share this answer
 

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