DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
        DropDownList d4 = GridView1.FooterRow.FindControl("DropDownList4") as DropDownList;
        DropDownList d6 = GridView1.FooterRow.FindControl("DropDownList6") as DropDownList;
        DropDownList d8 = GridView1.FooterRow.FindControl("DropDownList8") as DropDownList;
        DropDownList d10 = GridView1.FooterRow.FindControl("DropDownList10") as DropDownList;
        SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue; 
        SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
        SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue;
        SqlDataSource1.InsertParameters["AntiHBe"].DefaultValue = d8.SelectedValue;
        SqlDataSource1.InsertParameters["AntiHBc"].DefaultValue = d10.SelectedValue;
        SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
        SqlDataSource1.Insert();如上,我想以此实现从footer插入功能,但是上面的代码出错!提示信息如下:
"
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误: 
行 30:         SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue; 
行 31:         SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
行 32:         SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue; 
"
小弟愚笨,求达人指点,实在不明白错在哪里了。

解决方案 »

  1.   

    NullReferenceException  你调试下 那个地方出现了NULL 了  跟踪下 很好解决的
      

  2.   

    selectitem
    为空了!
    if(d2.selectitem!=null)
    SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue; 
    if(d4.selectitem!=null) 
    SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
    if(d6.selectitem!=null)
    SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue; 
    这样不知道对不
    得试下了
      

  3.   

    我用Button按钮测试过了,不是空的啊
     protected void Button1_Click(object sender, EventArgs e)
        {
            DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
            Response.Write(d2.SelectedValue);
        }
    它可以输出我想要的内容。。
      

  4.   

    取Footer中的控件好象比较特殊,没记错的话应该是Controls[0].Controls[0].FindControl.....
    大致如下:楼主试试,结贴的时候再来看看我的记忆力怎样DropDownList d3 = (DropDownList)GridView1.Controls[0].Controls[0].FindControl("DropDownList2");
      

  5.   

    DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;=>DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
    if(ds2!=null)
    {
      // do sth
    }其余同上
      

  6.   

    6楼抱歉!!记错了Controls[0].Controls[0].FindControl.....
    是寻找<EmptyDataTemplate>模板列中控件的!!
      

  7.   

    GridView1.FooterRow.FindControl("DropDownList2") as DropDownList
    改成GridView1.FooterRow.Cell[4].FindControl("DropDownList2") as DropDownList
    4是DropDownList2所在的单元格列号
      

  8.   

    [code=C#]using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class doctor_yiganliangduiban : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
            DropDownList d4 = GridView1.FooterRow.FindControl("DropDownList4") as DropDownList;
            DropDownList d6 = GridView1.FooterRow.FindControl("DropDownList6") as DropDownList;
            DropDownList d8 = GridView1.FooterRow.FindControl("DropDownList8") as DropDownList;
            DropDownList d10 = GridView1.FooterRow.FindControl("DropDownList10") as DropDownList;
            SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue;
            SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
            SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue;
            SqlDataSource1.InsertParameters["AntiHBe"].DefaultValue = d8.SelectedValue;
            SqlDataSource1.InsertParameters["AntiHBc"].DefaultValue = d10.SelectedValue;
            SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
            SqlDataSource1.Insert();       
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            //DropDownList d1 = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
            //DropDownList d3 = GridView1.FooterRow.FindControl("DropDownList3") as DropDownList;
            //DropDownList d5 = GridView1.FooterRow.FindControl("DropDownList5") as DropDownList;
            //DropDownList d7 = GridView1.FooterRow.FindControl("DropDownList7") as DropDownList;
            //DropDownList d9 = GridView1.FooterRow.FindControl("DropDownList9") as DropDownList;
            ////   [HBsAg] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
            ////[AntiHBs] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
            ////[HBeAg] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
            ////[AntiHBe] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
            ////[AntiHBc] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL 
            //SqlDataSource1.UpdateParameters["HBsAg"].Name = d1.SelectedValue;        ////SqlDataSource1.UpdateParameters["HBsAg"].DefaultValue = d1.SelectedValue; 
         
            ////SqlDataSource1.UpdateParameters["AntiHBs"].DefaultValue = d3.SelectedValue;
            ////SqlDataSource1.UpdateParameters["HBeAg"].DefaultValue = d5.SelectedValue;
            ////SqlDataSource1.UpdateParameters["AntiHBe"].DefaultValue = d7.SelectedValue;
            ////SqlDataSource1.UpdateParameters["AntiHBc"].DefaultValue = d9.SelectedValue;        ////SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
            ////SqlDataSource1.Insert();
            //SqlDataSource1.Update();
        }
        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            Table gvTable = ((Table)GridView1.Controls[0]);
            string d15 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList15")).SelectedValue;
            string d11 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList11")).SelectedValue;
            string d12 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList12")).SelectedValue;
            string d13 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList13")).SelectedValue;
            string d14 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList14")).SelectedValue;
            SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d15;
            SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue=d11;
            SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d12;
            SqlDataSource1.InsertParameters["AntiHBe"].DefaultValue =d13;
            SqlDataSource1.InsertParameters["AntiHBc"].DefaultValue =d14;    
            SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
            SqlDataSource1.Insert();
            //GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
            Response.Write(d2.SelectedValue);
        }
    }
    这是所有的代码了。
      

  9.   

    [/code]<%@ Page Language="C#" MasterPageFile="~/mineMasterPage/doctor.master" AutoEventWireup="true" CodeFile="yaowuzhil.aspx.cs" Inherits="doctor_cuhongsu" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <table style="width: 474px; height: 118px">
            <tr>
                <td colspan="3">
                    促红素</td>
            </tr>
            <tr>
                <td colspan="3" style="height: 408px; vertical-align: top;">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                        DataSourceID="SqlDataSource1" ShowFooter="True" AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None">
                        <Columns>
                            <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID" Visible="False">
                                <EditItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="处方日期" SortExpression="checkTime">
                                <EditItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("checkTime", "{0:D}") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("checkTime", "{0:D}") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="促红素每周剂量(IU)" SortExpression="cuhongsujiliang">
                                <EditItemTemplate>
                                    &nbsp;
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("cuhongsujiliang") %>' MaxLength="5" ValidationGroup="up"></asp:TextBox><asp:RegularExpressionValidator
                                        ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
                                        ErrorMessage="粗红素剂量只能输入数字!" ValidationExpression="[-]{0,1}\d+\.\d+|[1-9]\d*" ValidationGroup="up">*</asp:RegularExpressionValidator>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    &nbsp;
                                    <asp:TextBox ID="TextBox2" runat="server" MaxLength="5" ValidationGroup="up"></asp:TextBox><asp:RegularExpressionValidator
                                        ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBox2"
                                        ErrorMessage="粗红素剂量只能输入数字!" ValidationExpression="[-]{0,1}\d+\.\d+|[1-9]\d*" ValidationGroup="up">*</asp:RegularExpressionValidator>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("cuhongsujiliang") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="操作" ShowHeader="False">
                                <EditItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                        Text="更新" ValidationGroup="up"></asp:LinkButton>
                                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                        Text="取消"></asp:LinkButton>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">保存</asp:LinkButton>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                        Text="编辑"></asp:LinkButton>
                                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                                            Text="删除" OnClientClick="return confirm('删除后将无法恢复,是否确认删除此行信息?')"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <RowStyle BackColor="#E3EAEB" />
                        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#7C6F57" />
                        <AlternatingRowStyle BackColor="White" />
                        <EmptyDataTemplate>
                            <table style="width: 309px">
                                <tr>
                                    <td style="font-weight: bold; width: 25px; color: white; background-color: #1c5e55; height: 6px;">
                                        <nobr>处方日期</nobr></td>
                                    <td style="font-weight: bold; width: 156px; color: white; background-color: #1c5e55; height: 6px;">
                                        <nobr>促红素每周剂量(IU)</nobr></td>
                                    <td style="font-weight: bold; width: 34px; color: white; background-color: #1c5e55; height: 6px;">
                                        <nobr>操作</nobr></td>
                                </tr>
                                <tr>
                                    <td style="width: 25px; height: 18px;">
                                    </td>
                                    <td style="width: 156px; height: 18px;">
                                        <nobr>
                                            &nbsp;<asp:TextBox ID="TextBox3" runat="server" ValidationGroup="eb"></asp:TextBox><asp:RegularExpressionValidator
                                            ID="RegularExpressionValidator3" runat="server" ControlToValidate="TextBox3"
                                            ErrorMessage="粗红素剂量只能输入数字!" ValidationExpression="[-]{0,1}\d+\.\d+|[1-9]\d*" ValidationGroup="eb">*</asp:RegularExpressionValidator></nobr></td>
                                    <td style="width: 34px; height: 18px;">
                                        <nobr><asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click" ValidationGroup="eb">保存</asp:LinkButton></nobr></td>
                                </tr>
                            </table>
                        </EmptyDataTemplate>
                        <EmptyDataRowStyle BackColor="#E3EAEB" />
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:XYJHConnectionString %>"
                        DeleteCommand="DELETE FROM cuhongsu WHERE (ID = @id)" InsertCommand="INSERT INTO cuhongsu(cuhongsujiliang, UserID) VALUES (@cuhongsujiliang, @userid)"
                        SelectCommand="SELECT [cuhongsujiliang], [checkTime], [ID], [UserID] FROM [cuhongsu] where userid=@userid"
                        UpdateCommand="UPDATE cuhongsu SET cuhongsujiliang = @cuhongsujiliang WHERE (ID = @id)">
                        <DeleteParameters>
                            <asp:Parameter Name="id" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="cuhongsujiliang" />
                            <asp:Parameter Name="id" />
                        </UpdateParameters>
                        <SelectParameters>
                            <asp:SessionParameter Name="userid" SessionField="userid" />
                        </SelectParameters>
                        <InsertParameters>
                            <asp:Parameter Name="cuhongsujiliang" />
                            <asp:Parameter Name="userid" />
                        </InsertParameters>
                    </asp:SqlDataSource>
                </td>
            </tr>
        </table>
    </asp:Content>
      

  10.   

    在LinkButton3下取不到值??
    这个LinkButton3在哪里的??
      

  11.   

    是呀,我也很纳闷我是把dropdownlist传递参数放在上面出错,如果我把session传递参数放在上面的话,出一样的错误,所以我认为,不是dropdownlist问题
      

  12.   

    LinkButton3是保存呀,在编辑列的footer里。
      

  13.   

    string.isNullOrEmpty(d2.SelectedValue)?"":d2.SelectedValue;
    void Button1_Click(object sender, EventArgs e)

     TextBox t= Gridview1.FooterRow.FindControl("TextBox1) as TextBox;
     SqlDataSource1.InsertParameters["ID"].DefaultValue = t.Text;  
     SqlDataSource1.Insert();
    }foreach(GridViewRow gr in this.GridView1.Rows)
    {}
      

  14.   

    我估计是你的SqlDataSource的Inser参数没设置对!!
    可以将你的前台SqlDataSource代码贴一下!!
      

  15.   

    我是实现这样功能的,在gridview里显示一行可以直接插入的行,而我就直接在相应字段footer模板里拖dropdownlist控件,在编辑列footer模板里拖一个LinkButton,点击实施insert语句,然后再gridview里设置showfooter属性为true,这样就在gridview最下面一行显示可以直接插入的这一行,
    由于当表里没有内容的时候所有的footer不显示,所有我在EmptyDataTemplate模板里也插入一个两行表格,在表格第二行插入dropdownlist控件,第一行填写相应字段名,最后面插入一个linkbutton,点击linkbutton,实施insert语句
      

  16.   

    我是直接用的sqldatasorce控件,里面的insertquery我是这样写的:
    INSERT INTO yiganliangduiban(UserID, HBsAg, AntiHBs, HBeAg, AntiHBe, AntiHBc) VALUES (@UserID, @HBsAg, @AntiHBs, @HBeAg, @AntiHBe, @AntiHBe)
      

  17.   

    哦哦!!
    EmptyDataTemplate模板中用
    Controls[0].Controls[0].FindControl.....
    找控件!!
      

  18.   

    EmptyDataTemplate模板不是关键,即使把EmptyDataTemplate模板相应代码注释掉,还是同样问题的。。
      

  19.   

    对应的
                <InsertParameters>
                    <asp:Parameter Name="UserID" />
                    <asp:Parameter Name="HBsAg" />
    ........................
                </InsertParameters>
    对吗?
    还有你那个UserID是自增长的还是其他??
      

  20.   

    你肯定LinkButton3的click事件类似以下的取不到值??d2,d4等都为null???
      DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
      DropDownList d4 = GridView1.FooterRow.FindControl("DropDownList4") as DropDownList;
    .........仔细看了主贴,出错好象是设置insert参数出错!
      

  21.   

    我调试看过,d2,d4。。都不是null,如果我把这句放第一句的时候
    SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
    出一样的问题,即使我把等号左边改成常量,也是一样的问题。
    所以我想是不是在赋值的时候出现啥问题了。。
      

  22.   

    也就是说FooterRow是能够取到值的.那很好,前面都在讨论是否取到值!现在的问题是设置Insert参数不行!!问一下,以下LinkButton3事件下的都不行吗??
      SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue;
      SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
      SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue;
      SqlDataSource1.InsertParameters["AntiHBe"].DefaultValue = d8.SelectedValue;
      SqlDataSource1.InsertParameters["AntiHBc"].DefaultValue = d10.SelectedValue;
      SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
      

  23.   

    哦哦!!
    如果传常量都不可以的话!那可能是设置SqlDataSource1哪儿有问题!
    要不对数据库你没有Insert权限??
    建议:
    另外建一个页面,最简单弄两个字段试试,究竟问题在哪儿?
    因为我过去和你类似的搞过,没什么问题!!
      

  24.   

    www.sesef.info/?u=1160721899   点击吧  不错的