I have a grid control with a GridTemplateColumn that contains an ItemTemplate and an InsertItemTemplate. The InsertItemTemplate contains a dropdown list. The problem is that I get a null reference exception when I try to insert a new record. I know that the issue lies in the code behind where I am using the following line:
new_doc_stat_cmd.Parameters.AddWithValue("@docStat", rg_doc_stats.MasterTableView.FindControl("docStatDdlInsert"));
When I replace the above line with the one below, the insert command is executed successfully:
new_doc_stat_cmd.Parameters.AddWithValue("@docStat", 4);
Here is the code for the grid:
<telerik:RadGrid ID="rg_doc_stats" runat="server" DataSourceID="sdc_doc_stats" AllowSorting="True" Skin="Office2010Blue" CellSpacing="-1" GridLines="Both" OnInsertCommand="rg_doc_stats_InsertCommand"> <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True"></ClientSettings> <MasterTableView DataSourceID="sdc_doc_stats" AutoGenerateColumns="False" DataKeyNames="docStatIdPk" CommandItemDisplay="Top"> <Columns> <telerik:GridBoundColumn DataField="docStatIdPk" ReadOnly="True" HeaderText="docStatIdPk" SortExpression="docStatIdPk" UniqueName="docStatIdPk" DataType="System.Int32" FilterControlAltText="Filter docStatIdPk column"></telerik:GridBoundColumn> <telerik:GridTemplateColumn DataField="docStat" HeaderText="Status" SortExpression="docStat" UniqueName="docStat" DataType="System.Int32" FilterControlAltText="Filter docStat column"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("stat") %>' ID="statLbl"></asp:Label> </ItemTemplate> <InsertItemTemplate> <asp:DropDownList ID="docStatDdlInsert" runat="server" SelectedValue='<%# Bind("docStat") %>' DataSourceID="sdc_stats" DataTextField="stat" DataValueField="statIdPk" AppendDataBoundItems="true"><asp:ListItem Value="">--Please Select--</asp:ListItem></asp:DropDownList> </InsertItemTemplate> </telerik:GridTemplateColumn> <telerik:GridBoundColumn DataField="docStatDt" HeaderText="Status Date" SortExpression="docStatDt" UniqueName="docStatDt" FilterControlAltText="Filter docStatDt column" DataType="System.DateTime" ReadOnly="true"></telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="docStatSetBy" HeaderText="Status Set By" SortExpression="docStatSetBy" UniqueName="docStatSetBy" FilterControlAltText="Filter docStatSetBy column" ReadOnly="true"></telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="docIdFk" HeaderText="docIdFk" SortExpression="docIdFk" UniqueName="docIdFk" DataType="System.Int32" FilterControlAltText="Filter docIdFk column" ReadOnly="true"></telerik:GridBoundColumn> </Columns> </MasterTableView> </telerik:RadGrid> </telerik:RadPageView>
Here are the InsertParameters from the SqlDataSource:
<InsertParameters><asp:Parameter Name="docStat" Type="Int32"></asp:Parameter><asp:Parameter Name="docStatDt" Type="DateTime"></asp:Parameter><asp:Parameter Name="docStatSetBy" Type="String"></asp:Parameter><asp:Parameter Name="docIdFk" Type="Int32"></asp:Parameter> </InsertParameters>
Here is the InsertCommand from the SqlDataSource:
InsertCommand="INSERT INTO [docStats] ([docStat], [docStatDt], [docStatSetBy], [docIdFk]) VALUES (@docStat, @docStatDt, @docStatSetBy, @docIdFk)"
Here is the InsertCommand from the code behind:
protected void rg_doc_stats_InsertCommand(object sender, GridCommandEventArgs e) { SqlConnection drap_cnxn = new SqlConnection("Server=Mine\\SQLEXPRESS;Initial Catalog=drap;Integrated Security=True;"); { SqlCommand new_doc_stat_cmd = new SqlCommand("Insert Into docStats(docStat, docStatDt, docStatSetBy, docIdFk) Values(LTRIM(RTRIM(@docStat)), LTRIM(RTRIM(@docStatDt)), LTRIM(RTRIM(@docStatSetBy)), LTRIM(RTRIM(@docIdFk)))", drap_cnxn); new_doc_stat_cmd.Parameters.AddWithValue("@docStat", rg_doc_stats.MasterTableView.FindControl("docStatDdlInsert")); new_doc_stat_cmd.Parameters.AddWithValue("@docStatDt", DateTime.Now.ToString()); new_doc_stat_cmd.Parameters.AddWithValue("@docStatSetBy", HttpContext.Current.User.Identity.Name); new_doc_stat_cmd.Parameters.AddWithValue("@docIdFk", hdn_doc_id.Value); drap_cnxn.Open(); new_doc_stat_cmd.ExecuteNonQuery(); drap_cnxn.Close(); if (IsPostBack) { rg_ven_docs.DataBind(); rg_doc_stats.DataBind(); } } }
Thank you,
J