Chriz L

Chriz L

  • NA
  • 220
  • 51.1k

Error loading values on GridView DropDownList

Sep 29 2016 4:40 AM
Hello,
this is my code for binding the gridView:
  1. public void bindGrid(GridView gv, String id)  
  2.         {                
  3.             DataSet ds;  
  4.             SqlDataAdapter SqlAda;  
  5.             SqlConnection con =getConnection();  
  6.             SqlCommand cmd = new SqlCommand();  
  7.   
  8.             cmd.CommandType = CommandType.StoredProcedure;  
  9.             cmd.CommandText = "sproc";  
  10.             cmd.Parameters.Add("@id", SqlDbType.VarChar).Value =id;  
  11.             cmd.Connection = con;  
  12.             try  
  13.             {  
  14.                 con.Open();  
  15.                 gv.EmptyDataText = No data";  
  16.   
  17.                 SqlAda = new SqlDataAdapter(cmd);  
  18.                 ds = new DataSet();  
  19.   
  20.                 SqlAda.Fill(ds);  
  21.   
  22.                 gv.DataSource = ds;  
  23.                 gv.DataBind();  
  24.             }  
  25.             catch (Exception ex)  
  26.             {  
  27.                myMessageBox(ex.Message);  
  28.             }  
  29.             finally  
  30.             {  
  31.                 con.Close();  
  32.                 con.Dispose();  
  33.             }  
  34.         }  
It works ok until I added a dropdownlist on the gridview. I' m loading the  ddl using the following code:
  1. protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.         {  
  3.           if (e.Row.RowType == DataControlRowType.DataRow)  
  4.             {  
  5.                 DropDownList ddlTypes = (e.Row.FindControl("ddlType"as DropDownList);  
  6.                 fillDDL(ddlTypes, "sproc""ID", "TypeDescr", "");  
  7.   
  8.                 string myType = (e.Row.FindControl("lblType"as Label).Text;  
  9.                 ddlTypes.Items.FindByValue(myType).Selected = true;  
  10.             }  
  11.         }  
  1. private void fillDDL(DropDownList ddl, string sproc, string code, string description, string headertext)  
  2.    {  
  3.        DataTable dt = new DataTable();  
  4.        SqlConnection connection = appObj.getConnection();  
  5.   
  6.        ddl.Items.Clear();  
  7.        ddl.Items.Add(new ListItem(headertext, "0"));  
  8.   
  9.        ddl.AppendDataBoundItems = true;  
  10.        try  
  11.        {  
  12.            connection.Open();  
  13.   
  14.            SqlCommand sqlCmd = new SqlCommand(sproc, connection);  
  15.            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);  
  16.            sqlCmd.CommandType = CommandType.StoredProcedure;  
  17.   
  18.            sqlDa.Fill(dt);  
  19.   
  20.   
  21.            if (dt.Rows.Count > 0)  
  22.            {  
  23.                //Populate your DropDownList  
  24.                ddl.DataSource = dt;  
  25.                ddl.DataTextField = description;  
  26.                ddl.DataValueField = code;  
  27.                ddl.DataBind();  
  28.            }  
  29.        }  
  30.        catch (Exception ex)  
  31.        {  
  32.            HttpContext.Current.Session["error"] = ex.Message.ToString();  
  33.            HttpContext.Current.Response.Redirect("Error.aspx");  
  34.        }  
  35.        finally  
  36.        {  
  37.            connection.Close();  
  38.        }  
  39.    }  
 and the .aspx page
  1. <asp:TemplateField HeaderText="Type" Visible="false">  
  2.      <ItemTemplate>  
  3.          <asp:Label ID="lblType" runat="server" Text='<%#Eval("TypeDescr") %>'></asp:Label>  
  4.      </ItemTemplate>  
  5.      <EditItemTemplate>    
  6.          <asp:Label ID="lblValue" runat="server" Text='<%#Eval("TypeID") %>' Visible="true"></asp:Label>  
  7.        <asp:DropDownList ID="ddlType" runat="server"></asp:DropDownList>   
  8.      </EditItemTemplate>  
  9.  </asp:TemplateField>  
The problem is on loading I get the error "Object Reference not set to an instance of an object". The dropdownlist is loading with values, but cannot get assigned value.
 
Any kind of help would be appreciated.
 
Thank you in advance. 
 

Answers (2)