你调用的对象是空的。在RowDataCommand的gridview中 [英] Object reference not set to an instance of an object. in gridview on RowDataCommand

查看:46
本文介绍了你调用的对象是空的。在RowDataCommand的gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友当我们在RowDataCommand上计算列值的运行总数时,我有一个名为Geidview1的Gridview它返回错误对象引用未设置为对象的实例

下面是错误的图像



请任何人都可以建议我们如何删除此错误。



以下是我的代码



我的c#代码用于绑定gridview



public void bindgvduedate()

{

con = new SqlConnection();

con.ConnectionString = ConfigurationManager.ConnectionStrings [con1]。ConnectionString;

con.Open();

SqlCommand cmd = new SqlCommand();

cmd.CommandText =readfeedetails;

cmd.CommandType = CommandType.StoredProcedure;

cmd.Connection = con;

////cmd.Parameters.AddWithValue(\"@ classname,Convert.ToString(ddlclassname.SelectedItem));

////cmd.Parameters.AddWithValue(\"@feecatename,Convert.ToString(ddlstcategory.SelectedItem));



SqlDataAdapter da = new SqlDataAdapter(cmd);

DataTable table = new DataTable();

da.Fill(table );

GridView1.DataSource = table;

GridView1.DataBind();

con.Close();



}



这个函数我在页面加载事件上调用



Griedview aspx代码







< asp:GridView ID =GridView1runat =server AutoGenerateColumns =False

BackColor =WhiteBorderColor =#336666BorderStyle =Double

字体大小=11pxBorderWidth =3px

CellPadding =4GridLines =both

ShowFooter =Trueonrowdatabound =GridView1_RowDataBound>

< rowstyle forecolor =#333333backcolor =White height =21px>

< columns>

< asp:TemplateField>

< HeaderTemplate> Apr-15< / HeaderTemplate>

< itemtemplate>< asp:标签ID =lblaprlrunat =serverText ='<%#Eval(Apr-15)%>'>

< footertemplate> < asp:Label ID =lbl_aprTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> 5月15日< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lblmayrunat =serverText ='<%#Eval(May-15)%>'>

< footertemplate> < asp:Label ID =lbl_mayTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Jun-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lbljunrunat =serverText ='<%#Eval(Jun-15)%>'>

< footertemplate> < asp:Label ID =lbl_junTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Jul-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lbljulrunat =serverText ='<%#Eval(Jul-15)%>'>

< footertemplate> < asp:Label ID =lbl_julTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Aug-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lblaugrunat =serverText ='<%#Eval(Aug-15)%>'>

< footertemplate> < asp:Label ID =lbl_augTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Sep-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lblseprunat =serverText ='<%#Eval(Sep-15)%>'>

< footertemplate> < asp:Label ID =lbl_sepTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> 10月15日< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lbloctrunat =serverText ='<%#Eval(Oct-15)%>'>

< footertemplate> < asp:Label ID =lbl_octTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Nov-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lblnovrunat =serverText ='<%#Eval(Nov-15)%>'>

< footertemplate> < asp:Label ID =lbl_novTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Dec-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lbldecrunat =serverText ='<%#Eval(Dec-15)%>'>

< footertemplate> < asp:Label ID =lbl_decTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Jan-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lbljanrunat =serverText ='<%#Eval(Jan-15)%>'>

< footertemplate> < asp:Label ID =lbl_janTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Feb-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lblfebrunat =serverText ='<%#Eval(Feb-15)%>'>

< footertemplate> < asp:Label ID =lbl_febTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Mar-15< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lblmarrunat =serverText ='<%#Eval(Mar-15)%>'>

< footertemplate> < asp:Label ID =lbl_marTotalamountCssClass =footer_textrunat =serverText =1>





< asp:TemplateField>

< HeaderTemplate> Total< / HeaderTemplate>

< itemtemplate>< asp:Label ID =lbltotalrunat =serverText =0>

< footertemplate> < asp:Label ID =lbl_TotalsamountCssClass =footer_textrunat =serverText =1>





< footerstyle backcolor =Whiteforecolor =#333333>

< pagerstyle backcolor =#336666forecolor =Whitehorizo​​ntalalign =Center>

< SelectedRowStyle BackColor =#339966Font-Bold =TrueForeColor =White/> ;

< HeaderStyle BackColor =#336666Font-Bold =TrueForeColor =White/>





计算运行总计的代码



protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

{





if(e.Row.RowType == DataControlRowType.Footer)

{

con = new SqlConnection();

con.Connect ionString = ConfigurationManager.ConnectionStrings [con1]。ConnectionString;

con.Open();

SqlCommand cmd = new SqlCommand(从dbo中选择sum(feeamount)。 feemaster其中classname ='KG'和feeactive = 1和feecatename ='NEW'和duemonth ='Apr-15',con);

SqlDataReader dr = cmd.ExecuteReader();

if(dr.Read())

{

//e.Row.Cells[1].Text =Total:;

标签lbl_aprTotalamount = GridView1.FindControl(lbl_aprTotalamount)作为标签;

lbl_aprTotalamount.Text = dr [0] .ToString();

}



}



}





我的数据库表



CREATE TABLE [dbo]。[feemaster](

[feeId] [int ] IDENTITY(1,1)NOT NULL,

[ footype] [nvarchar](50)NULL,

[feename] [nvarchar](50)NULL,

[classname] [nvarchar](50)NULL,

[duemonth] [nvarchar](50)NULL,

[feeamount] [decimal](15,2)NULL,

[feeactive] [ bit] NULL,

[feecatename] [nvarchar](50)NULL

)ON [PRIMARY]

GO

SET IDENTITY_INSERT [dbo]。[feemaster] ON

INSERT [dbo]。[feemaster]([feeId],[footype],[feename],[classname],[duemonth],[ feeamount],[feeactive],[feecatename])VALUES(1,N'One Time',N'Prospectus&注册费',N'PRE NURSURY',N'Apr-15',CAST(300.00 AS Decimal(15,2)),1,N'NEW')

INSERT [dbo]。[ feemaster]([feeId],[footype],[feename],[classname],[duemonth],[feeamount],[feeactive],[feecatename])VALUES(13,N'One Time',N'Prospectus&注册费',N'NURSURY',N'Apr-15',CAST(300.00 AS Decimal(15,2)),1,N'NEW')

INSERT [dbo]。[feemaster] ]([feeId],[footype],[feename],[classname],[duemonth],[feeamount],[feeactive],[feecatename])VALUES(2,N'One Time',N'Prospectus& Registration费',N'KG',N'Apr-15',CAST(300.00 AS十进制(15,2)),1,N'NEW')



INSERT [dbo]。[feemaster]([feeId],[footype],[feename],[classname],[duemonth],[feeamount],[feeactive],[feecatename])VALUES(16,N'Annual', N'Maintenance Fee',N'NURSURY',N'Apr-15',CAST(500.00 AS Decimal(15,2)),1,N'NEW')

INSERT [dbo]。 [feemaster]([feeId] ,[footype],[feename],[classname],[duemonth],[feeamount],[feeactive],[feecatename])VALUES(17,N'Annual',N'Maintenance Fee',N'NURSURY',N 'Apr-15',CAST(500.00 AS Decimal(15,2)),1,N'OLD')

INSERT [dbo]。[feemaster]([feeId],[feetype], [feename],[classname],[duemonth],[feeamount],[feeactive],[feecatename])VALUES(18,N'Annual',N'Maintenance Fee',N'KG',N'Apr-15' ,CAST(500.00 AS十进制(15,2)),1,N'NEW')







我的存储过程



创建过程[dbo]。[readfeedetails]

AS

BEGIN

选择

*

来自



选择f.feename,f.feeamount ,dd.duemonth

来自feemaster作为f

左外连接duedate为dd在dd.duemonth = f.duemonth,其中classname ='KG'和feeactive = 1和feecatename ='老'



)a sf

pivot



min(f.feeamount)

for f.duemonth in([Apr- 15],[5月15日],[6月15日],[7月15日],[8月15日],[9月15日],[10月15日],[11月15日],[12月15日] ,[1月15日],[2月15日],[3月15日])

)作为PIV



END



GO

解决方案

这是我们遇到的最常见问题之一,也是我们所遇到的问题之一最没有能力回答,但你最有能力回答自己。



让我解释一下错误的含义:你试图使用变量,属性或者一个方法返回值,但它包含null - 这意味着变量中没有类的实例。

这有点像一个口袋:你的衬衫里有一个口袋,你用它来握笔。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。



我们无法分辨,因为我们不在那里,更重要的是,我们甚至看不到你的衬衫,更不用说口袋里的东西了!



回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。

但是你可以 - 而Visual Studio将在这里帮助你。在调试器中运行您的程序,当它失败时,VS将向您显示它发现问题的行。然后,您可以开始查看它的各个部分,以查看哪个值为null,并开始回顾代码以找出原因。因此,在包含错误行的方法的开头放置一个断点,然后从头再次运行程序。这一次,VS会在错误发生前停止,让你通过查看代码来查看你的价值来检查发生了什么。



但我们做不到那 - 我们没有您的代码,如果我们拥有它,我们不知道如何使用它,我们没有您的数据。所以尝试一下 - 看看你能找到多少信息!


标签lbl_aprTotalamount = GridView1.FindControl(   lbl_aprTotalamount as 标签; 



这一行给出了错误,因为当它位于多行时。所以请这样修改你的代码





 <   FooterTemplate  >  
< asp:label id = lbl_decTotalamount cssclass = footer_text runat = server text = 1 xmlns:asp = #unknown > < / asp:label >
< / FooterTemplate >









标签lbl_aprTotalamount =(标签)e.Row.FindControl(  lbl_aprTotalamount); 


尝试下面的代码: -



标签lbl_aprTotalamount =(标签)e.Row.FindControl(  lbl_aprTotalamount); 


Dear Friend I have one Gridview with the name Geidview1 when we calculate running total of column values on RowDataCommand Event it return an error "Object reference not set to an instance of an object"
below is the image of error

please any one can suggest us how we can remove this error.

below is my code

my c# code for bind gridview

public void bindgvduedate()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "readfeedetails";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
////cmd.Parameters.AddWithValue("@classname",Convert.ToString(ddlclassname.SelectedItem));
////cmd.Parameters.AddWithValue("@feecatename", Convert.ToString(ddlstcategory.SelectedItem));

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table );
GridView1.DataSource= table;
GridView1.DataBind();
con.Close();

}

this function i call on page load event

Griedview aspx code



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double"
Font-Size="11px" BorderWidth="3px"
CellPadding="4" GridLines="both"
ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
<rowstyle forecolor="#333333" backcolor="White" height="21px">
<columns>
<asp:TemplateField>
<HeaderTemplate>Apr-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lblaprl" runat="server" Text='<%#Eval("Apr-15") %>'>
<footertemplate> <asp:Label ID="lbl_aprTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>May-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lblmay" runat="server" Text='<%#Eval("May-15")%>'>
<footertemplate> <asp:Label ID="lbl_mayTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Jun-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lbljun" runat="server" Text='<%#Eval("Jun-15") %>'>
<footertemplate> <asp:Label ID="lbl_junTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Jul-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lbljul" runat="server" Text='<%#Eval("Jul-15") %>'>
<footertemplate> <asp:Label ID="lbl_julTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Aug-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lblaug" runat="server" Text='<%#Eval("Aug-15") %>'>
<footertemplate> <asp:Label ID="lbl_augTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Sep-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lblsep" runat="server" Text='<%#Eval("Sep-15") %>'>
<footertemplate> <asp:Label ID="lbl_sepTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Oct-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lbloct" runat="server" Text='<%#Eval("Oct-15") %>'>
<footertemplate> <asp:Label ID="lbl_octTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Nov-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lblnov" runat="server" Text='<%#Eval("Nov-15") %>'>
<footertemplate> <asp:Label ID="lbl_novTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Dec-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lbldec" runat="server" Text='<%#Eval("Dec-15") %>'>
<footertemplate> <asp:Label ID="lbl_decTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Jan-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lbljan" runat="server" Text='<%#Eval("Jan-15") %>'>
<footertemplate> <asp:Label ID="lbl_janTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Feb-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lblfeb" runat="server" Text='<%#Eval("Feb-15") %>'>
<footertemplate> <asp:Label ID="lbl_febTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Mar-15 </HeaderTemplate>
<itemtemplate><asp:Label ID="lblmar" runat="server" Text='<%#Eval("Mar-15") %>'>
<footertemplate> <asp:Label ID="lbl_marTotalamount" CssClass="footer_text" runat="server" Text="1">


<asp:TemplateField>
<HeaderTemplate>Total </HeaderTemplate>
<itemtemplate><asp:Label ID="lbltotal" runat="server" Text="0">
<footertemplate> <asp:Label ID="lbl_Totalsamount" CssClass="footer_text" runat="server" Text="1">


<footerstyle backcolor="White" forecolor="#333333">
<pagerstyle backcolor="#336666" forecolor="White" horizontalalign="Center">
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />


code for calculating running total

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{


if (e.Row.RowType == DataControlRowType.Footer)
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("select sum(feeamount) from dbo.feemaster where classname='KG'and feeactive=1 and feecatename='NEW'and duemonth='Apr-15'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
//e.Row.Cells[1].Text = "Total : ";
Label lbl_aprTotalamount = GridView1.FindControl("lbl_aprTotalamount") as Label;
lbl_aprTotalamount.Text = dr[0].ToString();
}

}

}


my database table

CREATE TABLE [dbo].[feemaster](
[feeId] [int] IDENTITY(1,1) NOT NULL,
[feetype] [nvarchar](50) NULL,
[feename] [nvarchar](50) NULL,
[classname] [nvarchar](50) NULL,
[duemonth] [nvarchar](50) NULL,
[feeamount] [decimal](15, 2) NULL,
[feeactive] [bit] NULL,
[feecatename] [nvarchar](50) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[feemaster] ON
INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) VALUES (1, N'One Time', N'Prospectus & Registration Fee', N'PRE NURSURY', N'Apr-15', CAST(300.00 AS Decimal(15, 2)), 1, N'NEW')
INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) VALUES (13, N'One Time', N'Prospectus & Registration Fee', N'NURSURY', N'Apr-15', CAST(300.00 AS Decimal(15, 2)), 1, N'NEW')
INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) VALUES (2, N'One Time', N'Prospectus & Registration Fee', N'KG', N'Apr-15', CAST(300.00 AS Decimal(15, 2)), 1, N'NEW')

INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) VALUES (16, N'Annual', N'Maintenance Fee', N'NURSURY', N'Apr-15', CAST(500.00 AS Decimal(15, 2)), 1, N'NEW')
INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) VALUES (17, N'Annual', N'Maintenance Fee', N'NURSURY', N'Apr-15', CAST(500.00 AS Decimal(15, 2)), 1, N'OLD')
INSERT [dbo].[feemaster] ([feeId], [feetype], [feename], [classname], [duemonth], [feeamount], [feeactive], [feecatename]) VALUES (18, N'Annual', N'Maintenance Fee', N'KG', N'Apr-15', CAST(500.00 AS Decimal(15, 2)), 1, N'NEW')



my stored procedure

create proc [dbo].[readfeedetails]
AS
BEGIN
select
*
from
(
select f.feename, f.feeamount,dd.duemonth
from feemaster as f
left outer join duedate as dd on dd.duemonth =f.duemonth where classname='KG' and feeactive=1 and feecatename='OLD'

) as f
pivot
(
min(f.feeamount)
for f.duemonth in ([Apr-15],[May-15],[Jun-15],[Jul-15],[Aug-15],[Sep- 15],[Oct-15],[Nov-15],[Dec-15],[Jan-15],[Feb-15],[Mar-15])
) as PIV

END

GO

解决方案

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!


Label lbl_aprTotalamount = GridView1.FindControl("lbl_aprTotalamount") as Label;


this line gave error because when it lies muliple rows. so please modify your code this way


<FooterTemplate>
            <asp:label id="lbl_decTotalamount" cssclass="footer_text" runat="server" text="1" xmlns:asp="#unknown"> </asp:label>
            </FooterTemplate>





Label lbl_aprTotalamount= (Label)e.Row.FindControl("lbl_aprTotalamount");


Try Below Code :-

Label lbl_aprTotalamount = (Label)e.Row.FindControl("lbl_aprTotalamount");


这篇关于你调用的对象是空的。在RowDataCommand的gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆