Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;
using System.IO;
using System.Text;
using System.Net;
using System.Configuration;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.Expressions;

public partial class Admin_view_record_demo : System.Web.UI.Page
{

    Class1 cls;
    

    protected void Page_Load(object sender, EventArgs e)
    {

        cls = new Class1();
        if (!Page.IsPostBack)
        {
            
            this.bind_grd1();
        }
    }



    protected void DownloadFile(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        Response.ContentType = ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
        Response.WriteFile(filePath);
        Response.End();
    }



    protected void bind_grd1()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "select_grid_demo";
        cmd.Connection = con;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        grd1.DataSource = ds;
        grd1.DataBind();

    }
    protected void grd1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grd1.EditIndex = e.NewEditIndex;
        this.bind_grd1();
    }
    protected void grd1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string admn_cmmnt=(grd1.Rows[e.RowIndex].FindControl("t1") as TextBox).Text;
        string aprORrjct = (grd1.Rows[e.RowIndex].FindControl("drpAOR_1") as DropDownList).SelectedItem.Value;
        string j_ID = grd1.DataKeys[e.RowIndex].Value.ToString();
        string strConnString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        using (SqlConnection con1 = new SqlConnection(strConnString))
        {
            string query1 = "update Admin_Added_Details set Admin_Comment=@Admin_Comment1,ApprovedORrejected=@ApprovedORrejected1 where ID=@ID1";
            using (SqlCommand cmd2 = new SqlCommand(query1))
            {
                cmd2.Connection = con1;
                cmd2.Parameters.AddWithValue("@Admin_Comment1", admn_cmmnt);
                cmd2.Parameters.AddWithValue("@ApprovedORrejected1",aprORrjct);
                cmd2.Parameters.AddWithValue("@ID1",j_ID);
                con1.Open();
                cmd2.ExecuteNonQuery();
                con1.Close();
            }
        }

         
    }
    
    protected void grd1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grd1.EditIndex = -1;
        bind_grd1();
    }




    protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && grd1.EditIndex==e.Row.RowIndex)
        {
                DropDownList ddrrpp = (DropDownList)e.Row.FindControl("drpAOR_1");
                string query = "select ApprovedORrejected from approved_OR_rejected ";
                SqlCommand cmd2 = new SqlCommand(query);
                ddrrpp.DataSource = GetData(cmd2);
                ddrrpp.DataTextField = "ApprovedORrejected";
                ddrrpp.DataValueField = "ApprovedORrejected"; 
                ddrrpp.DataBind();
                ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text).Selected = true;
                 
            }
        
    }


    private DataTable GetData(SqlCommand cmd2)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
        //string strr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        //using (SqlConnection conn = new SqlConnection(strr))
        //{
            using (SqlDataAdapter ssda = new SqlDataAdapter())
            {
                cmd2.Connection = con;
                ssda.SelectCommand = cmd2;
                using (DataTable ddt = new DataTable())
                {
                    ssda.Fill(ddt);
                    return ddt;
                }


            //}
        }
    }


     
}


m getting this error-
Object reference not set to an instance of an object.
ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text).Selected = true;

plzz help..
Posted
Updated 16-Dec-12 10:16am
v2

1 solution

either
C#
(e.Row.FindControl("LL0") as Label)


is returning null or

C#
ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text)


is null. You should be checking for null on both of them before trying to call one of their properties.

C#
Label label = (e.Row.FindControl("LL0") as Label);

if (label != null)
{
    ListItem item = ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text);
    
    if (item != null)
    {
        item.Selected = true;
    }
}


This should keep your page from crashing but there's no guarantee that it will operate in the way you think it should. That will depend on how your GridView is set up. However, you should be able to step through this using the debugger and determine which object is null.
 
Share this answer
 
v2
Comments
purohitritika 16-Dec-12 16:41pm    
This is my grid..

<asp:GridView ID="grd1" runat="server" AutoGenerateColumns="false" Width="100%"
ForeColor="WhiteSmoke" DataKeyNames="ID" onrowediting="grd1_RowEditing"
onrowupdating="grd1_RowUpdating"
onrowcancelingedit="grd1_RowCancelingEdit"
onrowdatabound="grd1_RowDataBound">
<columns>
<asp:TemplateField HeaderText="ID" ItemStyle-ForeColor="Aqua" HeaderStyle-ForeColor="IndianRed">
<itemtemplate>
<%# Eval("ID") %>



<asp:TemplateField HeaderText="Paper Title" HeaderStyle-ForeColor="IndianRed">
<itemtemplate>
<%# Eval("papertitle")%>



<asp:TemplateField HeaderText="Author Name" HeaderStyle-ForeColor="IndianRed" >
<itemtemplate>
<%# Eval("author") %>



<asp:TemplateField HeaderText="Author's Email ID" HeaderStyle-ForeColor="IndianRed">
<itemtemplate>
<%# Eval("email") %>



<asp:TemplateField HeaderText="Journal" HeaderStyle-ForeColor="IndianRed">
<itemtemplate>
<%# Eval("attachResearchPaper")%>



<asp:TemplateField HeaderText="Download" HeaderStyle-ForeColor="IndianRed">
<itemtemplate>
<asp:LinkButton ID="lnkdwn" runat="server" CommandName="download" CommandArgument='<%# Eval("FilePath") %>' Text="Download" OnClick="DownloadFile">



<asp:TemplateField HeaderText="Auto Generated ID" HeaderStyle-ForeColor="IndianRed">
<itemtemplate>
<%# Eval("autogenID")%>

purohitritika 16-Dec-12 16:42pm    
<asp:TemplateField HeaderText="Admin Comment" Visible="true">
<itemtemplate>
<%# Eval("Admin_Comment")%>


<edititemtemplate>
<asp:TextBox ID="t1" runat="server" Text='<%# Eval("Admin_Comment") %>'>




<asp:TemplateField HeaderText="Approved Or Rejected" Visible="true" ItemStyle-Width="100%" >
<itemtemplate>
<asp:Label runat="server" ID="LL0" Visible="true" Text='<%# Eval("ApprovedORrejected") %>'>



<edititemtemplate>
<asp:Label runat="server" ID="LL0" Visible="false" Text='<%# Eval("ApprovedORrejected") %>'>
<asp:DropDownList runat="server" ID="drpAOR_1" >





<asp:TemplateField HeaderText="Assigned On Date" Visible="true" >
<itemtemplate>
<%# Eval("Assigned_On_Date")%>



<asp:CommandField HeaderText="EDIT" CancelText="CANCEL" EditText="EDIT" ShowEditButton="true" ShowCancelButton="true" />




purohitritika 16-Dec-12 17:06pm    
Thank you so much rahkan..
Its working now.

Regards
Ritika.
AshishChaudha 17-Dec-12 5:54am    
Marked the solution as your answer ritika, as you got your solution..

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900