ICollection  AddRow()
{
DataRow dr = MyDt.NewRow(); dr[0] = TbLine.Text ;
dr[1] = DropDownList1.SelectedItem.Text ;
dr[2] = DropDownList2.SelectedItem.Text ;
dr[3] = TbNumber.Text ;
dr[4] = TbSinglePrice.Text ;
dr[5] = TbTotalPrice.Text ;
dr[6] = Tbfactory.Text ;
dr[7] = TbSupply.Text ;
dr[8] = TbSupplyOutId.Text ;
dr[9] = TbSupplyOutFp.Text ;
dr[10] = TbTime.Text ;

MyDt.Rows.Add(dr); DataView dv = new DataView(MyDt); DropDownList1.SelectedIndex=0;
DropDownList2.SelectedIndex=0;
TbNumber.Text = "" ;
TbSinglePrice.Text = "" ;
TbTotalPrice.Text = "" ;
Tbfactory.Text = "" ;
TbSupply.Text = "" ;
TbSupplyOutId.Text = "" ;
TbSupplyOutFp.Text = "" ;
TbTime.Text = "" ; return dv ;
} DataTable CreateColumns() 
{
//DataTable MyDt = new DataTable();
MyDt = new DataTable(); DataColumn col=new DataColumn("入库流水号", typeof(string));
col.AllowDBNull=false;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("耗材类型名称", typeof(string));
col.AllowDBNull=false;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("耗材型号名称", typeof(string));
col.AllowDBNull=false;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("数量", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("单价", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("总价", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col);

col=new DataColumn("生产厂家", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("供应商", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("供应商出库号", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col);

col=new DataColumn("供应商发票号", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); col=new DataColumn("购置日期", typeof(string));
col.AllowDBNull=true;
col.MaxLength=100;
MyDt.Columns.Add(col); //AddRow();
MyDt.AcceptChanges();
//DataRow dr = MyDt.NewRow(); return MyDt;
} private void Page_Load(object sender, System.EventArgs e)
{
myInfo =new GetMySysteminfo ();
myInfo.myPage = this ;
if (!myInfo.ValidateInfo()) 
return ; if (!IsPostBack) 
{
InitDropDownListExpendType();     Calendar1.SelectionMode=CalendarSelectionMode.Day;     Calendar1.ShowNextPrevMonth=true;

MyDataGrid.Width=1500 ;
MyDataGrid.DataSource= CreateColumns();
MyDataGrid.DataBind();
}

InitLine();
}DataTable CreateColumns() 
{
//DataTable MyDt = new DataTable();
MyDt = new DataTable();这里创建的啊ICollection  AddRow()
{
DataRow dr = MyDt.NewRow();是在这里报错的怎么解决各位老大!

解决方案 »

  1.   

    是不是你的对象名重复了
    dr有没有已经被datareader用过了?
      

  2.   

    MyDt是我在类里面定义,调用我的函数AddRow()时,在DataRow dr = MyDt.NewRow();报对象未引用错误
      

  3.   

    对象&函数   ?
      

  4.   

    楼主,拜托,我从头看到尾都没看到你在哪里调用了AddRow,难道是灵异现象?
    我只能猜测你根本就没搞清楚ASP.NET的回发模型,在PostBack的时候,会创建一个新的Page实例,你在回发之前赋值的所有的变量全部都会丢失。
      

  5.   

    我调用addrow就在一个点按钮的事件里面,这个会丢失吗???
      

  6.   

    <%@ Page language="c#" Codebehind="ExpendInAdd.aspx.cs" AutoEventWireup="false" Inherits="DeviceManage.ExpendInAdd" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
      <HEAD>
    <title>ExpendInAdd</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:label id="Label1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 16px" runat="server"
    Height="18" Width="88px">入库单流水</asp:label><asp:button id="ButtonSave" style="Z-INDEX: 118; LEFT: 328px; POSITION: absolute; TOP: 16px"
    runat="server" Height="24px" Width="73px" Text="增加"></asp:button><asp:button id="ButtonCancel" style="Z-INDEX: 110; LEFT: 112px; POSITION: absolute; TOP: 240px"
    runat="server" Height="32" Width="73" Text="返回"></asp:button><asp:label id="Label8" style="Z-INDEX: 108; LEFT: 24px; POSITION: absolute; TOP: 152px" runat="server"
    Height="24px" Width="80px">单价</asp:label><FONT face="宋体"><asp:label id="Label7" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 184px" runat="server"
    Height="16px" Width="88px">总价</asp:label></FONT><asp:label id="Label4" style="Z-INDEX: 107; LEFT: 24px; POSITION: absolute; TOP: 120px" runat="server"
    Height="8px" Width="80px">数量</asp:label><asp:label id="Label3" style="Z-INDEX: 106; LEFT: 616px; POSITION: absolute; TOP: 56px" runat="server"
    Height="24px" Width="72px">供应商</asp:label><asp:label id="Label6" style="Z-INDEX: 105; LEFT: 328px; POSITION: absolute; TOP: 56px" runat="server"
    Height="18px" Width="72px">生产厂家</asp:label><FONT face="宋体"><asp:label id="Label5" style="Z-INDEX: 103; LEFT: 328px; POSITION: absolute; TOP: 120px" runat="server"
    Height="18px" Width="64px">购置日期</asp:label></FONT><asp:label id="Label2" style="Z-INDEX: 104; LEFT: 24px; POSITION: absolute; TOP: 56px" runat="server"
    Height="18" Width="97">耗材类型名称</asp:label><asp:textbox id="TbLine" style="Z-INDEX: 109; LEFT: 144px; POSITION: absolute; TOP: 16px" runat="server"
    Height="26px" Width="169px" Enabled="False"></asp:textbox><asp:dropdownlist id="DropDownList1" style="Z-INDEX: 111; LEFT: 144px; POSITION: absolute; TOP: 56px"
    runat="server" Height="22" Width="169"></asp:dropdownlist><asp:textbox id="TbSinglePrice" style="Z-INDEX: 112; LEFT: 144px; POSITION: absolute; TOP: 152px"
    runat="server" Height="22px" Width="169px"></asp:textbox><asp:textbox id="TbTotalPrice" style="Z-INDEX: 113; LEFT: 144px; POSITION: absolute; TOP: 184px"
    runat="server" Height="22px" Width="169px"></asp:textbox><asp:textbox id="TbNumber" style="Z-INDEX: 114; LEFT: 144px; POSITION: absolute; TOP: 120px"
    runat="server" Height="22px" Width="169px"></asp:textbox><asp:textbox id="TbSupply" style="Z-INDEX: 115; LEFT: 728px; POSITION: absolute; TOP: 56px" runat="server"
    Height="22px" Width="169px"></asp:textbox><asp:textbox id="Tbfactory" style="Z-INDEX: 116; LEFT: 432px; POSITION: absolute; TOP: 56px"
    runat="server" Height="22px" Width="169px"></asp:textbox><asp:calendar id="Calendar1" style="Z-INDEX: 117; LEFT: 432px; POSITION: absolute; TOP: 144px"
    runat="server" Height="208px" Width="360px" Visible="False"></asp:calendar><asp:textbox id="TbTime" style="Z-INDEX: 119; LEFT: 432px; POSITION: absolute; TOP: 120px" runat="server"
    Height="22px" Width="169px" Enabled="False"></asp:textbox><asp:button id="BtnTime" style="Z-INDEX: 120; LEFT: 600px; POSITION: absolute; TOP: 120px" runat="server"
    Height="16px" Width="16px"></asp:button>
    <asp:RegularExpressionValidator id="REVNumber" style="Z-INDEX: 121; LEFT: 464px; POSITION: absolute; TOP: 16px"
    runat="server" Width="129" Height="24" ErrorMessage="请输入正确的数量" ControlToValidate="TbNumber" ValidationExpression="^(-?\d+)(\.\d+)?$"></asp:RegularExpressionValidator>
    <asp:RegularExpressionValidator id="REVSingle" style="Z-INDEX: 122; LEFT: 512px; POSITION: absolute; TOP: 16px"
    runat="server" Width="129" Height="24" ErrorMessage="请输入正确的单价" ControlToValidate="TbSinglePrice" ValidationExpression="^(-?\d+)(\.\d+)?$"></asp:RegularExpressionValidator>
    <asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 123; LEFT: 600px; POSITION: absolute; TOP: 16px"
    runat="server" Width="129px" Height="24px" ErrorMessage="请输入正确的总价" ControlToValidate="TbTotalPrice" ValidationExpression="^(-?\d+)(\.\d+)?$"></asp:RegularExpressionValidator>
    <asp:DataGrid id="MyDataGrid" style="Z-INDEX: 124; LEFT: 24px; POSITION: absolute; TOP: 280px"
    runat="server" Width="600px" Height="192px" BorderColor="Tan" BorderWidth="1px" BackColor="LightGoldenrodYellow"
    CellPadding="2" GridLines="None" ForeColor="Black">
    <FooterStyle BackColor="Tan"></FooterStyle>
    <SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle>
    <AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle>
    <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>
    <PagerStyle HorizontalAlign="Center" ForeColor="DarkSlateBlue" BackColor="PaleGoldenrod"></PagerStyle>
    </asp:DataGrid>
    <asp:Button id="Button1" style="Z-INDEX: 125; LEFT: 24px; POSITION: absolute; TOP: 240px" runat="server"
    Width="73px" Height="32px" Text="提交"></asp:Button>
    <asp:Label id="Label9" style="Z-INDEX: 126; LEFT: 24px; POSITION: absolute; TOP: 88px" runat="server"
    Width="97px" Height="18px">耗材型号</asp:Label>
    <asp:DropDownList id="DropDownList2" style="Z-INDEX: 127; LEFT: 144px; POSITION: absolute; TOP: 88px"
    runat="server" Width="169" Height="22"></asp:DropDownList>
    <asp:Label id="Label10" style="Z-INDEX: 128; LEFT: 328px; POSITION: absolute; TOP: 88px" runat="server"
    Width="96px" Height="24px">供应商出库号</asp:Label>
    <asp:Label id="Label11" style="Z-INDEX: 129; LEFT: 616px; POSITION: absolute; TOP: 88px" runat="server"
    Width="96px" Height="24px">供应商发票号</asp:Label>
    <asp:TextBox id="TbSupplyOutId" style="Z-INDEX: 130; LEFT: 432px; POSITION: absolute; TOP: 88px"
    runat="server" Width="169px" Height="22px"></asp:TextBox>
    <asp:TextBox id="TbSupplyOutFp" style="Z-INDEX: 131; LEFT: 728px; POSITION: absolute; TOP: 88px"
    runat="server" Width="169px" Height="22px"></asp:TextBox></FONT></form>
    </body>
    </HTML>
      

  7.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace DeviceManage
    {
    /// <summary>
    /// ExpendInAdd 的摘要说明。
    /// </summary>
    public class ExpendInAdd : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label5;
    protected System.Web.UI.WebControls.Label Label6;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Label Label4;
    protected System.Web.UI.WebControls.Label Label7;
    protected System.Web.UI.WebControls.Label Label8;
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Calendar Calendar1;
    protected System.Web.UI.WebControls.TextBox TbLine;
    protected System.Web.UI.WebControls.TextBox TbSinglePrice;
    protected System.Web.UI.WebControls.TextBox TbTotalPrice;
    protected System.Web.UI.WebControls.TextBox TbNumber;
    protected System.Web.UI.WebControls.TextBox TbSupply;
    protected System.Web.UI.WebControls.TextBox Tbfactory;
    protected System.Web.UI.WebControls.Button ButtonCancel;
    protected System.Web.UI.WebControls.Button ButtonSave;
    protected System.Web.UI.WebControls.Button BtnTime;
    protected System.Web.UI.WebControls.TextBox TbTime;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.RegularExpressionValidator REVNumber;
    protected System.Web.UI.WebControls.RegularExpressionValidator REVSingle;
    protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Label Label9;
    protected System.Web.UI.WebControls.DropDownList DropDownList2;
    protected System.Web.UI.WebControls.Label Label10;
    protected System.Web.UI.WebControls.Label Label11;
    protected System.Web.UI.WebControls.TextBox TbSupplyOutId;
    protected System.Web.UI.WebControls.TextBox TbSupplyOutFp;
    protected System.Web.UI.WebControls.DataGrid MyDataGrid;

    protected DataTable MyDt; protected GetMySysteminfo myInfo;

    private void InitDropDownListExpendType()
    {
    DropDownList1.AutoPostBack=true;
    string myExecuteQuery="select * from tbExpendType";
    DropDownList1.DataSource=myInfo.GetDataView(myExecuteQuery);
    DropDownList1.DataTextField = "TextField";
    DropDownList1.DataValueField = "ValueField";
    DropDownList1.DataBind();
    DropDownList1.SelectedIndex=0;
    } private void InitDropDownListExpendSmallType(string myExecuteQuery)
    {
    DropDownList2.DataSource=myInfo.GetDataView(myExecuteQuery);
    DropDownList2.DataTextField = "TextField";
    DropDownList2.DataValueField = "ValueField";
    DropDownList2.DataBind();
    DropDownList2.SelectedIndex=0;
    } private void InitLine()
    {
    string strExecuteQuery = "select max(expendingrarage_line)  from tbexpendingarage where expendingrarage_line like '" ;
    strExecuteQuery += string.Format("{0:0000}",DateTime.Now.Year); 
    strExecuteQuery += string.Format("{0:00}",DateTime.Now.Month);
    strExecuteQuery += string.Format("{0:00}",DateTime.Now.Day);
    strExecuteQuery += "%'";

    if (myInfo.GetTableCount(strExecuteQuery) == 1 )
    {
    TbLine.Text = string.Format("{0:0000}",DateTime.Now.Year); 
    TbLine.Text += string.Format("{0:00}",DateTime.Now.Month);
    TbLine.Text += string.Format("{0:00}",DateTime.Now.Day) ;
    TbLine.Text += string.Format("{0:000}",myInfo.GetTableCount(strExecuteQuery)) ;
    }
    else
    {
    TbLine.Text += string.Format("{0}",myInfo.GetTableCount(strExecuteQuery)) ;
    }
    }

    ICollection  AddRow()
    {
    DataRow dr = MyDt.NewRow(); dr[0] = TbLine.Text ;
    dr[1] = DropDownList1.SelectedItem.Text ;
    dr[2] = DropDownList2.SelectedItem.Text ;
    dr[3] = TbNumber.Text ;
    dr[4] = TbSinglePrice.Text ;
    dr[5] = TbTotalPrice.Text ;
    dr[6] = Tbfactory.Text ;
    dr[7] = TbSupply.Text ;
    dr[8] = TbSupplyOutId.Text ;
    dr[9] = TbSupplyOutFp.Text ;
    dr[10] = TbTime.Text ;

    MyDt.Rows.Add(dr); DataView dv = new DataView(MyDt); DropDownList1.SelectedIndex=0;
    DropDownList2.SelectedIndex=0;
    TbNumber.Text = "" ;
    TbSinglePrice.Text = "" ;
    TbTotalPrice.Text = "" ;
    Tbfactory.Text = "" ;
    TbSupply.Text = "" ;
    TbSupplyOutId.Text = "" ;
    TbSupplyOutFp.Text = "" ;
    TbTime.Text = "" ; return dv ;
    } DataTable CreateColumns() 
    {
    //DataTable MyDt = new DataTable();
    MyDt = new DataTable(); DataColumn col=new DataColumn("入库流水号", typeof(string));
    col.AllowDBNull=false;
    col.MaxLength=100;
    MyDt.Columns.Add(col); col=new DataColumn("耗材类型名称", typeof(string));
    col.AllowDBNull=false;
    col.MaxLength=100;
    MyDt.Columns.Add(col); col=new DataColumn("耗材型号名称", typeof(string));
    col.AllowDBNull=false;
    col.MaxLength=100;
    MyDt.Columns.Add(col); col=new DataColumn("数量", typeof(string));
    col.AllowDBNull=true;
    col.MaxLength=100;
    MyDt.Columns.Add(col); col=new DataColumn("单价", typeof(string));
    col.AllowDBNull=true;
    col.MaxLength=100;
    MyDt.Columns.Add(col); col=new DataColumn("总价", typeof(string));
    col.AllowDBNull=true;
    col.MaxLength=100;
    MyDt.Columns.Add(col);

    col=new DataColumn("生产厂家", typeof(string));
    col.AllowDBNull=true;
    col.MaxLength=100;
    MyDt.Columns.Add(col); col=new DataColumn("供应商", typeof(string));
    col.AllowDBNull=true;
    col.MaxLength=100;
    MyDt.Columns.Add(col); col=new DataColumn("供应商出库号", typeof(string));
    col.AllowDBNull=true;
    col.MaxLength=100;
    MyDt.Columns.Add(col);

    col=new DataColumn("供应商发票号", typeof(string));
    col.AllowDBNull=true;
    col.MaxLength=100;
    MyDt.Columns.Add(col); col=new DataColumn("购置日期", typeof(string));
    col.AllowDBNull=true;
    col.MaxLength=100;
    MyDt.Columns.Add(col); //AddRow();
    //DataRow dr = MyDt.NewRow();
    //MyDt.Rows.Add(dr);
    MyDt.AcceptChanges();
    return MyDt;
    } private void Page_Load(object sender, System.EventArgs e)
    {
    myInfo =new GetMySysteminfo ();
    myInfo.myPage = this ;
    if (!myInfo.ValidateInfo()) 
    return ; if (!IsPostBack) 
    {
    InitDropDownListExpendType();     Calendar1.SelectionMode=CalendarSelectionMode.Day;
        Calendar1.ShowNextPrevMonth=true;

    MyDataGrid.Width=1500 ;
    MyDataGrid.DataSource= CreateColumns();
    MyDataGrid.DataBind();
    }

    InitLine();
    }