2013-10-09 47 views
0

時,這是this thread with how to build a DetailsView button that will check all the boxes in a given form.延續我試圖將先前的線程中得到援助,但沒有迴應這樣的新線程。選擇校驗按鈕錯誤保存到數據庫

截至目前,該複選框按鈕的工作原理沒有問題,但是當我去點擊「保存」按鈕,這條線在CS文件中出現的錯誤消息:

   TextBox tb = FindControl<TextBox>(row.Cells[i].Controls); 
       if (tb != null) 
       { 
        table.Rows[0][j] = tb.Text; 
       } 

與錯誤:

System.NullReferenceException:對象不設置爲一個對象的一個​​實例。

我很難過,因爲我在這裏做錯了。我在想我不知道這個過程如何在幕後完全運作。我一直在翻閱MSDN文檔,看看我是否能夠更好地解釋如何從理論上正確地將其保存到數據庫,但一直沒有找到合適的文檔來協助。

的SQL代碼是固體,沒有問題之前,我添加的複選框按鈕,代碼工作。

表格代號:

(Note some cosmetic code for the details view has been omitted) 


<%@ Page Title="Test Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="form2.aspx.cs" Inherits="form2" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" Runat="Server"> 
</asp:Content> 
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server"> 
<asp:DetailsView ID="DetailsView" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"> 
<AlternatingRowStyle BackColor="White" /> 
<insert asp cosmetic gobbledy gook here> 
<Fields> 
    <asp:BoundField DataField="Field_1" HeaderText="Ticket Number" SortExpression="Field_1" /> 
    <asp:BoundField DataField="Field 2" HeaderText="Field 2" SortExpression="Field_2" /> 
    <asp:BoundField DataField="Field_3" HeaderText="Field 3" SortExpression="Field_3" /> 
    <asp:BoundField DataField="Field_4" HeaderText="Field 4" SortExpression="Field_4" /> 
    <asp:BoundField DataField="Field_5" HeaderText="Field 5" SortExpression="Field_5" /> 
    <asp:BoundField DataField="Field_6" HeaderText="Field 6" SortExpression="Field_6" /> 
    <asp:BoundField DataField="Field_7" HeaderText="Field 7" SortExpression="Field_7" /> 
    <asp:BoundField DataField="Field_8" HeaderText="Field 8" SortExpression="Field_8" /> 
    <asp:BoundField DataField="Field_9" HeaderText="Field 9" SortExpression="Field_9" /> 
    <asp:BoundField DataField="Field_10" HeaderText="Field 10" SortExpression="Field_10" /> 
    <asp:CheckBoxField DataField="Check_Box_1" HeaderText="Check Box 1" SortExpression="Check_Box_1" /> 
    <asp:CheckBoxField DataField="Check_Box_2" HeaderText="Check Box 2" SortExpression="Check_Box_2" /> 
    <asp:CheckBoxField DataField="Check_Box_3" HeaderText="Check Box 3" SortExpression="Check_Box_3" /> 
    <asp:CheckBoxField DataField="Check_Box_4" HeaderText="Check Box 4" SortExpression="Check_Box_4" /> 
    <asp:CheckBoxField DataField="Check_Box_5" HeaderText="Check Box 5" SortExpression="Check_Box_5" /> 
    <asp:CheckBoxField DataField="Check_Box_6" HeaderText="Check Box 6" SortExpression="Check_Box_6" /> 
    <asp:CheckBoxField DataField="Check_Box_7" HeaderText="Check Box 7" SortExpression="Check_Box_7" /> 
    <asp:CheckBoxField DataField="Check_Box_8" HeaderText="Check Box 8" SortExpression="Check_Box_8" /> 
    <asp:CheckBoxField DataField="Check_Box_9" HeaderText="Check Box 9" SortExpression="Check_Box_9" /> 
    <asp:CheckBoxField DataField="Check_Box_10" HeaderText="Check Box 10" SortExpression="Check_Box_10" /> 
    <asp:CheckBoxField DataField="Check_Box_11" HeaderText="Check Box 11" SortExpression="Check_Box_11" /> 
    <asp:CheckBoxField DataField="Check_Box_12" HeaderText="Check Box 12" SortExpression="Check_Box_12" /> 
    <asp:CheckBoxField DataField="Check_Box_13" HeaderText="Check Box 13" SortExpression="Check_Box_13" /> 
    <asp:CheckBoxField DataField="Check_Box_14" HeaderText="Check Box 14" SortExpression="Check_Box_14" /> 
    <asp:CheckBoxField DataField="Check_Box_15" HeaderText="Check Box 15" SortExpression="Check_Box_15" /> 
    <asp:CheckBoxField DataField="Check_Box_16" HeaderText="Check Box 16" SortExpression="Check_Box_16" /> 
    <asp:CheckBoxField DataField="Check_Box_17" HeaderText="Check Box 17" SortExpression="Check_Box_17" /> 
    <asp:CheckBoxField DataField="Check_Box_18" HeaderText="Check Box 18" SortExpression="Check_Box_18" /> 
    <asp:CheckBoxField DataField="Check_Box_19" HeaderText="Check Box 19" SortExpression="Check_Box_19" /> 


    <asp:ButtonField ButtonType="Button" CommandName="btnSelectAll" Text="Select/Check All Servers" > 


    <asp:BoundField DataField="Field_11" HeaderText="Field 11" SortExpression="Field_11" /> 
    <asp:BoundField DataField="Field_12" HeaderText="Field 12" SortExpression="Field_12" /> 
    <asp:BoundField DataField="Field_13" HeaderText="Field 13" SortExpression="Field_13" /> 


    <asp:ButtonField ButtonType="Button" CommandName="btnCancel" Text="Cancel" /> 
    <asp:ButtonField ButtonType="Button" CommandName="btnSave" Text="Save" /> 

    <asp:CommandField ButtonType="Button" NewText="CreateRecord" ShowInsertButton="True" /> 
</Fields> 
<more asp non essential cosmetic gobbledy gook> 

    </asp:DetailsView> 
    <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:test_form_connect %>" runat="server" InsertCommand="INSERT INTO [test].[detailsview_test_form] ([Field_1], [Field_2], [Field_3], [Field_4], [Field_5], [Field_6], [Field_7], [Field_8], [Field_9], [Field_10], [Check_Box_1], [Check_Box_2], [Check_Box_3], [Check_Box_4], [Check_Box_5], [Check_Box_6], [Check_Box_7], [Check_Box_8], [Check_Box_9], [Check_Box_10], [Check_Box_11], [Check_Box_12], [Check_Box_13], [Check_Box_14], [Check_Box_15], [Check_Box_16], [Check_Box_17], [Check_Box_18], [Check_Box_19], [Field_11], [Field_12], [Field_13]) VALUES (@Field_1, @Field_2, @Field_3, @Field_4, @Field_5, @Field_6, @Field_7, @Field_8, @Field_9, @Field_10, @Check_Box_1, @Check_Box_2, @Check_Box_3, @Check_Box_4, @Check_Box_5, @Check_Box_6, @Check_Box_7, @Check_Box_8, @Check_Box_9, @Check_Box_10, @Check_Box_11, @Check_Box_12, @Check_Box_13, @Check_Box_14, @Check_Box_15, @Check_Box_16, @Check_Box_17, @Check_Box_18, @Check_Box_19, @Field_11, @Field_12, @Field_13) "> 

    <InsertParameters> 
<asp:Parameter Name="Field_1" /> 
<asp:Parameter Name="Field_2" /> 
<asp:Parameter Name="Field_3" /> 
<asp:Parameter Name="Field_4" /> 
<asp:Parameter Name="Field_5" /> 
<asp:Parameter Name="Field_6" /> 
<asp:Parameter Name="Field_7" /> 
<asp:Parameter Name="Field_8" /> 
<asp:Parameter Name="Field_9" /> 
<asp:Parameter Name="Field_10" /> 
<asp:Parameter Name="Check_Box_1" /> 
<asp:Parameter Name="Check_Box_2" /> 
<asp:Parameter Name="Check_Box_3" /> 
<asp:Parameter Name="Check_Box_4" /> 
<asp:Parameter Name="Check_Box_5" /> 
<asp:Parameter Name="Check_Box_6" /> 
<asp:Parameter Name="Check_Box_7" /> 
<asp:Parameter Name="Check_Box_8" /> 
<asp:Parameter Name="Check_Box_9" /> 
<asp:Parameter Name="Check_Box_10" /> 
<asp:Parameter Name="Check_Box_11" /> 
<asp:Parameter Name="Check_Box_12" /> 
<asp:Parameter Name="Check_Box_13" /> 
<asp:Parameter Name="Check_Box_14" /> 
<asp:Parameter Name="Check_Box_15" /> 
<asp:Parameter Name="Check_Box_16" /> 
<asp:Parameter Name="Check_Box_17" /> 
<asp:Parameter Name="Check_Box_18" /> 
<asp:Parameter Name="Check_Box_19" /> 
<asp:Parameter Name="Field_11" /> 
<asp:Parameter Name="Field_12" /> 
<asp:Parameter Name="Field_13" /> 

     </InsertParameters> 
     </asp:SqlDataSource> 


</asp:Content> 

代碼隱藏

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 

public partial class checkbox_dev : System.Web.UI.Page 
{ 
DataTable m_table = null; 

public DataTable table 

{ 
    get 
    { 
     if (ViewState["m_table"] != null) 
      return (DataTable)ViewState["m_table"]; 
     else 
      return null; 
    } 
    set 
    { 
     ViewState["m_table"] = value; 
    } 
} 

public static T FindControl<T>(ControlCollection controls) 
{ 
    for (int i = 0; i < controls.Count; i++) 
    { 
     if (controls[i] is T) 
      return (T)(object)controls[i]; 
    } 

    return default(T); 
} 

protected void Page_Load(object sender, EventArgs e) 
//private static DataTable GetData(string sqlCommand) 
{ 
    string connectionString = GetConnectionString(); 
    SqlConnection connection = new SqlConnection(connectionString); 
} 

static private string GetConnectionString() 
{ 
    return "(insert SQL Server connection Stringhere)"; 
} 



protected void DetailsView1_ItemCommand(Object sender, DetailsViewCommandEventArgs e) 
{ 
    if (e.CommandName == "btnSelectAll") 
    { 
     foreach (DetailsViewRow row in DetailsView1.Rows) 
     { 
      for (int i = 0; i < row.Cells.Count; i++) 
      { 
       CheckBox cb = FindControl<CheckBox>(row.Cells[i].Controls); 
       if (cb != null) 
       { 
        cb.Checked = true; 
       } 
      } 
     } 
    } 
    if (e.CommandName == "btnSave") 
    { 
     int j = 0; 
     foreach (DetailsViewRow row in DetailsView1.Rows) 
     { 
      for (int i = 0; i < row.Cells.Count; i++) 
      { 
       TextBox tb = FindControl<TextBox>(row.Cells[i].Controls); 
       if (tb != null) 
       { 
        table.Rows[0][j] = tb.Text; 
       } 

       CheckBox cb = FindControl<CheckBox>(row.Cells[i].Controls); 
       if (cb != null) 
       { 
        table.Rows[0][j] = cb.Checked; 
       } 
      } 
      j++; 
     } 

     DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); 
     DetailsView1.DataSource = table; 
     DetailsView1.DataBind(); 
    } 
    if (e.CommandName == "btnEdit") 
    { 
     DetailsView1.ChangeMode(DetailsViewMode.Edit); 
     DetailsView1.DataSource = table; 
     DetailsView1.DataBind(); 
    } 
    if (e.CommandName == "btnCancel") 
    { 
     DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); 
     DetailsView1.DataSource = table; 
     DetailsView1.DataBind(); 
    } 
} 

} 
+0

你確定'table'沒有返回null嗎?你的get方法有可能返回null,我會在它上面放一個斷點,看看它返回的是什麼。 – JuStDaN

+0

在這一行中:「TextBox tb = FindControl (row.Cells [i] .Controls);」在立即窗口中添加一個斷點,鍵入:「row.Cells [i]。控件「,如果它爲空,則輸入」row「以查看它是否爲空 – User2012384

+1

幾乎所有'NullReferenceException'的情況都是相同的,請參閱」[.NET中的什麼是NullReferenceException?](http://stackoverflow.com/問題/ 4660142/what-is-a-nullreferenceexception-in-net)「的一些提示。 –

回答

0

我認爲它會彈出我最大的問題是你的代碼是你的語法,row.Cells[i].Controls在作出假設。您應該檢查以確保該單元格具有「控件」集合。

望着這個代碼塊,我最初的傳感器快要瘋了。

if (e.CommandName == "btnSave") 
{ 
    int j = 0; 
    foreach (DetailsViewRow row in DetailsView1.Rows) 
    { 
     for (int i = 0; i < row.Cells.Count; i++) 
     { 
      TextBox tb = FindControl<TextBox>(row.Cells[i].Controls); 
      if (tb != null) 
      { 
       table.Rows[0][j] = tb.Text; 
      } 

      CheckBox cb = FindControl<CheckBox>(row.Cells[i].Controls); 
      if (cb != null) 
      { 
       table.Rows[0][j] = cb.Checked; 
      } 
     } 
     j++; 
    } 

什麼,你在上面幹什麼,基本上是初始化類型的變量,「廉政」 0(int j = 0;),但你有沒有考慮到您正試圖在稍後訪問列集合索引加一上面的代碼,'DetailsView1.Rows'的每一行都有table.Rows[0][j] = cb.Checked;

所以基本上你在做什麼是由1對DetailsView1.Rows每一行遞增,並試圖通過索引來訪問你的數據表的「列」。但是,如果某個列不存在於該索引處?

有一件事我會建議修復中期問題是出在「細胞」收集使用前檢查電池的存在。像這樣...

if (table.Rows[0][j] != null) 
{ 
    table.Rows[0][j] = tb.Text; 
} 

這僅僅是一個創可貼修復,不應該被長期使用作爲可能迭代過去收集的界限根本問題是不是真的可以接受的做法。我會花一些時間分析你的方法和重構。是的,它對你來說可能是有意義的,即單元格應該都存在,因爲你的詳細視圖的行少於你的DataTable的單元格,但它仍然不是很好的做法,並且可能成爲你錯誤的原因。

+0

現在正在處理這個問題,但是我遇到了一些挑戰,我的調試器在SSL中不起作用,需要一天的時間 –

+0

你不能在VS IIS燈光環境中本地運行? –

+0

不,我在這個網站上強制使用SSL,現在它正在窒息,我正在配置VS來利用這個證書 –