[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

Object reference not set to an instance of an object.

Asked by chris_desborough in Programming for ASP.NET, C# Programming Language

Tags: asp.net c#

Cannot resolve the error I'm receiving - Object reference not set to an instance of an object - for "dt.Rows.Count" in the FindRow method.
   

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
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.Collections;
 
public partial class Default3 : System.Web.UI.Page
{
 
    static DataTable dt = null;
    static DataRow dr = null;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["counter"] = 0;  // used to increment the 'id' Datatable value
 
            dt = new DataTable();
 
            dt.Columns.Add("id");
            dt.Columns.Add("qtyordered");
            dt.Columns.Add("itemdesc");
            dt.Columns.Add("unitofissue");
            dt.Columns.Add("itemno");
 
            ItemInOrder.DataSource = dt;
            ItemInOrder.DataBind();
 
            lblItemsInOrder.Text = "0";
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        int id = int.Parse(b.CommandArgument);
        DeletedRows.Add(id);
        ItemInOrder.DeleteRow(id);
    }
    protected void ItemInOrder_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt = (DataTable)ItemInOrder.DataSource;
        foreach (int i in DeletedRows)
        {
            DataRow dr = FindRow(dt, i);
            dt.Rows.Remove(dr);
        }
        ItemInOrder.DataSource = dt;
        ItemInOrder.DataBind();
    }
    private DataRow FindRow(DataTable dt, int id)
    {
      
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow row = dt.Rows[i];
            int rowId = int.Parse(row["id"].ToString());
            if (rowId == id)
                return row;
        }
        return dr;
    }
    public ArrayList DeletedRows
    {
        get
        {
            if (Session["DELETED_ROW_COLLECTION"] == null)
                Session["DELETED_ROW_COLLECTION"] = new ArrayList();
            return (ArrayList)Session["DELETED_ROW_COLLECTION"];
        }
    }
 
    protected void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
 
    }
 
    protected void SqlItemList_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
 
    }
 
    protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
    {
 
        // Get the currently selected row using the SelectedRow property.
        GridViewRow row = GridView1.SelectedRow;
 
        // Get the invisible gridview column Item_ID value  
        string idText = ((Label)GridView1.SelectedRow.FindControl("lblItemID")).Text;
 
        // Get the value of the textbox qty template field
        string qtyText = ((TextBox)GridView1.SelectedRow.FindControl("QtyToAdd")).Text;
  
        int maxOrderQty = Convert.ToInt32(row.Cells[6].Text);
        int orderQty = Convert.ToInt32(qtyText);
 
        if (orderQty > maxOrderQty)
        {
            // do something clever
            Response.Write("<script>confirm('Quantity you ordered exceeds the maximum order quantity for this item.')</script>");
            return;
        }
 
        // Insert data into the temp datatable
 
        DataRow dr = dt.NewRow();
 
        int @counter = Convert.ToInt32(Session["counter"]) + 1;
 
        dr["id"] = @counter;
        dr["qtyordered"] = qtyText;
        dr["itemdesc"] = row.Cells[2].Text;
        dr["unitofissue"] = row.Cells[3].Text;
        dr["itemno"] = row.Cells[5].Text;
 
        dt.Rows.Add(dr);
 
        ItemInOrder.DataSource = dt;
        ItemInOrder.DataBind();
 
        Session["counter"] = @counter; 
 
        lblItemsInOrder.Text = @counter.ToString();
 
    }
}
==========================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3"
    EnableEventValidation="false" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            color: #FFFFFF;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlCategoriesList" runat="server" ConnectionString="<%$ ConnectionStrings:docstoresConnectionString %>"
            SelectCommand="SELECT [Category] FROM [Category] WHERE ([Category] &lt;&gt; @Category) ORDER BY [Category]">
            <SelectParameters>
                <asp:Parameter DefaultValue="ALL ITEMS" Name="Category" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlItemList" runat="server" ConnectionString="<%$ ConnectionStrings:docstoresConnectionString %>"
            OnSelecting="SqlItemList_Selecting" SelectCommand="SELECT [Item_ID], [Item_Description], [Item_UnitofIssue], [Item_Image], [Item_ItemNo], [Item_MaxOrderQty] FROM [Items] WHERE (([Item_Active] = @Item_Active) AND ([Item_Category] = @Item_Category))">
            <SelectParameters>
                <asp:Parameter DefaultValue="true" Name="Item_Active" Type="Boolean" />
                <asp:ControlParameter ControlID="ddlCategory" Name="Item_Category" PropertyName="SelectedValue"
                    Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        <table style="width: 100%;">
            <tr>
                <td bgcolor="#0033CC">
                    <span class="style1">Select Category</span>
                    <asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True" DataSourceID="SqlCategoriesList"
                        DataTextField="Category" DataValueField="Category">
                    </asp:DropDownList>
                </td>
                <td>
                    &nbsp;
                </td>
                <td bgcolor="#0033CC" class="style1">
                    Order Total =
                    <asp:Label ID="lblItemsInOrder" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Item_ID"
                        DataSourceID="SqlItemList" EmptyDataText=".. No items in this category" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
                        OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
                        <Columns>
                            <asp:CommandField ButtonType="Button" SelectText="Add" ShowSelectButton="True" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:TextBox ID="QtyToAdd" runat="server" Width="25px"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="Item_Description" HeaderText="Description" SortExpression="Item_Description" />
                            <asp:BoundField DataField="Item_UnitofIssue" HeaderText="Unit of Issue" SortExpression="Item_UnitofIssue" />
                            <asp:BoundField DataField="Item_Image" HeaderText="Image" SortExpression="Item_Image" />
                            <asp:BoundField DataField="Item_ItemNo" HeaderText="Item No" SortExpression="Item_ItemNo" />
                            <asp:BoundField DataField="Item_MaxOrderQty" HeaderText="Max Qty" SortExpression="Item_MaxOrderQty" />
                            <asp:TemplateField HeaderText="ID" Visible="False">
                                <ItemTemplate>
                                    <asp:Label ID="lblItemID" runat="server" Text='<%# Eval("Item_ID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
                <td>
                    &nbsp;
                </td>
                <td valign="top">
                    <asp:GridView ID="ItemInOrder" runat="server" OnRowDeleting="ItemInOrder_RowDeleting"
                        EmptyDataText=".. no items in order">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Button ID="btnDelete" runat="server" Text="Del" CommandArgument='<%#DataBinder.Eval(Container,"DataItem.id")%>'
                                        OnClick="btnDelete_Click" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td valign="top">
                    &nbsp;
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 
Loading Advertisement...
 
[+][-]07/02/09 03:49 PM, ID: 24768549Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Programming for ASP.NET, C# Programming Language
Tags: asp.net c#
Sign Up Now!
Solution Provided By: guru_sami
Participating Experts: 4
Solution Grade: A
 
[+][-]07/02/09 03:32 PM, ID: 24768393Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/02/09 03:32 PM, ID: 24768395Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/02/09 03:34 PM, ID: 24768412Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/02/09 03:35 PM, ID: 24768417Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/02/09 03:37 PM, ID: 24768431Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/02/09 04:05 PM, ID: 24768657Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/02/09 04:10 PM, ID: 24768690Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/02/09 04:27 PM, ID: 24768789Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/02/09 04:40 PM, ID: 24768863Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625