Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System.NullReferenceException: Object reference not set to an instance

Status
Not open for further replies.

BunBo900

MIS
Dec 11, 2009
50
US
Hello:

The code below works fine on one server, but send error msg on another server. Please advice. Thanks.

Error Msg: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

protected void Page_Load(object sender, EventArgs e)
{
//My Tickets if Basic User
if (!IsPostBack)
{
edipi = commonHD.getEDIPI();

ViewState["strSortBy"] = "TicketID";
ViewState["strSortDirection"] = "DESC";
bindTickets();

//Initial Sort - By TicketId in Descending Order
DataView dtsort = (DataView)Session["dvGrid"];
string str = ViewState["strSortBy"] + " " + ViewState["strSortDirection"];
if (str != "")
{
dtsort.Sort = str; ******Error here *****
gvTickets.DataSource = dtsort;
gvTickets.DataBind();
}
}
}
 
What is the value of str? If it is NULL, your code will throw the error you are seeing.
 
I see what you're saying. What confused me is it did not give me the error message on production server. How can I trap? or what I need to do in this case? Thanks
 
I would change the if:
Code:
 if(!String.IsNullOrWhiteSpace(str))
         { 
            gvTickets.DataSource = dtsort;
         }
       else
        {
           gvTickets.DataSource = "Set a Default Sort Order";
         }
       gvTickets.DataBind();
 
I think part of the problem is in the aspx page; data need to be loaded in the Gridview first to avoid empty/null string. The system uses the person login credential to return the list of data belong to him.
Let say.

lname string is credential of the person logon the system.

Is it possible to pass lname string like so below?

<asp:SqlDataSource ID="srcTickets" runat="server" ConnectionString="<%$ ConnectionStrings:HelpDesk %>" DataSourceMode="DataSet" SelectCommand="select firstname, lastname, customer, date FROM tblname where lastname=lname;

<asp:GridView ID="gvTickets" runat="server" AllowSorting="true" PageSize="20" AutoGenerateColumns="false"
DataSourceID="srcTickets" DataKeyNames="ticketId">
 
I don't use the datasource conttrols. I would create a stored procedure and return only the data that the user is authorized to view.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top