发出错误的System.NullReferenceException [英] geting a erro System.NullReferenceException

查看:72
本文介绍了发出错误的System.NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

得到这个错误无法理解这里的错误





plz help ..





SqlDataSource sqlds =(SqlDataSource)e.Item.FindControl(SqlDataSource3)as SqlDataSource;

第86行:

第87行:sqlds.ConnectionString =数据源= DIMPLE;初始目录= torqbolt;集成安全性=真;

第88行:sqlds.SelectCommand =选择注释,rply_name,rpl_date,task_id来自rply where task_id ='+ lblID.Text +';





System.NullReferenceException:对象引用未设置为实例一个对象。





getting this error cant understand whats wrong here


plz help ..


SqlDataSource sqlds =(SqlDataSource) e.Item.FindControl("SqlDataSource3") as SqlDataSource;
Line 86:
Line 87: sqlds.ConnectionString = "Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True";
Line 88: sqlds.SelectCommand = "select comment,rply_name,rpl_date,task_id from rply where task_id ='" + lblID.Text + "'";


System.NullReferenceException: Object reference not set to an instance of an object.


<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource2" OnItemCommand="ListView1_ItemCommand"

     OnItemDataBound="use" DataKeyNames="task_id">
    <AlternatingItemTemplate><br>
    <div class="postback">
        <span>
        <asp:Label ID="id_Label" runat="server" Text='<%# Eval("task_id") %>' />
        <span class="heding_color">Assigned to </span>
        <asp:Label ID="post_nameLabel" runat="server" Text='<%# Eval("post_name") %>'  />
        <br />
        <br />
        <span class="heding_color">Description</span><br />
        <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>' />
        <br />

        <br />


        <br />


        <br />
        <span class="heding_color">Posted by : </span>
        <asp:Label ID="asigntask_nameLabel" runat="server"

            Text='<%# Eval("asign_name") %>'  />



        <span class="asgndate "><span class="heding_color"> Posted On </span>
        <asp:Label ID="curnt_dateLabel" runat="server"

            Text='<%# Eval("curnt_date") %>'  /></span><br />



            <span id='<%# Eval("task_id") %>'>

       <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource3">
       <ItemTemplate>
       <ul>
       <li>

           <asp:Label ID="task_id" runat="server" Text='<%# Eval("task_id") %>' />
       <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           <asp:Label ID="rply_name" runat="server" Text='<%# Eval("rply_name") %>' />
           <asp:Label ID="date" runat="server" Text='<%# Eval("rpl_date") %>' />
       </li>
       </ul>
       </ItemTemplate>

       </asp:ListView>

            <div class="cust_summary replaybtn">Replay</div>

            <div id="cust_details" class="cust_details" style="display:none; ">
            <br />

            <asp:TextBox ID="TextBoxL"    placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br /> <br />

                <asp:Button ID="Button3" CommandName="insert" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" /> </div> <br />

    </span></span></div>
    </AlternatingItemTemplate>


<ItemTemplate><br>
<div class="postback">
    <span>
    <asp:Label ID="id_Label" runat="server" Text='<%# Eval("task_id") %>'/>
    <span class="heding_color">Assigned to </span>
     <asp:Label ID="post_nameLabel" runat="server" Text='<%# Eval("post_name") %>'  />
    <br /><br />
    <span class="heding_color">Description</span><br />
    <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>'   />
    <br />
    <br />

    <br />

    <br />




    <br />
    <span class="heding_color"> Posted by : </span>
    <asp:Label ID="asign_nameLabel" runat="server"

        Text='<%# Eval("asign_name") %>' />


    <span class="asgndate"><span class="heding_color"> Posted On </span>
    <asp:Label ID="curnt_dateLabel" runat="server"

        Text='<%# Eval("curnt_date") %>' /></span><br /><br />


        <span id='<%# Eval("task_id") %>'>
    <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource3">
       <ItemTemplate>
       <ul>
       <li>

           <asp:Label ID="task_id" runat="server" Text='<%# Eval("task_id") %>' />
       <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           <asp:Label ID="rply_name" runat="server" Text='<%# Eval("rply_name") %>' />
           <asp:Label ID="date" runat="server" Text='<%# Eval("rpl_date") %>' />
       </li>
       </ul>
       </ItemTemplate>

       </asp:ListView>
        <div class="cust_summary replaybtn">Replay</div>

            <div id="cust_details" class="cust_details" style="display:none; "><br />

            <asp:TextBox ID="TextBoxL" Enabled="true"   placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br />
            <br />
                <asp:Button ID="Button3" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" />
                </div>
    <br />


   </span> </span></div>
</ItemTemplate>
    <LayoutTemplate>

    <br>
        <div ID="itemPlaceholderContainer" runat="server">


            <span runat="server" id="itemPlaceholder" />
        </div>
        <div style="">
        </div>
    </LayoutTemplate>
    <SelectedItemTemplate>
    <div class="postback">
        <span >
        <asp:Label ID="id_Label" runat="server" Text='<%# Eval("task_id") %>'/>
        <span class="heding_color">Assigned to </span>
          <asp:Label ID="post_nameLabel" runat="server" Text='<%# Eval("post_name") %>'   />
        <br />
        <span class="heding_color">Description</span><br />
        <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>' />
        <br />
        <br />


        <br />

               <span class="heding_color"> Posted by : </span>

        <asp:Label ID="asign_nameLabel" runat="server"

            Text='<%# Eval("asign_name") %>' />
        <span class="asgndate"><span class="heding_color">Posted On </span>
        <asp:Label ID="curnt_dateLabel" runat="server"

            Text='<%# Eval("curnt_date") %>' /></span><br /><br />

        <span id='<%# Eval("task_id") %>'>
         <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource3">
       <ItemTemplate>
       <ul>
       <li>
           <div><%# Eval("task_id") %></div>
           <asp:Label ID="task_id" runat="server" Text='<%# Eval("task_id") %>' />
       <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           <asp:Label ID="rply_name" runat="server" Text='<%# Eval("rply_name") %>' />
           <asp:Label ID="date" runat="server" Text='<%# Eval("rpl_date") %>' />
       </li>
       </ul>
       </ItemTemplate>

       </asp:ListView>

            <div class="cust_summary replaybtn">Replay</div>

            <div id="cust_details" class="cust_details" style="display:none; "><br />

            <asp:TextBox ID="TextBoxL" placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br />
            <br />
                <asp:Button ID="Button3"  class="btncomment" runat="server" Text="Post" onclick="Button3_Click" />
                </div>
        <br />
       <br />

       </span> </span></div>
    </SelectedItemTemplate>


</asp:ListView>


   <asp:SqlDataSource ID="SqlDataSource1" runat="server"

    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"

    SelectCommand="SELECT [name]FROM [names]">


</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" >
</asp:SqlDataSource>

 <asp:SqlDataSource ID="SqlDataSource3" runat="server" >
</asp:SqlDataSource>













protected void Page_Load(object sender, EventArgs e)
{
    SqlDataSource2.ConnectionString = "Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True";
    SqlDataSource2.SelectCommand = "select task_id,message,post_name,curnt_date,asign_name from task";

   // string s2 = System.Web.HttpContext.Current.User.Identity.Name;
     Session["name"] = s2;
    }











protected void use(object sender, ListViewItemEventArgs e)

{

Label d = (Label)e.Item.FindControl(\"id_Label\");

ViewState[\"task_id\"] = d.Text;

// try

// {

Label lblID =(Label)e.Item.FindControl(\"id_Label\") as Label;



SqlDataSource sqlds =(SqlDataSource) e.Item.FindControl(\"SqlDataSource3\") as SqlDataSource;



sqlds.ConnectionString = \"Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True\";

sqlds.SelectCommand = \"select comment,rply_name,rpl_date,task_id from rply where task_id =’\" + lblID.Text + \"’\";

}






protected void use(object sender, ListViewItemEventArgs e)
{
Label d = (Label)e.Item.FindControl("id_Label");
ViewState["task_id"] = d.Text;
// try
// {
Label lblID =(Label)e.Item.FindControl("id_Label") as Label;

SqlDataSource sqlds =(SqlDataSource) e.Item.FindControl("SqlDataSource3") as SqlDataSource;

sqlds.ConnectionString = "Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True";
sqlds.SelectCommand = "select comment,rply_name,rpl_date,task_id from rply where task_id ='" + lblID.Text + "'";
}

推荐答案

ConnectionStrings:ApplicationServices %>\"

SelectCommand=\"SELECT [name]FROM [names]\">


</asp:SqlDataSource>
<asp:SqlDataSource ID=\"SqlDataSource2\" runat=\"server\" >
</asp:SqlDataSource>

<asp:SqlDataSource ID=\"SqlDataSource3\" runat=\"server\" >
</asp:SqlDataSource>
ConnectionStrings:ApplicationServices %>" SelectCommand="SELECT [name]FROM [names]"> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" > </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource3" runat="server" > </asp:SqlDataSource>













protected void Page_Load(object sender, EventArgs e)
{
    SqlDataSource2.ConnectionString = "Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True";
    SqlDataSource2.SelectCommand = "select task_id,message,post_name,curnt_date,asign_name from task";

   // string s2 = System.Web.HttpContext.Current.User.Identity.Name;
     Session["name"] = s2;
    }











protected void use(object sender, ListViewItemEventArgs e)

{

Label d = (Label)e.Item.FindControl(\"id_Label\");

ViewState[\"task_id\"] = d.Text;

// try

// {

Label lblID =(Label)e.Item.FindControl(\"id_Label\") as Label;



SqlDataSource sqlds =(SqlDataSource) e.Item.FindControl(\"SqlDataSource3\") as SqlDataSource;



sqlds.ConnectionString = \"Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True\";

sqlds.SelectCommand = \"select comment,rply_name,rpl_date,task_id from rply where task_id =’\" + lblID.Text + \"’\";

}






protected void use(object sender, ListViewItemEventArgs e)
{
Label d = (Label)e.Item.FindControl("id_Label");
ViewState["task_id"] = d.Text;
// try
// {
Label lblID =(Label)e.Item.FindControl("id_Label") as Label;

SqlDataSource sqlds =(SqlDataSource) e.Item.FindControl("SqlDataSource3") as SqlDataSource;

sqlds.ConnectionString = "Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True";
sqlds.SelectCommand = "select comment,rply_name,rpl_date,task_id from rply where task_id ='" + lblID.Text + "'";
}


My guess is that you get your error here:

My guess is that you get your error here:
sqlds.ConnectionString = "Data Source=DIMPLE;Initial Catalog=torqbolt;Integrated Security=True";



because sqlds is null.



Use the debugger and set a break point here:


because sqlds is null.

Use the debugger and set a break point here:

Label d = (Label)e.Item.FindControl("id_Label");



then step through the code.


then step through the code.


how to solve null reference exception ?
step 1:Check in which you are getting Exception
step 2:you must be invoking the member(method or property) of some object (that object must be not initialized any where)
step 3: initialized that object of which you are calling the member (method or property)


这篇关于发出错误的System.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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