Adding DropDownList to a DataGrid.

I am getting the following error and I do not understand why?<BR><BR>Could someone please point out to me what I have done wrong.<BR><BR>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. <BR><BR>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.<BR><BR>Source Error: <BR><BR><BR>Line 161: </ItemTemplate><BR>Line 162: <EditItemTemplate><BR>Line 163: <asp:DropDownList runat="server" DataSource='<%# ctryDS.Tables("SysCountry").DefaultView %>' DataTextField="Txt" DataValueField="id" id="ddl" /><BR>Line 164: </EditItemTemplate><BR>Line 165: </asp:TemplateColumn> <BR><BR><BR>The code is as follows:<BR><BR><%@ Page aspcompat=true Debug=true%><BR><%@ Import Namespace="System.Data" %><BR><%@ Import NameSpace="System.Data.OleDb" %><BR><html><BR><BR><script language="VB" runat="server"><BR><BR> Public MyConnection As OleDbConnection<BR> Public MyAdapter As OleDbDataAdapter<BR> Public MyCommand As OleDbCommand<BR><BR> Public SystemConnection As OleDbConnection<BR> Public SystemAdapter As OleDbDataAdapter <BR> Public SystemCommand As OleDbCommand <BR><BR> Public ctryDS As DataSet<BR> Public DS As DataSet<BR><BR> sub Page_Load(sender As Object, E As EventArgs) <BR> <BR> MyConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=C:Documents and SettingsantellDesktopProjectsSBSdbvglsbs_data.mdb ;Persist Security Info=False")<BR> MyConnection.Open<BR> SystemConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=C:Documents and SettingsantellDesktopProjectsSBSdbvglsbs_system.m db;Persist Security Info=False")<BR> SystemConnection.Open<BR> MyAdapter = New OleDbDataAdapter("select * from CliAddress", MyConnection)<BR><BR> ds = new DataSet()<BR> MyAdapter.Fill(ds, "CliAddress")<BR> <BR> MyDataGrid.DataSource=ds.Tables("CliAddress").DefaultView<BR> If Not (IsPostBack)<BR> SystemAdapter = New OleDbDataAdapter("select id, txt from SysCountry where active = True", SystemConnection)<BR> ctryDS = new DataSet()<BR> SystemAdapter.Fill(ctryDS, "SysCountry")<BR> BindGrid()<BR> End if<BR> End Sub<BR><BR> sub Page_UnLoad(sender As Object, E As EventArgs) <BR> MyConnection.Close<BR> SystemConnection.Close<BR> end sub<BR> <BR> Sub MyDataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)<BR> <BR> MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)<BR> BindGrid()<BR> <BR> End Sub<BR> <BR> Sub MyDataGrid_Cancel(Sender As Object, E As DataGridCommandEventArgs)<BR> <BR> MyDataGrid.EditItemIndex = -1<BR> BindGrid()<BR> <BR> End Sub<BR> <BR> Sub MyDataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)<BR> Dim Sql As String<BR> dim s As String<BR> Dim ad1Text As TextBox = CType(e.Item.Cells(1).Controls(0), TextBox) <BR> <BR> Response.Write(ad1Text.Text & "<BR>")<BR> s = "update CliAddress set ad1='" & ad1Text.Text & "' where id=" & MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))<BR> <BR> MyCommand = New OleDbCommand(s,MyConnection)<BR> Dim myReader As OleDbDataReader = myCommand.ExecuteReader()<BR> Response.write(s)<BR> MyDataGrid.EditItemIndex = -1<BR> BindGrid()<BR> End Sub<BR><BR> Sub MyDataGrid_Delete(Sender As Object, E As DataGridCommandEventArgs)<BR> Dim Sql As String<BR> Dim s As String <BR> s = "delete from cliAddress where id=" & MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))<BR> MyCommand = New OleDbCommand(s,MyConnection)<BR> Dim myReader As OleDbDataReader = MyCommand.ExecuteReader()<BR> Response.write(s)<BR> <BR> MyDataGrid.EditItemIndex = -1<BR> If Not IsPostBack<BR> BindGrid()<BR> end if<BR> <BR> End Sub<BR><BR> Sub BindGrid()<BR> MyAdapter = new OleDbDataAdapter("select * from CliAddress", MyConnection)<BR> DS = new DataSet()<BR> MyAdapter.Fill(DS, "CliAddress")<BR> MyDataGrid.DataSource=DS.Tables("CliAddress").DefaultView<BR> MyDataGrid.DataBind()<BR> End Sub<BR><BR></script><BR><BR><body style="font: 10pt verdana"><BR><BR> <form runat="server"><BR><BR> <h3><font face="Verdana">Updating a Row of Data w/ Templated Column</font></h3><BR><BR> <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p><BR><BR> <ASP:DataGrid id="MyDataGrid" runat="server"<BR> Width="800"<BR> BackColor="#ccccff"<BR> BorderColor="black"<BR> ShowFooter="false"<BR> CellPadding=3<BR> CellSpacing="0"<BR> Font-Name="Verdana"<BR> Font-Size="8pt"<BR> HeaderStyle-BackColor="#aaaadd"<BR> OnEditCommand="MyDataGrid_Edit"<BR> OnCancelCommand="MyDataGrid_Cancel"<BR> OnUpdateCommand="MyDataGrid_Update"<BR> DataKeyField="id"<BR> AutoGenerateColumns="false"><BR> <Columns><BR> <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false"/><BR> <asp:BoundColumn HeaderText="id" SortExpression="id" ReadOnly="True" DataField="ID" ItemStyle-Wrap="false"/><BR> <asp:TemplateColumn HeaderText="Address 1" SortExpression="ad1"><BR> <ItemTemplate><BR> <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ad1") %>'/><BR> </ItemTemplate><BR> <EditItemTemplate><BR> <asp:TextBox runat="server" id="edit_ad1" Text='<%# DataBinder.Eval(Container.DataItem, "ad1") %>'/><BR> </EditItemTemplate><BR> </asp:TemplateColumn><BR> <asp:TemplateColumn HeaderText="Address 2" SortExpression="ad2"><BR> <ItemTemplate><BR> <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ad2") %>'/><BR> </ItemTemplate><BR> <EditItemTemplate><BR> <asp:TextBox runat="server" id="edit_ad2" Text='<%# DataBinder.Eval(Container.DataItem, "ad2") %>'/><BR> </EditItemTemplate><BR> </asp:TemplateColumn><BR> <asp:TemplateColumn HeaderText="Address 3" SortExpression="ad3"><BR> <ItemTemplate><BR> <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ad3") %>'/><BR> </ItemTemplate><BR> <EditItemTemplate><BR> <asp:TextBox runat="server" id="edit_ad3" Text='<%# DataBinder.Eval(Container.DataItem, "ad3") %>'/><BR> </EditItemTemplate><BR> </asp:TemplateColumn><BR> <asp:TemplateColumn HeaderText="Address 4" SortExpression="ad4"><BR> <ItemTemplate><BR> <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ad4") %>'/><BR> </ItemTemplate><BR> <EditItemTemplate><BR> <asp:TextBox runat="server" id="edit_ad4" Text='<%# DataBinder.Eval(Container.DataItem, "ad4") %>'/><BR> </EditItemTemplate><BR> </asp:TemplateColumn><BR> <asp:TemplateColumn HeaderText="Country" SortExpression="Country"><BR> <ItemTemplate><BR> <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "country") %>'/><BR> </ItemTemplate><BR> <EditItemTemplate><BR> <asp:DropDownList runat="server" DataSource='<%# ctryDS.Tables("SysCountry").DefaultView %>' DataTextField="Txt" DataValueField="id" id="ddl" /><BR> </EditItemTemplate><BR> </asp:TemplateColumn><BR> <asp:TemplateColumn HeaderText="Postcode" SortExpression="pcode"><BR> <ItemTemplate><BR> <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "pcode") %>'/><BR> </ItemTemplate><BR> <EditItemTemplate><BR> <asp:TextBox runat="server" id="edit_pcode" Text='<%# DataBinder.Eval(Container.DataItem, "pcode") %>'/><BR> </EditItemTemplate><BR> </asp:TemplateColumn><BR> </Columns><BR> </ASP:DataGrid><BR> </form><BR></body><BR></html><BR><BR><BR>Regards<BR><BR>SteveSteve,<BR><BR>I had been suffering from the same problem, your code helped me understand that I needed to do the binding to the template controls at runtime (ie in the properties of the dropdown list)as the compiler could not see into the DataGrid at compile time. <BR><BR>Anyway once I had done that I made the same error as you. In the page load you do the dump of the data to the DataSet when the page is new rather than if its a post back. Therefore when you click the edit button on the datagrid at runtime, and the drop down list tries to bind to the data set, it throws an error as there is no data (clicking the edit link is a post back). You need to move the data set population into an else in the if not ispostback, i.e.<BR><BR>sub Page_Load(sender As Object, E As EventArgs) <BR><BR> MyConnection = New OleDbConnection()<BR> MyConnection.Open <BR> SystemConnection = New OleDbConnection("")<BR> SystemConnection.Open <BR> MyAdapter = New OleDbDataAdapter("select * from CliAddress", MyConnection) <BR><BR> ds = new DataSet() <BR> MyAdapter.Fill(ds, "CliAddress") <BR><BR> MyDataGrid.DataSource=ds.Tables("CliAddress").DefaultView <BR><BR> If Not (IsPostBack) <BR> BindGrid()<BR> else<BR> SystemAdapter = New OleDbDataAdapter("select id, txt from SysCountry where active = True", SystemConnection) <BR> ctryDS = new DataSet() <BR> SystemAdapter.Fill(ctryDS, "SysCountry")<BR> End if <BR>End Sub <BR><BR>Hope that works and / or makes some sense.<BR><BR>DMitch
 
Back
Top